Game Development Community

Weapon *states

by Anthony C · in Torque Game Engine · 05/19/2007 (12:36 pm) · 2 replies

I'm looking at the weapon script for the emaga5, and the animations are throwing me off some.

I understand that "states" dictate the action, but i dont see anywhere in the code where the *state is called, assigned, retrieved, or defined. For example, how does code know that the gun is in the stateTransitionOnAmmo[5] state?

The one i did understand was stateScript[3] = "OnFire" which is defined as a local function in the cs file

Quote:// Images have a state system which controls how the animations
// are run, which sounds are played, script callbacks, etc. This
// state system is downloaded to the client so that clients can
// predict state changes and animate accordingly. The following
// system supports basic ready->fire->reload transitions as
// well as a no-ammo->dryfire idle state.

// Initial start up state
stateName[0] = "Preactivate";
stateTransitionOnLoaded[0] = "Activate";
stateTransitionOnNoAmmo[0] = "NoAmmo";

// Activating the gun. Called when the weapon is first
// mounted and there is ammo.
stateName[1] = "Activate";
stateTransitionOnTimeout[1] = "Ready";
stateTimeoutValue[1] = 0.6;
stateSequence[1] = "Activate";

// Ready to fire, just waiting for the trigger
stateName[2] = "Ready";
stateTransitionOnNoAmmo[2] = "NoAmmo";
stateTransitionOnTriggerDown[2] = "Fire";

// Fire the weapon. Calls the fire script which does
// the actual work.
stateName[3] = "Fire";
stateTransitionOnTimeout[3] = "Reload";
stateTimeoutValue[3] = 0.2;
stateFire[3] = true;
stateRecoil[3] = LightRecoil;
stateAllowImageChange[3] = false;
stateSequence[3] = "Fire";
stateScript[3] = "onFire";
// stateSound[3] = CrossbowFireSound;

// Play the relead animation, and transition into
stateName[4] = "Reload";
stateTransitionOnNoAmmo[4] = "NoAmmo";
stateTransitionOnTimeout[4] = "Ready";
stateTimeoutValue[4] = 0.8;
stateAllowImageChange[4] = false;
stateSequence[4] = "Reload";
stateEjectShell[4] = true;
// stateSound[4] = CrossbowReloadSound;

// No ammo in the weapon, just idle until something
// shows up. Play the dry fire sound if the trigger is
// pulled.
stateName[5] = "NoAmmo";
stateTransitionOnAmmo[5] = "Reload";
stateSequence[5] = "NoAmmo";
stateTransitionOnTriggerDown[5] = "DryFire";

// No ammo dry fire
stateName[6] = "DryFire";
stateTimeoutValue[6] = 1.0;
stateTransitionOnTimeout[6] = "NoAmmo";
// stateSound[6] = CrossbowFireEmptySound;

#1
05/19/2007 (8:26 pm)
I think you have things mixed up.

The "stateName[3] = "Fire";" state is actually calling the "onFire" function and not the other way around.
stateScript[3] = "onFire";
That line there is what calls the function. You can call a function in any state that you like.

The state system is defined in source code, not script. The engine determines what state the weapon is in pending on a set of conditions. E.g. you ask how the engine knows to go to state 5, well in some key states (0, 2, 4, 6) you can see one of these lines:
stateTransitionOnNoAmmo[X] = "NoAmmo";
stateTransitionOnTimeout[X] = "NoAmmo";
That tells the engine that if the weapon is in that given state and runs out of ammo, jump to the "NoAmmo" state.

The states don't necessarily run in sequential order and there are many different ways to use them.
#2
05/20/2007 (8:36 am)
Take a look at the weapon script file, weapon.cs file, and the inventory.cs file. the onFire() function starts a series of functions which eventually calls %obj.setImageAmmo(%slot,%bool). This is an engine function which determines the ammo state of the image at %slot based on %bool.