Thursday, February 16, 2012

Weapon Class (UnrealScript)

The Weapon class is a subclass of Inventory. The Weapon System in UDK is very flexible for creating different types of weapons or usable items. The image below shows the existing subclasses of Weapon in UDK:


Each weapon can have multiple firing modes. The UDK uses two types: primary, which is fired with the left mouse button, and the secondary, which is fired with the right mouse button. However, other firing modes can be created. For each firing mode is necessary to define the fire type. The UDK already has defined two fire types, the Instant Fire that is used for very fast weapons such as a Sniper Rifle. In this fire type, the bullet is not created. What happens at the time of the shooting is that a trace is done representing the path of the bullet to find any obstacle collision immediately. The other fire type is the Projectile Fire. When the weapon is fired in this fire type, an object of Projectile class is created with the information of speed and direction. The Projectile class is responsible for the collision checking.

The fire types are defined in the Weapon class in an Enumeration (enum). An enum is very useful to limit the possible values ​​that a variable can have. A dynamic Array is used to define the fire type for each firing mode. Each position in the Array represents a firing mode. The index of an Array starts in zero, so the information of the primary firing mode is in the index zero and the information of the secondary firing mode is in the index one. The following code shows the declaration of the enum and the Array.
// Extract from Weapon class (Engine package)

enum EWeaponFireType
{
    EWFT_InstantHit,
    EWFT_Projectile,
    EWFT_Custom,
    EWFT_None
};

var Array<EWeaponFireType>  WeaponFireTypes;

A good weapon example in UDK is the Link Gun. This is the default weapon that the player gets when runs a game in UDK as UTGame. The Link Gun's primary firing mode is of type EWFT_Projectile that launches fast plasmas projectiles. The secondary firing mode is of type EWFT_InstantHit that fires a constant plasma beam as shown below.


The class that defines the Link Gun weapon is UTWeap_LinkGun. In the defaultproperties block are defined the fire type and the class that represents the projectile for the primary firing mode as seen in the code below.
//Extract from UTWeap_LinkGun class (UTGame package)

defaultproperties
{
  //...

  WeaponFireTypes(0)=EWFT_Projectile
  WeaponProjectiles(0)=class'UTProj_LinkPlasma'

  //...
}

The function of the Weapon class responsible for firing weapons of type EWFT_InstantHit is InstantFire(). The function used in the type EWFT_Projectile to fire projectiles is ProjectileFire(). Below is a sample code of the ProjectileFire() function in the UTWeapon class.
//Extract from UTWeapon class (UTGame package)

simulated function Projectile ProjectileFire()
{
    local vector        RealStartLoc;
    local Projectile    SpawnedProjectile;

    // tell remote clients that we fired, to trigger effects
    IncrementFlashCount();

    if( Role == ROLE_Authority )
    {
        // this is the location where the projectile is spawned.
        RealStartLoc = GetPhysicalFireStartLoc();

        // Spawn projectile
        SpawnedProjectile = Spawn(GetProjectileClass(),,, RealStartLoc);

        if( SpawnedProjectile != None && !SpawnedProjectile.bDeleteMe )
        {
            SpawnedProjectile.Init( Vector(GetAdjustedAim( RealStartLoc )) );
        }

        // Return it up the line
        return SpawnedProjectile;
    }

    return None;
}

For more information about the Weapon class:
Weapon System Technical Guide