Trying to write a party onTurn 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
philippe.martin
Posts: 9
Joined: Fri May 15, 2015 10:44 am

Trying to write a party onTurn hook

Post by philippe.martin » Fri May 15, 2015 11:14 am

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...

User avatar
Xanathar
Posts: 629
Joined: Sun Apr 15, 2012 10:19 am
Location: Torino, Italy
Contact:

Re: Trying to write a party onTurn hook

Post by Xanathar » Fri May 15, 2015 2:18 pm

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" ;)
Waking Violet (Steam, PS4, PSVita, Switch) : http://www.wakingviolet.com

The Sunset Gate [MOD]: viewtopic.php?f=14&t=5563

My preciousss: http://www.moonsharp.org

philippe.martin
Posts: 9
Joined: Fri May 15, 2015 10:44 am

Re: Trying to write a party onTurn hook

Post by philippe.martin » Fri May 15, 2015 4:20 pm

That does the trick !

A warm thank you to this quick reply !

minmay
Posts: 2700
Joined: Mon Sep 23, 2013 2:24 am

Re: Trying to write a party onTurn hook

Post by minmay » Fri May 15, 2015 6:41 pm

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.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.

philippe.martin
Posts: 9
Joined: Fri May 15, 2015 10:44 am

Re: Trying to write a party onTurn hook

Post by philippe.martin » Fri May 15, 2015 8:43 pm

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 ?

minmay
Posts: 2700
Joined: Mon Sep 23, 2013 2:24 am

Re: Trying to write a party onTurn hook

Post by minmay » Fri May 15, 2015 9:12 pm

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.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.

philippe.martin
Posts: 9
Joined: Fri May 15, 2015 10:44 am

Re: Trying to write a party onTurn hook

Post by philippe.martin » Sat May 16, 2015 12:29 am

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

Post Reply