Page 1 of 1
How to add a hook
Posted: Fri Nov 07, 2014 6:18 am
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
Re: How to add a hook
Posted: Fri Nov 07, 2014 7:40 am
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.
Re: How to add a hook
Posted: Fri Nov 07, 2014 12:33 pm
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.
Re: How to add a hook
Posted: Fri Nov 07, 2014 12:40 pm
by Prozail
Here is one example that will prob. answer all your questions.
viewtopic.php?f=22&t=7974
Re: How to add a hook
Posted: Fri Nov 07, 2014 12:47 pm
by strangely
That's exactly what I needed.
Thanks!
Re: How to add a hook
Posted: Fri Nov 07, 2014 1:23 pm
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