Triggers


Right off the bat, there is potential for confusion when discussing the term trigger in Torque, so let's get that out of the way. There are four kinds of triggers that people talk about when programming with Torque:

  • area triggers

  • animation triggers

  • weapon state triggers

  • player event control triggers

I'll introduce you to all four here but we'll talk about three of them—area triggers, animation triggers, and weapon state triggers—in more detail in future chapters.

Area Triggers

Area triggers are a special in-game construct.An area in the 3D world of a game is defined as a trigger object. When a player's avatar enters the bounds of the trigger area, an event message is posted on the server. We can write handlers to be activated by these messages. We will be covering area triggers in more depth in Chapter 22.

Animation Triggers

Animation triggers are used to synchronize footstep sounds with walking animation in player models. Modeling tools that support animation triggers have ways of tagging frames of animation sequences. The tags tell the game engine that certain things should happen when this frame of an animation is being displayed. We'll discuss these later in Chapter 20.

Weapon State Triggers

Torque uses weapon state triggers for managing and manipulating weapon states. These triggers dictate what to do when a weapon is firing, reloading, recoiling, and so on. We'll look at this in more detail later in Chapter 20 in the section "Weapon Sounds".

Player Event Control Triggers

Finally, there are player event control triggers, which are a form of indirect messaging of interest to us in this chapter. These mechanisms are used to process certain player inputs on the client in real time. You can have up to six of these triggers, each held by a variable with the prefix $mvTriggerCountn (where n is an index number from 0 to 5).

When we use a trigger move event, we increment the appropriate $mvTriggerCountn variable on the client side. This change in value causes an update message back to the server. The server will process these changes in the context of our control object, which is usually our player's avatar. After the server acts on the trigger, it decrements its count. If the count is nonzero, it acts again when it gets the next change in its internal scheduling algorithm. In this way we can initiate these trigger events by incrementing the variable as much as we want (up to a maximum of 255 times), without having to wait and see if the server has acted on the events. They are just automatically queued up for us via the $mvTriggerCountn variable mechanism.

Torque has default support for the first four control triggers built into its player and vehicle classes (see Table 6.1).

Table 6.1: Default Player Event Control Triggers

Trigger

Default Action

$mvTriggerCount0

Shoots or activates the mounted weapon in image slot 0 of the player's avatar. (The "fire" button, so to speak.)

$mvTriggerCount1

Shoots or activates the mounted weapon in image slot 1 of the player's avatar. (The "alt fire".)

$mvTriggerCount2

Initiates the "jump" action and animation for the player's avatar.

$mvTriggerCount3

Initiates the "jetting" (extra boost) action and animation for the vehicle on which a player's avatar is mounted.

$mvTriggerCount4

Unassigned.

$mvTriggerCount5

Unassigned.

In the server control code, we can put a trigger handler in our player's avatar for any of these triggers that override the default action. We define a trigger handler like this:

 function MyAvatarClass::onTrigger(%this, %obj, %triggerNum, %val) {    // trigger activity here   $switch(%triggerNum)   {     case 0:       //replacement for the "fire" action.     case 1:       //replacement for the "alt fire" action.     case 2:       //replacement for the "jump" action.     case 3:       //replacement for the "jetting" action.     case 4:       //whatever you like     case 5:       //whatever you like   } } 

The MyAvatarClass class is whatever you have defined in your player avatar's datablock using the following statement:

    className = MyAvatarClass; 

To use these handlers, you merely have to increment one of the player event control triggers on the client, something like this:

 function mouseFire(%val) {    $mvTriggerCount0++; } 




3D Game Programming All in One
3D Game Programming All in One (Course Technology PTR Game Development Series)
ISBN: 159200136X
EAN: 2147483647
Year: 2006
Pages: 197

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net