Battlezone-style weapon changes in Unreal Engine 3

In a future post, I will be providing a short tutorial on providing Battlezone-style weapon pickups for vehicles in games built using Unreal Engine 3. The existing code for vehicles in Unreal Engine 3 assumes a single weapon per vehicle “seat”, and that behaviour is not ideal for the tutorial. I have previously built a simple vehicle that can toggle between multiple weapons in the same seat, as an implementation example for the Battlezone 3 project. For reference, I’ve provided a slightly modified version of that example class here:

class WeaponChangeGoliath extends UTVehicle_Goliath_Content;

var int toggleWeaponIndex;
var array< class<UTVehicleWeapon> > toggleWeapons;

exec function toggleWeapon() {
  toggleWeaponIndex++;
  if (toggleWeaponIndex >= toggleWeapons.length)
    toggleWeaponIndex -= toggleWeapons.length;

  changeWeapon(0, toggleWeapons[toggleWeaponIndex]);
}

function changeWeapon(int seatNum, class<UTVehicleWeapon> newGunClass) {
  if (seats[seatNum].gun != none) {
    // Might be overkill on the "stop firing" stuff here.
    if (seatNum == 0)
      stopFiringWeapon();
    seats[seatNum].gun.endFire(0);
    seats[seatNum].gun.endFire(1);
    seats[seatNum].gun.forceEndFire();
    seats[seatNum].gun.destroy();
  }

  seats[seatNum].GunClass = newGunClass;
  seats[seatNum].Gun = UTVehicleWeapon(InvManager.CreateInventory(seats[seatNum].GunClass));
  seats[seatNum].Gun.SetBase(self);

  if (seats[seatNum].gun != none) {
    seats[seatNum].gun.SeatIndex = seatNum;
    seats[seatNum].gun.MyVehicle = self;
    if (seatNum == 0) {
      weapon = seats[seatNum].gun;
      invManager.setCurrentWeapon(seats[0].gun);
    }
  }
}

defaultproperties
{
  toggleWeaponIndex=0
  toggleWeapons(0)=class'UTVWeap_GoliathTurret';
  toggleWeapons(1)=class'UTVWeap_GoliathMachineGun';
  toggleWeapons(2)=class'UTVWeap_MantaGun';
}

The toggleWeapons array contains the classes of all of the weapons that we wish to toggle between. The changeWeapon function does all of the real work of changing the weapon, while the toggleWeapon exec function allows us to change the weapon from the console. toggleWeapon could also be bound to a key. If the vehicle has only one seat, and you do not need to use the number keys for anything else, you can use them to change the weapons instead. The number keys (bound to switching weapons) run through the switchWeapon method on the player’s currently controlled Pawn. We can override that method to select the weapons:

simulated function switchWeapon(byte newGroup) {
  local int newWeaponIndex;

  newWeaponIndex = newGroup-1;
  if (newWeaponIndex >= 0 && newWeaponIndex < toggleWeapons.length && newWeaponIndex != toggleWeaponIndex) {
    toggleWeaponIndex = newWeaponIndex;
    changeWeapon(0, toggleWeapons[newWeaponIndex]);
  }
}

One quick note: The switchWeapon method has not been tested in multiplayer, and probably does not work there as-is. Since it is just a convenience, and not relevant to the coming tutorial, I have not designed it for multiplayer support.

This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Battlezone-style weapon changes in Unreal Engine 3

  1. Pingback: Battlezone-style weapon pickups in Unreal Engine 3 | Brendon Robinson's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s