Page 1 of 1

[Technique] Trigger a function based on direction traveled

Posted: Fri Nov 07, 2014 9:05 pm
by NutJob
.............................

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

Posted: Sat Nov 08, 2014 2:20 am
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

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

Posted: Sat Nov 08, 2014 2:25 am
by Grimfan
Now that is nice work NutJob! I will definitely be using some variation of this myself. Code copied! :D

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

Posted: Sat Nov 08, 2014 4:14 am
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.

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

Posted: Mon Nov 10, 2014 12:26 am
by NutJob
...................

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

Posted: Mon Nov 10, 2014 1:39 am
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.

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

Posted: Mon Nov 10, 2014 2:24 am
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).