How to add a hook

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

How to add a hook

Post by strangely »

I'm sorry if this seems like a simple question but I've been unable to find the answer so far.
How do I add a hook?
For example, what's the syntax to add an onAttack hook to the party?

Something like:

party.part:onAttack = function( <parameters>...)

doMySpecialThing...

end

Thanks for any help
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: How to add a hook

Post by cromcrom »

Instead of feeding you a fish, I will teach you how to fish, and make you a free man, instead of being a slave to others: type onAttack, or onMove, or onDie, in the search box of the forum, and look for examples, there are tens of them here and there.
A trip of a thousand leagues starts with a step.
User avatar
strangely
Posts: 25
Joined: Mon Oct 29, 2012 2:28 am
Location: Tucson, AZ

Re: How to add a hook

Post by strangely »

OK, I tried to search for the term before I posted here.
I can't find an example of how to hook up the function that works in grimrock 2.

I've seen examples of something like this:

Code: Select all

cloneObject{
	name = "party",
	baseObject = "party",

	onAttack = function(self, champion, action, slot)
		hudPrint("Attacking")
	end,
}
added to objects.lua but when I try that I get the following error on dungeon load:

mod_assets/scripts/objects.lua:5: attempt to call global 'cloneObject' (a nil value).

Most of the examples I've seen are from before G2 was released so I assume they are for G1 and the scripting syntax has changed in G2.
User avatar
Prozail
Posts: 158
Joined: Mon Oct 27, 2014 3:36 pm

Re: How to add a hook

Post by Prozail »

Here is one example that will prob. answer all your questions.

viewtopic.php?f=22&t=7974
User avatar
strangely
Posts: 25
Joined: Mon Oct 29, 2012 2:28 am
Location: Tucson, AZ

Re: How to add a hook

Post by strangely »

That's exactly what I needed.

Thanks!
User avatar
strangely
Posts: 25
Joined: Mon Oct 29, 2012 2:28 am
Location: Tucson, AZ

Re: How to add a hook

Post by strangely »

That's working well but now I have another question.
In Jgwman's scripts, he defines the onAttack hook as follows:

Code: Select all

onAttack = function(party, champion, weapon)
   ...
end
but the scripting reference shows that onAttack has the following signature:

Code: Select all

onAttack(self, champion, action, slot)
I modified the code to use the latter signature and it is called with no errors. However, I'm not sure what is in the 'action' parameter.
type(action) returns 'table' but the following code prints nothing:

Code: Select all

for key,value in pairs(action) do
   hudPrint(key, value)
end
Anyone know which is the correct signature or, if both are fine, what's supposed to be in the action parameter?

Thanks
Post Reply