Page 1 of 1

Trying to write a party onTurn hook

Posted: Fri May 15, 2015 11:14 am
by philippe.martin
Hi There,

I'm quite new on LoG2 scripting. For one of my puzzle I'd like to test wether my party has turned using the onTurn Party hook.

After reading this excellent topic viewtopic.php?f=14&t=5803&hilit=+hook, I try doing the same in my LoG2 dungeon editor.

I was following the 3 steps, apart the third step which I adapt to LoG2 like described here : viewtopic.php?f=22&t=7642

The code in my party_hooks.lua file is (actually I only need onTurn hook) :

Code: Select all

defineObject{
   name = "party",
   baseObject = "party",
   components = {
		{
        class = "Party",
		onTurn = function(party,dir,arg1) 
			--print(party.go.id,"turning to",dir)
			return hooks.party_onTurn(party, dir)
		end
		}
	},
}
In my dungeon editor I create an lua script named 'hooks' containing :

Code: Select all

function party_onTurn(party, dir)
	print (dir)
end
Until here all is OK, dungeon is starting correctly. When my party is turning on the following occurs:

Code: Select all

mod_assets/scripts/party_hooks.lua:9: attempt to call field 'party_onTurn' (a nil value)
stack traceback:
mod_assets/scripts/party_hooks.lua:9: in function <mod_assets/scripts/party_hooks.lua:7>
...
I don't see my mistake, could someone light my way ? Any help would be appreciate.

N.B. Calling print(hooks) into onTurn function (insteed of print(party.go.id,"turning to",dir) ) returns a table...

Re: Trying to write a party onTurn hook

Posted: Fri May 15, 2015 2:18 pm
by Xanathar

Code: Select all

return hooks.script.party_onTurn(party, dir)
basically: "hooks" refers to the GameObject, not the script component, you want to reference the script component, thus "hooks.script" ;)

Re: Trying to write a party onTurn hook

Posted: Fri May 15, 2015 4:20 pm
by philippe.martin
That does the trick !

A warm thank you to this quick reply !

Re: Trying to write a party onTurn hook

Posted: Fri May 15, 2015 6:41 pm
by minmay
philippe.martin wrote:For one of my puzzle I'd like to test wether my party has turned using the onTurn Party hook.
You need to be careful about this because players can also turn using free-look, which does not call the onTurn hook.

Re: Trying to write a party onTurn hook

Posted: Fri May 15, 2015 8:43 pm
by philippe.martin
minmay wrote:
philippe.martin wrote:For one of my puzzle I'd like to test wether my party has turned using the onTurn Party hook.
You need to be careful about this because players can also turn using free-look, which does not call the onTurn hook.
Right ! Again thank you for the tips !

Is there a way to catch party free-look ?

Re: Trying to write a party onTurn hook

Posted: Fri May 15, 2015 9:12 pm
by minmay
During free-look, you can inspect the party's transformation matrix with party:getWorldRotation() and calculate the camera angle from that. If you only need to catch the actual act of turning, which only occurs once the mouse is actually released, simply check party.facing on every update (easy way to do this: put a TimerComponent on the party with an interval of 0.0001, timers can only trigger exactly once per update so the timer's onActivate hook will be called exactly once per update; it does NOT activate 10000 times per second, so don't worry about performance).

There is also a DisableMouseLook game flag, but shutting off a basic interface feature is a pretty terrible thing to do for a puzzle, so please don't do that.

Re: Trying to write a party onTurn hook

Posted: Sat May 16, 2015 12:29 am
by philippe.martin
minmay wrote:During free-look, you can inspect the party's transformation matrix with party:getWorldRotation() and calculate the camera angle from that. If you only need to catch the actual act of turning, which only occurs once the mouse is actually released, simply check party.facing on every update (easy way to do this: put a TimerComponent on the party with an interval of 0.0001, timers can only trigger exactly once per update so the timer's onActivate hook will be called exactly once per update; it does NOT activate 10000 times per second, so don't worry about performance).

There is also a DisableMouseLook game flag, but shutting off a basic interface feature is a pretty terrible thing to do for a puzzle, so please don't do that.
Yes ! Working perfect !

Thanks again ! :P