Game Development Community

Trigger Code

by Nick Voloro · in General Discussion · 05/01/2008 (5:59 pm) · 10 replies

Hello, I was wondering if anyone knew how to make it so after the player hits the trigger one time it dissapears. Any help or push in the right direction would be great. Thanks.

#1
05/02/2008 (11:52 am)
Check the trigger script functions OnEnter or OnLeave. Or you can check the collision function for it as well. Depends on what you're hitting it with, but either way, that's where I would put my "disappear trigger" code.
#2
05/02/2008 (3:14 pm)
It's been my experience that when you delete a trigger while an object is within Torque will crash.
#3
05/02/2008 (7:20 pm)
Ok well I found a way to delete it by on leave function using %delete.trigger();
However the only problem is that when I reload the mission it is not there. Have any ideas?
Or an alternative.
#4
05/08/2008 (6:34 am)
If you use the delete function to remove the trigger and then save the mission you will lose the trigger. If you want to change the trigger in any way in game (like delete or move) by script, you'll have to make sure not to delete it and save your mission in the same session.

There is a way to get around this though, you can just move your trigger to some place the player can't reach it (like underground) instead of deleting it. Then when the mission loads, you can ask if the trigger is below the terrain level and if so you can fix it then.
#5
05/08/2008 (7:27 am)
As Jason said above, relocating the trigger in the height axis (out of the player's reach) is also how a lot of the big budget games turn triggers on and off (I'm thinking from my Call of Duty modding days here).

It shouldn't be too difficult to write a little script for a function that when activated checks the trigger's position and then moves it 100 units up/down the z axis - thus effectively disabling the trigger.

Would be less time consuming that manually writing out the new location everytime you want to disable a trigger.
#6
05/08/2008 (8:33 am)
It might be easier to add a variable to the trigger that equals 0 and when a player enters the first time it changes to a 1. Have the onEnter check what the variable equals and if it equals 1 then just do nothing. Simple but effective.
#7
05/08/2008 (8:52 am)
I should have some "trigger once" code somewhere at home. I'll post it later.

[EDIT]My changes affect the engine and I think I'm not allowed to post them in the public forums. Looks like you don't own the engine so I'm sorry, I'm afraid I can't help you.[/EDIT]
#8
05/08/2008 (10:07 am)
Actually, after a little test I realise that you'd have to make a function to inform the server that the trigger has been moved, only moving it on the clientside won't disable the trigger.

So here's a really easy way to enable/disable triggers without scripting CommandToServer functions.

1. Give all your triggers (or at least those you'll want to de-activate) the dynamic field disable and set the default value 0. Now all of your triggers have the disable function set to zero/off.

2.In your trigger scripts, alter OnEnterTrigger, OnLeaveTrigger and OnTickTrigger to read something like this:
function yourtrigger::onEnterTrigger(%this,%trigger,%obj)
{

if(%trigger.disable == 0)
{
echo("Trigger is ON")
//do your trigger stuff here
}
else
{
echo("Trigger is Disabled");
//trigger stuff isn't going to get done
}

   Parent::onEnterTrigger(%this,%trigger,%obj);
}

When the player/AI/whoever enters the trigger it fires because disable is set to 0.

3. Set the trigger's disable field to 1.
%trigger.disable=1;

Depending on when you want the trigger to be disabled depends on where you put the script. You could put it in the OnLeaveTrigger function, so when the player exits the trigger it won't work again.

Now Nick originally asked about entering a trigger and it only works once, so this bit of code should be fine.
The trigger checks whether it is disabled on the player entering, and then disables when he exits.
It's important to remember that triggers work with any entity, not just players, so here's the code for that too.

//in trigger section

function yourtrigger::onEnterTrigger(%this,%trigger,%obj)
{

//first check that the entity is a player and not an AI
%checkclass = %obj.getclassname();
   if(%checkclass $= "player")
	{

//check trigger status
if(%trigger.disable == 0)
{

echo("PLAYER in trigger");
//Put you triggered event here

	}
	else
	{

echo("DISABLED TRIGGER");

	}
}
else
{

echo("Not player in trigger - must be AI -ignore");

}

   Parent::onEnterTrigger(%this,%trigger,%obj);
}

//exit trigger section
function yourtrigger::onLeaveTrigger(%this,%trigger,%obj)
{

//check it's the player, ignore AI
%checkclass = %obj.getclassname();
   if(%checkclass $= "player")
	{

//check trigger status
if(%trigger.disable == 0)
{

echo("PLAYER leaving trigally2");
%trigger.disable = 1;

	}
	else
	{

echo("Trigger already disabled");

	}
}
else
{

//echo("Not player in trigger - must be AI -ignore");

}
   Parent::onLeaveTrigger(%this,%trigger,%obj);
}

And that should work.

It's important to remember that you need the code example from section 2 above on any of the trigger functions (OnEnterTrigger, OnLeaveTrigger and OnTickTrigger) that you have scripted events for. If you have not scripted an event for that function then you can leave it blank.
#9
12/04/2008 (10:08 am)
Awesome. Is there any way to do this same thing with Physical Zone? I know that I can create a dynamic field in Physical zones. But can I control them through script? I guess much like this example,

What I would like to do is move the physical zone "down" when the player enters a trigger. That way it will suck the player "up" once he is inside the physical zone.
#10
12/06/2008 (9:58 am)
I found this little nugget in the TDN but it doesn't mention how to use it.

AttachEffect

This allows you to attach an effect such as particle or physical zones.

function Trigger::AttachEffect( %Obj )
{

}


So my question is how can you use AttachEffect with a preexisting Trigger?

Do you have to get a handle of a physicalzone that you want to use?