CountInv

From ZDoom Wiki
Jump to navigation Jump to search

Actor

clearscope int CountInv(class<Inventory> itemtype, int ptr_select = AAPTR_DEFAULT)

Usage

Returns the amount of the specified Inventory item the calling actor has.

Parameters

  • class<Inventory> itemtype
The Inventory item class to check for. Note, this has to be a valid existing class based on Inventory.
  • int ptr_select
The DECORATE actor pointer. Points to the actor whose inventory list should be checked. The default value is AAPTR_DEFAULT which is the calling actor.
In ZScript this argument is generally moot, since instead you can directly call this function on a ZScript actor pointer.

Return value

  • int — returns the exact count of itemtype the actor has.

Examples

ZScript

This will print out the number of shells the calling actor has:

Console.Printf("You have %d shells", self.CountInv('Shell'));

This shows an example of how a custom version of A_WeaponReady could be created, so it allows calling the User1 Weapon state if the calling player has enough GrenadeAmmo (this is an example name, not an existing GZDoom class):

// This assumes that the weapon has a User1 state sequence
// that throws a grenade.
action void A_CustomWeaponReady(int flags = WRF_ALLOWUSER1) //the allow flag is passed by default
{
	if (CountInv('GrenadeAmmo') == 0)
	{
		flags &= ~WRF_ALLOWUSER1; //if not enough grenades, the flag is removed
	}
	A_WeaponReady(flags);
}

DECORATE (deprecated)

This monster strafes randomly left or right.

 <...>
 Strafe:
   TNT1 A 0 A_Jump( 128, "See" )                    // Chance to fail.
   TNT1 A 0 A_TakeInventory( "NmAngle", 0 )
   TNT1 A 0 A_TakeInventory( "NmForce", 0 )
   TNT1 A 0 A_GiveInventory( "NmAngle", RandomPick( 
                   80, 90, 100, 110, 120, 135,      // Left (in degrees).
                   225, 240, 250, 260, 270, 280     // Right (in degrees).
                   ) )
   NGHT A 8 A_GiveInventory( "NmForce", Random( 5, 10 ) )
   NGHT A 0 ThrustThing( ( Angle + CountInv( "NmAngle" ) ) * 256/360, CountInv( "NmForce" ) )
   NGHT A 0 ThrustThingZ( 0, Random( 40, 80 ), 0, 0 )
   Goto See