Game Development Community

Help: how to use trigger?

by Jack Cicci · in General Discussion · 08/11/2008 (7:19 am) · 9 replies

I dont understand online tutorial on trigger...

someone have some example?

thanks

#1
08/11/2008 (8:10 am)
Have you searched TDN? I find the tutorial on triggers there is fairly simple. tdn.garagegames.com/wiki/WorldBuilding/MissionEditor/Creating_Triggers

Triggers, like everything in Torque have 2 parts. The datablock, and the object. The object is each trigger in the mission, and the objects don't really do much aside from refer to their datablock. Trigger datablocks are set up once in script, contain all the info for a trigger and what each of it's methods does. Multiple trigger objects can use the same datablock.

You set up a datablock in script just like you would any other datablock. Something like:
datablock TriggerData( myNewTriggerData )
{
    tickPeriodMS = 100;
};
In here, tickPeriodMS is how many milliseconds between each "tick" of the trigger. From here you can add details to the functions of this datablock. For example onEnterTrigger() and onLeaveTrigger()
function myNewTriggerData::onEnterTrigger(%this,%trigger,%obj)
{
    //add stuff to do here

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

function myNewTriggerData::onLeaveTrigger(%this,%trigger,%obj)
{
    //add stuff to do here

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

Where %trigger is the data for the trigger object, from this you can call all of a triggers methods to do things like check how many objects are inside, or what those objects are. %obj is the object entering/leaving the trigger. And finally is the trigger tick function. This is called each time the trigger tickPeriodMS passes in time. So for myNewTriggerData, every 100ms.

function DefaultTrigger::onTickTrigger(%this,%trigger)
{
   Parent::onTickTrigger(%this,%trigger);
}
There is no %obj parameter here because it isn't caused by an object activating the method (like a player walking into the trigger), just a timed reaction. You can still use methods of the %trigger to get each object inside the trigger.

You can also use
function Trigger::AttachEffect( %Obj )
{

}
to attach things such as particleEmitters to your trigger.
#2
08/11/2008 (9:54 am)
I've tryed... but with no result... (i'm newbie in object oriented/script programming)

what is "myNewTriggerData"?

I need to reveal when MyPlayer enter in the "triggerzone"; to do this i used these statement:


function EnteringZone::onEnterTrigger(%this,%trigger,%obj)
{
echo("im in!");

Parent::onLeaveTrigger(%this,%trigger,%obj);
}
#3
08/11/2008 (10:02 am)
You have changed the "myNewTriggerData" in the function, so you have to change the name in the datablock name if you haven't changed, it should work.
#4
08/11/2008 (10:30 am)
Sorry, i made confusion...

what is "TriggerData"?

I've understood that "myNewTriggerData" is the name i've assigned...
#5
08/11/2008 (10:57 am)
When a new datablock of "TriggerData" is created, a new trigger is created in the codebase and that will be available to be placed in the mission editor. And one more thing is that, the name of the datablock TriggerData which u have specified as EnteringZone is to be used in the function name, because the function is created in the reference of the name of the TriggerData. So as your function is EnteringZone::onEnterTrigger, the name of the trigger datablock must be EnteringZone in the place of myNewTriggerData.
#6
08/11/2008 (11:24 am)
Ok... it's what i've done... but nothing happen when MyPlayer entering in the trigger zone...



i wrote, in "game.cs":

datablock t2dSceneObjectDatablock(EnteringZone)
{
tickPeriodMS = 100; // 1 Sec
};



i wrote, in "myexec.cs" (it's been loaded with exec("./myexec.cs");)

function EnteringZone::onEnterTrigger(%this,%trigger,%obj)
{
echo("i'm in");
Parent::onEnterTrigger(%this,%trigger,%obj);
}


function EnteringZone::onLeaveTrigger(%this,%trigger,%obj)
{
echo("i'm out");
Parent::onLeaveTrigger(%this,%trigger,%obj);
}


Perhaps, i put something in the wrong place...

Is right using "t2dSceneObjectDatablock()"?

Thanks (im really a newbie... please, be patient)
#7
08/11/2008 (11:45 am)
Then u are using TGB, I think it would be something different though.

I think the functions would be:

EnteringZone::onEnter(%this, %object)

and

EnteringZone::onLeave(%this, %object)

and in the datablock, enter these fields

enterCallback = true;
leaveCallback = true;

and move the datablock to your "myexec.cs" and place it before the functions, because datablock should be created before creating the functions.

try this...
#8
08/12/2008 (3:28 am)
Thank You! Solved!

Using TGB: for my purpose datablock is not necessary; enterCallback and leaveCallback are used in "levelX.t2d", where sceneobject "MyTrigger" is created and are true by default.
#9
08/12/2008 (9:23 am)
Ok... fine