Ask a simple question, get a simple answer

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
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

https://www.dropbox.com/s/eskuvw1zgq8zp ... r.zip?dl=1

Code: Select all

    --[[ Floor triggers should call floorTriggeredEffect() on activate.
        Scripts and timers can call spinTo or spinTo2 directly.
        Scripts can include the chosen direction for spinTo or spinTo2.
        Timers and triggers default to a random direction.

       If you plan to call the effect from a floor_trigger through a script that
       calls either spinTo or SpinTo2, do so by calling your function with a delay,
       as shown in floorTriggerEffect(); or change delayedCall in floorTriggerEffect,
       to call your function name instead.

       And/or/also  delayedCall can send a facing direction as parameter to spinTo/spinTo2.
    --]]   

    function floorTriggeredEffect()    
       delayedCall(self.go.id, .35, "spinTo2")
    end

    function spinTo(self, facing)
       local facing = facing or math.random(4)%3
       local dir = iff(math.random() <.5, 1, -1)
       local loc = {}
       loc[1],loc[2],loc[3],loc[5] = party:getPosition()
       loc[4] = party.elevation
       loc[3] = facing
       party:setPosition(unpack(loc))
    end

    --[[
        spinTo2 is a cool alternate of the effect, but it might be unstable :(
       It crashed on me once, and I haven't been able to reproduce it.
       The error it gave, happened while checking for obstacles.
       (Afterwards) I had set it up on a timer to run it every .4 seconds while trying
       to wander the map in every direction and bump into things for a good five minutes.
       
       it's been edited since, and I've had no problems with it since,
       but it's something to be aware of.  If you use it and have any problem, you can
       always switch to spinTo instead; all you lose is the animated turn... which might be
       what you want anyway. spinTo's effect is instant.
    --]]

    function spinTo2(caller, facing)
       local facing = facing or math.random(4)%3
       local dir = iff(math.random() <.5, 1, -1)
       local loc = {}
       loc[1],loc[2],loc[3],loc[5] = party:getPosition()
       loc[4] = party.elevation
       loc[3] = facing - dir
       party:setPosition(unpack(loc))
       delayedCall(self.go.id, .01, "turn", dir)
    end

function turn(dir)
		if party.party:isIdle() then
	       party.party:turn(dir)
		end	
end
Updated two mistakes, noticed by minmay. 8-)
Last edited by Isaac on Wed Jun 17, 2015 7:18 pm, edited 2 times in total.
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

you need %4 not %3, and the reason your spinto2 doesn't work is that it tries to turn the party without making sure the party is idle first.
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.
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

minmay wrote:you need %4 not %3
Good catch [%3]. It's a typo ~in the sense, that I know this, and mistakenly typed it wrong anyway... and then didn't see it even after all the tests. :shock: That line is just the random direction case; and I missed its flawed effect because I only paid close attention to the implicitly given directions, and their results. I just never noticed that the random choice was never 3.
, and the reason your spinto2 doesn't work is that it tries to turn the party without making sure the party is idle first.
Do you mean as reason for the one time it crashed, early on? Other than that, it works as intended (even if I'm still missing something). Though I'd love to improve it.

** Do you know whether or not if the turn method is safe if called while the party is not idle? I've not been able to crash it since, and that's with calling :turn hundreds of times while constantly moving the party.
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Isaac wrote:** Do you know whether or not if the turn method is safe if called while the party is not idle? I've not been able to crash it since, and that's with calling :turn hundreds of times while constantly moving the party.
i don't know if it's "safe" to call it while the party is moving, but i do know that it's stupid to call it while the party is moving
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.
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

minmay wrote:
Isaac wrote:** Do you know whether or not if the turn method is safe if called while the party is not idle? I've not been able to crash it since, and that's with calling :turn hundreds of times while constantly moving the party.
i don't know if it's "safe" to call it while the party is moving, but i do know that it's stupid to call it while the party is moving
Thank you for the kind comment.

I'll look into it, but [currently] I do think that kind of a check would over-complicate it (possibly necessary or not).
Last edited by Isaac on Wed Jun 17, 2015 6:51 pm, edited 2 times in total.
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

you're welcome

but seriously using it while the party is moving is a terrible idea, it'll stop the move and put the camera in the wrong place. checking if the party is idle, which is what you are explicitly told to do, before calling turn() or move(), is not overcomplicating
Last edited by minmay on Wed Jun 17, 2015 6:54 pm, edited 2 times in total.
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.
User avatar
Duncan1246
Posts: 404
Joined: Mon Jan 19, 2015 7:42 pm

Re: Ask a simple question, get a simple answer

Post by Duncan1246 »

Isaac wrote:
function spinTo2(caller, facing)
local facing = facing or math.random(4)%4
local dir = iff(math.random() <.5, 1, -1)
I don't know these expressions. What means the 'or' in the first line, and 'ifff' in the second, please?
The Blue Monastery (LOG1)
download at:http://www.nexusmods.com/grimrock/mods/399/?

Finisterrae(LOG2)
download at:http://www.nexusmods.com/legendofgrimrock2/mods/61/?
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

Duncan1246 wrote:
Isaac wrote:
function spinTo2(caller, facing)
local facing = facing or math.random(4)%4
local dir = iff(math.random() <.5, 1, -1)
I don't know these expressions. What means the 'or' in the first line, and 'ifff' in the second, please?
minmay might have some insight I do not, but I am using this because the function may, or may not be called with a parameter. That declaration makes a new local facing variable and assigns it the value of the existing facing variable ~if it exists; if it doesn't exist, [or] it assigns a random facing value.

This lets the function be called from a script and given a direction to turn to; and allows a trigger or timer to call it, and picks a random direction in that case.
User avatar
Duncan1246
Posts: 404
Joined: Mon Jan 19, 2015 7:42 pm

Re: Ask a simple question, get a simple answer

Post by Duncan1246 »

Isaac wrote:
Duncan1246 wrote:
Isaac wrote:
function spinTo2(caller, facing)
local facing = facing or math.random(4)%4
local dir = iff(math.random() <.5, 1, -1)
I don't know these expressions. What means the 'or' in the first line, and 'ifff' in the second, please?
minmay might have some insight I do not, but I am using this because the function may, or may not be called with a parameter. That declaration makes a new local facing variable and assigns it the value of the existing facing variable ~if it exists; if it doesn't exist, [or] it assigns a random facing value.

This lets the function be called from a script and given a direction to turn to; and allows a trigger or timer to call it, and picks a random direction.
So, the first line means facing is facing if facing exists, OR math.random if not. OK, and ifff(math.random()<.5,1,-1)??
Thanks for your reply!
The Blue Monastery (LOG1)
download at:http://www.nexusmods.com/grimrock/mods/399/?

Finisterrae(LOG2)
download at:http://www.nexusmods.com/legendofgrimrock2/mods/61/?
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

minmay wrote: but seriously using it while the party is moving is a terrible idea, it'll stop the move and put the camera in the wrong place. checking if the party is idle, which is what you are explicitly told to do, before calling turn() or move(), is not overcomplicating
This is good insight. I will work on it.

*I wish that had made it into the script reference.

(My initial offhand guess was that any pass or fail check before the :turn, would just make the function fail, without recursion of some kind.)
Last edited by Isaac on Wed Jun 17, 2015 7:06 pm, edited 1 time in total.
Post Reply