onUseItem bug

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
AdrTru
Posts: 223
Joined: Sat Jan 19, 2013 10:10 pm
Location: Trutnov, Czech Republic

onUseItem bug

Post 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.
Last edited by AdrTru on Sat Feb 07, 2015 5:22 pm, edited 1 time in total.
My LOG2 projects: virtual money, Forge recipes, liquid potions and
MultiAlcoveManager, Toolbox, Graphic text,
User avatar
Eleven Warrior
Posts: 752
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re: onUseIem bug

Post by Eleven Warrior »

Hi ArdTru you may need to post a pm to Jkos or Minmay for help mate :)
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: onUseIem bug

Post 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!
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Post Reply