A_SelectWeapon

From ZDoom Wiki
Jump to: navigation, search

bool A_SelectWeapon (string type [, int flags])

Usage

Selects the specified weapon type as the calling player's current weapon.

Parameters

  • type: the class name of the weapon to switch to. Unless the SWF_SELECTPRIORITY flag is passed, this has to be a valid weapon and the weapon must be present in the player's inventory for the switch to happen. The function will be carried out whether or not the weapon to switch to has ammo.
  • flags: Default is 0.
    • SWF_SELECTPRIORITY — if the specified weapon is either invalid or does not exist in the inventory and this flag is set, the function switches to the highest priority weapon the player has, in the same vein as running out of ammo.


This function can only be called from weapons or custom inventory items.

Return value

The function returns true if the weapon switch happens, otherwise it returns false.

Examples

This new Berserk powerup uses A_SelectWeapon to change to a new unarmed attack which replaces the fist.

ACTOR NewBerserk : Berserk replaces Berserk
{
  States
  {
  Pickup:
     TNT1 A 0 A_GiveInventory("PowerStrength") 
     TNT1 A 0 HealThing(100)
     TNT1 A 0 A_SelectWeapon("KoolFist")
     stop
  } 
}


This rather odd item, when picked up, tries to switch the player's weapon to the BFG9000. If the weapon exists in the player's inventory, the switch happens and the player receives 40 points of energy cells. If, however, the switch fails, i.e the player does not have the weapon, the player gets damage by half of his or her health.

ACTOR OddArtifact : Megasphere
{
  Inventory.PickupMessage "Oddsphere!"
  States
  {
  Pickup:
    TNT1 A 0
    { // See anonymous functions.
      If(A_SelectWeapon("BFG9000"))
      {
        A_GiveInventory("Cell", 40);
      }
      Else
      {
        A_DamageSelf(health / 2);
      }
    }
    Stop
  }
}