Use

From ZDoom Wiki
Jump to navigation Jump to search

Inventory

virtual void Use(bool pickup)

Usage

A virtual function called by Inventory items when they're used. Items can be used as follows:

  • The player selects an item in the inventory bar and presses the "use item" button. (The item needs the INVENTORY.INVBAR flag set to be visible in the inventory bar.)
  • The player uses the use <itemname> command where <itemname> is the Inventory item class.
  • Use() is called automatically on items that have the INVENTORY.AUTOACTIVATE flag (this happens in the item's TryPickup call).
  • Use() can be called directly on an item.

It's important to note that by default items don't utilize this function; most items are just picked up and placed in the actor's inventory, without being "used" in any way besides that.

Parameters

  • bool pickup
Is true if the item was used automatically when it was picked up. Can be checked to apply different effects when item is used from inventory or on pickup.

Return values

Returns a boolean value. If the function returns true the item is consumed: its amount is reduced by 1, and the item is removed if the amount is set to 0 (unless the item has the INVENTORY.KEEPDEPLETED flag).

Note: if the item's Amount property is set to 0 but it has the INVENTORY.AUTOACTIVATE flag set, it will still call Use() when received.

Examples

This virtual function is a convenient choice when you want to have an item apply an effect after it's been picked up but don't want to recreate the various checks for whether an item can be picked up (which is something you'd have to do in a TryPickup override).

This is a variation of the Doom Backpack that also restores the player's health up to the default maximum (100) with GiveBody when it's picked up:

class HealingBackpack : Backpack
{
	Default
	{
		+Inventory.AUTOACTIVATE
	}

	override bool Use(bool pickup)
	{
		// bDepleted will be set to true if the backpack
		// was dropped with the 'drop' command. Check that
		// it's false, so that the player can't heal
		// themselves by repeatedly dropping and picking
		// up this backpack:
		if (!bDepleted && owner)
		{
			owner.GiveBody(100);
		}
		// We don't want backpack to be consumed:
		return false;
	}
}

See also