Page 1 of 2

Error Calling "onHitMonster"

Posted: Fri Nov 21, 2014 10:38 pm
by strangely
I'm having trouble getting a callback to work in the editor.

I used the following code to add connectors to the weapons in my level:

Code: Select all

    -- Wire up the MeleeAttackComponents on this level.
    for e in second_timer.map:allEntities() do
        if e.meleeattack then

            e.meleeattack:addConnector("onHitMonster", "myscript", "onHitMonster")
        end
    end
I then add the following to the "myscript" file:

Code: Select all

function onHitMonster(meleeAttackComponent, monster, tside, damage, champion)
    print("Here")
end
When I use a weapon, the editor throws the following error:

[string "Component.lua"]:0: attempt to index field 'map' (a nil value)
stack traceback:
[string "Component.lua"]: in function 'triggerConnectors'
[string "Component.lua"]: in function 'callHook'
[string "Component.lua"]: in function 'attack'

...

Anyone got any idea what's going on here?

Thanks

Re: Error Calling "onHitMonster"

Posted: Fri Nov 21, 2014 11:39 pm
by cameronC
Have you tried using this:

Code: Select all

if e:getComponent("meleeattack") then
It returns true if the named component exists, false if not.

Re: Error Calling "onHitMonster"

Posted: Sat Nov 22, 2014 12:10 am
by NutJob
Can you try using something other than "e"? If I recall I once had a problem using that as a variable because, I believe, it's a defined constant in the LoG2 API. I could be wrong, but I ran into a few odd errors, because of the names I choose for variables.

Re: Error Calling "onHitMonster"

Posted: Sat Nov 22, 2014 12:30 am
by strangely
Thanks for the suggestions but I tried both of them and neither one helped.

I verified that I was working with the right component by adding a call to MeleeAttackComponent:setCameraShake(true) instead of adding my connector and the camera shakes when the item is used.

Re: Error Calling "onHitMonster"

Posted: Sat Nov 22, 2014 12:50 am
by strangely
In the mean time, I'll just have to use @JKos's cloneObject script and define each weapon in the game with an overridden 'onHitMonster' function. I wanted to do it with script so I didn't have to update my items.lua for every weapon in the game but at least cloneObject allows me to continue...

Re: Error Calling "onHitMonster"

Posted: Sat Nov 22, 2014 10:50 pm
by NutJob
attempt to index field 'map' (a nil value)

Yup, confirmed, I'm now having the same problem using addConnector on components. Floortriggers and Party, no problem. Practically anything else such as obj.item:addConnector("onEquipItem", self.go.id, "doSomething") *WILL* doSomething but it's immediately followed by the underlined statement/error above. If I put my function back into the items defineObject all works fine with no "map is nil" error.

Edit: just realized adding onTurn no longer works either when adding it to the party through a script. onMove, onRest, onDamage, onDie, etc (all the rest of the events) can be added with addConnector but onTurn no longer is firing for me as if I never used addConnector("onTurn", blah, blah). It was working a couple weeks ago, the last I made a script using it, but I see now it no longer does. No error though, just doesn't execute the function the event is attached to. BUT if I put it [onTurn] into the party defineObject all works as it's supposed to.

Re: Error Calling "onHitMonster"

Posted: Sun Nov 23, 2014 12:11 am
by NutJob
How do I open the grimrock2.dat and extract the Component.lua from it, so I can see what the heck is going on?

OR

Could a dev post the functions callHook and triggerConnectors?

Re: Error Calling "onHitMonster"

Posted: Sun Nov 23, 2014 3:40 am
by strangely
Ooh, yes, I second that request!

Cheers.

Re: Error Calling "onHitMonster"

Posted: Sun Nov 23, 2014 3:58 am
by NutJob
So to clarify [to a Dev or any others concerned], this works in items.lua (or any file that's imported in)...

1.

Code: Select all

defineObject{
	baseObject = "base_item",
	name = "rope",
	components = {
		{
			class = "Model",
			model = "assets/models/items/rope.fbx",
			name = "model"
		},
		{
			class = "Item",
			gfxIndex = 318,
			uiName = "Rope",
			impactSound = "impact_blunt",
			weight = 4.4,
			description = "A long and sturdy rope is the best ally of all daring spelunkers.",
			primaryAction = "climb",
			traits = {
				"usable"
			},
			onEquipItem = function(self, champion, slot) print(self, champion, slot) end,
			onUnequipItem = function(self, champion, slot) print(self, champion, slot) end,
		},
		{
			class = "RopeTool",
			cooldown = 4.5,
			name = "climb",
			uiName = "Climb Down"
		}
	}
}
...and this does not work called from another function that requires a Rope to be created at x,y,z,facing:

2.

Code: Select all

-- script_entity "lib"
function specialRope(x, y, z, facing)
	local o = spawn("rope", party.level, x, y, facing, z)
	o.item:addConnector("onUnequipItem", "lib", "bbb")
	o.item:addConnector("onEquipItem", "lib", "aaa")
end

function aaa()
	print("A")
end

function bbb()
	print("B")
end
NOTE: #2 method did work at one point in time.

Edit: Fixed my display code that cameronC pointed out. Thank you. Also, still errors out with the eventName correctly written.

Re: Error Calling "onHitMonster"

Posted: Sun Nov 23, 2014 6:05 pm
by cameronC
o.item:addConnector("onEquipitem", "lib", "aaa")
lowercase "item"?