WorldBuilding/MissionEditor/Creating Triggers

From TDN

Contents

Introduction

Triggers are areas of the mission which will act to create a reaction once activated. This is typically used for doors, checkpoints, etc.

This section has not been fully detailed, but will be at some stage soon. The best example of triggers are the checkpoints in the Racing demo, also included here below.

Setup


Here are the typical trigger functions.

DefaultTrigger


DefaultTrigger is used by the mission editor. This is also an example of trigger methods and callbacks. It derives from GameBaseData and so has the standard category and className fields. Beyond those, TriggerData defines only one new field.

The period is value is used to control how often the console onTriggerTick callback is called while there are any objects in the trigger. The default value is 100 MS.

datablock TriggerData(DefaultTrigger)
{
   category     = "DefaultTrigger";
   tickPeriodMS = 100; // Every 10th of a second
};

OnEnterTrigger


This method is called whenever an object enters the %trigger area, the object is passed as %obj. The default onEnterTrigger method (in the C++ code) invokes the ::onTrigger(%trigger,1) method on every object (whatever it's type) in the same group as the trigger.

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

OnLeaveTrigger


This method is called whenever an object leaves the %trigger area, the object is passed as %obj. The default onLeaveTrigger method (in the C++ code) invokes the ::onTrigger(%trigger,0) method on every object (whatever it's type) in the same group as the trigger.

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

onTickTrigger


This method is called every tickPerioMS, as long as any objects intersect the trigger. The default onTriggerTick method (in the C++ code) invokes the ::onTriggerTick(%trigger) method on every object (whatever it's type) in the same group as the trigger.

You can iterate through the objects in the list by using these methods:
%trigger.getNumObjects();
%trigger.getObject(n);

function DefaultTrigger::onTickTrigger(%this,%trigger)
{
   Parent::onTickTrigger(%this,%trigger);
}

AttachEffect


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

function Trigger::AttachEffect( %Obj )
{

}

Game Usage

Polyhedron

When creating a Trigger (or trigger derived objects like PhysicalZones) you have the opportunity to set it's polyhedron. The polyhedron defines the shape of the trigger. It's usually defined like so...

new Trigger(MyTrigger) {
      position = "-158.282 968.511 195.767";
      rotation = "1 0 0 0";
      scale = "1 1 1";
      polyhedron = "0.0000000 0.0000000 0.0000000
                             1.0000000 0.0000000 0.0000000
                             0.0000000 -1.0000000 0.0000000
                             0.0000000 0.0000000 1.0000000";
};

The polyhedron entry consists of a corner point and the 3 vectors which extend from it. While you can change this value to size the trigger, the same can be done by setting the scale of the trigger.

Checkpoints

//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

datablock TriggerData(CheckPointTrigger)
{
   // The period is value is used to control how often the console
   // onTriggerTick callback is called while there are any objects
   // in the trigger.  The default value is 100 MS.
   tickPeriodMS = 100; // 1 Sec
};

//-----------------------------------------------------------------------------

function CheckPointTrigger::onEnterTrigger(%this,%trigger,%obj)
{
	Parent::onEnterTrigger(%this,%trigger,%obj);

	if(%obj.client.nextCheck == %trigger.checkpoint)
	{
		if(%trigger.isLast)
		{
			// Player has completed a lap.
			%obj.client.lap++;

			if(%obj.client.lap >= $Game::Laps)
			{
				// Increase his score by 1.
				%obj.client.incScore(1);
            	// End the game
             	cycleGame();			
			}
			else {
				%obj.client.nextCheck = 1;
				commandToClient(%obj.client, 'IncreaseLapCounter');
			}
		}
		else {
			// Continue to the next one.
			%obj.client.nextCheck++;
		}
	}
	else
		centerPrint(%obj.client, "Wrong Checkpoint!", 1, 1);
}