[Technique] Trigger a function based on direction traveled

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
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

[Technique] Trigger a function based on direction traveled

Post by NutJob »

.............................
Last edited by NutJob on Thu Nov 20, 2014 12:19 am, edited 2 times in total.
User avatar
MadCatter
Posts: 34
Joined: Mon Nov 03, 2014 5:02 am
Location: Southampton, UK

Re: [Technique] Trigger a function based on direction travel

Post by MadCatter »

This could be very useful for making enemies that respawn when you enter certain areas, or resetting puzzles. Thanks. I'll give it a test when I have time. :D
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: [Technique] Trigger a function based on direction travel

Post by Grimfan »

Now that is nice work NutJob! I will definitely be using some variation of this myself. Code copied! :D
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: [Technique] Trigger a function based on direction travel

Post by NutJob »

Thanks, both of you. I'll probably wrap it all up in one easy routine to use eventually. Something like...

Code: Select all


oneWayTrigger(
    x, y, z, -- for the floor_trigger
    int_direction, -- direction *the party is traveling* it triggers on (0 - 3)
    "script_id",
    "mainFunc",
    "resetFunc"
)
...that can be called on level setup (or init)

I know I don't want to hand code it every time I want to use. Keep an eye on this thread.
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

.................

Post by NutJob »

...................
Last edited by NutJob on Wed Nov 12, 2014 1:29 am, edited 1 time in total.
ninjanerdbgm
Posts: 28
Joined: Tue Nov 04, 2014 6:53 pm

Re: [Technique] Trigger a function based on direction travel

Post by ninjanerdbgm »

I needed code like that in my custom dungeon. This was my solution:

in init.lua

Code: Select all

      
defineObject{
   name = "party",
   baseObject = "party",
   components = {
      {
        class = "Party",
      onMove = function(party,dir,arg1) 
            somescript.script.printDir(dir)
      end,
      },
     },
}
I put a script in my map, called it "somescript", and give it this function:

Code: Select all

function printDir(d)
	print(d)
end
With that code, whenever the party moves north (no matter what direction they're facing), it always prints "0". Whenever they move east, it prints "1", south "2", and west "3".

That was kind of pointless for me, though, so I came up with this code to return the direction the party is moving regardless of facing:

Code: Select all

function printDir(d)
	-- Facing north
	if party.facing == 0 then
		if d == 0 then
			print("forward")
		elseif d == 1 then
			print("right")
		elseif d == 2 then
			print("back")
		else
			print("left")
		end
	-- Facing east
	elseif party.facing == 1 then
		if d == 0 then
			print("left")
		elseif d == 1 then
			print("forward")
		elseif d == 2 then
			print("right")
		else
			print("back")
		end
	-- Facing south
	elseif party.facing == 2 then
		if d == 0 then
			print("back")
		elseif d == 1 then
			print("left")
		elseif d == 2 then
			print("forward")
		else
			print("right")	
		end
	-- Facing west
	elseif party.facing == 3 then
		if d == 0 then
			print("right")
		elseif d == 1 then
			print("back")
		elseif d == 2 then
			print("left")
		else
			print("forward")
		end
	end
end
It won't be hard to call this function from a floor_switch to determine which way the party was moving when they crossed it.
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: [Technique] Trigger a function based on direction travel

Post by NutJob »

This may better explain what's happening. As for how to use, that's impossible to describe. You only need to know you need one way firing of several scripts. This is kind of like... hmm, oh! an undo party technique. ~laughs~

So if I used this:

Code: Select all

oneWayTrig(5,5,0, {0,1}, "a", "b", "c")
The color-coded dots are where each function (simply named "a", "b", "c" for brevity) would fire.

Image

Great for building a completely different trap every time the party goes into a room or a certain direction in the dungeon (from a crossroads), or building the puzzle completion code dynamically for use elsewhere (not even related to that room).
Post Reply