Page 1 of 1

onUseItem bug

Posted: Sun Jan 25, 2015 2:49 am
by AdrTru
I have problem with detect onUseItem situation.
If I addConnector to item.usableitem it look like good but after using item program made this error:
Image

For way around this problem I try use FW hook. If I create import "mod_assets/scripts/clone_object.lua" as JKos wrote and add

Code: Select all

   modifyObjects{
       filter = {
			hasComponents = {"item","usableitem"},
       },
       components = {
			fw_addHooks{
			class = 'UsableItem',
			},
       }
    }
all touched object lost its standard onUseItem features (are only consumed ).

What I must do for managing onUseItem event when I want to define it from script during running game for its correct working?
thx.

Re: onUseIem bug

Posted: Sun Jan 25, 2015 9:51 am
by Eleven Warrior
Hi ArdTru you may need to post a pm to Jkos or Minmay for help mate :)

Re: onUseIem bug

Posted: Sun Jan 25, 2015 10:33 am
by minmay
When a connector is triggered, it checks the component's parent's map in some way. I don't know why it does this, but it does. This means that a connector triggering on a component belonging to an object that is not on a map - such as an item in a champion's inventory, on the mouse, or inside a container - will cause the Lua error you discovered.
If you want to use connectors on items, the easiest way is definitely to use the scripting framework. But be careful about function ownership. In particular, you should only reference the global environment (and any arguments you receive) in your hook functions.
This is because saving/reloading with a function that is referenced from multiple places can change the environment of the function. For example I created a test dungeon with two script entities named "cow" and "goat". I placed "goat" to the northwest of "cow". Here is the source for "cow":

Code: Select all

ANIMAL = "Cow"

function test()
	print(ANIMAL)
end
And the source for "goat":

Code: Select all

ANIMAL = "Goat"

dtest = cow.script.test
Now if I export this dungeon, start the game, and try calling both function references:

Code: Select all

cow.script.test()
Cow
goat.script.test()
Cow
We can see that both fields have cow.script as their local environment.
But if I save and reload the dungeon, then try the test again:

Code: Select all

cow.script.test()
Goat
goat.script.test()
Goat
Now both fields have goat.script as their local environment! If you're ever going to hold references to a function outside of the script that defines it, don't try to use the local environment. It will absolutely bite you in the ass.

On the other hand, this is really useful if you want to detect when the player reloads the game!