Error Calling "onHitMonster"

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!
User avatar
strangely
Posts: 25
Joined: Mon Oct 29, 2012 2:28 am
Location: Tucson, AZ

Error Calling "onHitMonster"

Post 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
cameronC
Posts: 80
Joined: Wed Apr 11, 2012 10:54 pm

Re: Error Calling "onHitMonster"

Post 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.
Writer and sometimes artist of the very slightly acclaimed comic series Scrambled Circuits. A comic series about a robot written by a human.

Grimrock 2 socketSystem: viewtopic.php?f=22&t=8546 / Github
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Error Calling "onHitMonster"

Post 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.
User avatar
strangely
Posts: 25
Joined: Mon Oct 29, 2012 2:28 am
Location: Tucson, AZ

Re: Error Calling "onHitMonster"

Post 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.
User avatar
strangely
Posts: 25
Joined: Mon Oct 29, 2012 2:28 am
Location: Tucson, AZ

Re: Error Calling "onHitMonster"

Post 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...
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Error Calling "onHitMonster"

Post 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.
Last edited by NutJob on Sun Nov 23, 2014 1:14 am, edited 1 time in total.
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Error Calling "onHitMonster"

Post 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?
User avatar
strangely
Posts: 25
Joined: Mon Oct 29, 2012 2:28 am
Location: Tucson, AZ

Re: Error Calling "onHitMonster"

Post by strangely »

Ooh, yes, I second that request!

Cheers.
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Error Calling "onHitMonster"

Post 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.
Last edited by NutJob on Sun Nov 23, 2014 8:43 pm, edited 1 time in total.
cameronC
Posts: 80
Joined: Wed Apr 11, 2012 10:54 pm

Re: Error Calling "onHitMonster"

Post by cameronC »

o.item:addConnector("onEquipitem", "lib", "aaa")
lowercase "item"?
Writer and sometimes artist of the very slightly acclaimed comic series Scrambled Circuits. A comic series about a robot written by a human.

Grimrock 2 socketSystem: viewtopic.php?f=22&t=8546 / Github
Post Reply