Jump to content

Code for Enter and Exit events on a trigger volume


pharoseer
 Share

Recommended Posts

I was working through Jorn Theunissen's tutorials on youtube and when I got to #4 where he creates a pressure plate I wanted to toggle the texture or color of the plate to indicate that it was activated or deactivated. That was when I discovered that the ability to test for Enter and Exit events wasn't implemented yet for trigger volumes. I tinkered around a bit and come up with the following script that should handle that nicely:

 

--------------------------------------------------------------------------------
-- Author: Frank Taylor
-- Date: January 22, 2016
-- Description: Allows the detection of Enter and Exit events when applied to
-- a trigger volume.
--------------------------------------------------------------------------------

-- Toggles the states of the events allowing them to be fired only once before
-- the other event is triggered again.
Script.EnterEnabled = true
Script.ExitEnabled = false

-- This allows us to wait a period of time to ensure we've ACTUALLY left the
-- volume. If it was true/false the state would rapidly toggle. Checking if
-- this value drops below 0 is a two value decrease, meaning that
-- Script:Collision was not triggered for at least one update -- a good sign
-- that the volume is no longer occupied.
Script.EventIndex = 1

function Script:Collision(entity, position, normal, speed)
self.EventIndex = 1

if (self.EnterEnabled) then
self.EnterEnabled = false
self.ExitEnabled = true

-- Send our event
self.component:CallOutputs("Entered")

if DEBUG then
print("Volume ".. self.entity:GetKeyValue("name") .." Entered")
end
end
end

function Script:UpdateWorld()
if (self.ExitEnabled and self.EventIndex < 0) then
-- Enabled the Enter event
self.EnterEnabled = true

-- Disable the Exit event
self.ExitEnabled = false

-- Send our event
self.component:CallOutputs("Exited")

if DEBUG then
print("Volume ".. self.entity:GetKeyValue("name") .." Exited")
end
end

if (self.ExitEnabled) then
self.EventIndex = self.EventIndex - 1
end
end

 

Feel free to use it. I wouldn't mind credit, but I won't demand it either. biggrin.png The only thing I've noticed is that the print statements don't seem to appear in the output until after exiting the game window. I've only been playing with Leadwerks for a few days so I'm sure I'm missing something.

 

Cheers,

Frank

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...