Classes:Health

From ZDoom Wiki
Jump to navigation Jump to search
Note: Wait! Stop! You do not need to copy this actor's code into your project! Here's why:
  1. This actor is already defined in GZDoom, there's no reason to define it again.
  2. In fact, trying to define an actor with the same name will cause an error (because it already exists).
  3. If you want to make your own version of this actor, use inheritance.
  4. Definitions for existing actors are put on the wiki for reference purpose only.
Health
Actor type Internal Game MiniZDoomLogoIcon.png (ZDoom)
DoomEd Number None Class Name Health


Classes: InventoryHealth
 →CrystalVial
 →HealthBonus
 →Medikit
 →MegasphereHealth
 →Soulsphere
 →Stimpack

A Health item adds a certain amount to the player's health points. Items of this type are always effective when picked up. They cannot be placed in the inventory; to have health items in inventory, use HealthPickup. Health is never used directly. This class is only used as a base class for predefined items (like Doom's Stimpack or for items defined in ZScript/DECORATE.

Note: The Health class is a type of item that can be picked up. This is NOT the same entity as an actor's current health—that one is an Actor class variable field, NOT an item.


Using in ZScript and DECORATE

Health items support all the basic Inventory properties. However, they use a few of them differently:

  • Inventory.Amount value
Sets the amount of health this item gives when picked up.
  • Inventory.MaxAmount value
Sets the maximum amount of health you can get with this item. If this is greater than 0, the maximum health points gain is added to this to determine the final maximum amount.

In addition they define one new property:

  • Health.LowMessage value, message
When pickupper's health is lower than value, the pickup message is set to message.

Examples

class Whiskey : Health
{
  Default
  {
    Inventory.PickupMessage "You drank some booze.";  // This is an example. It's recommended to use LANGUAGE for player-facing strings.
    Inventory.Amount 5;
    Inventory.MaxAmount 200;
    +COUNTITEM //means that the item counts toward item percentage
  }
  States
  {
  Spawn:
    RWHI A -1;
    Stop;
  }
}


class DogFood : Health 10575
{
  Default
  {
    Inventory.PickupMessage "Ate some dog food. Woof!";  // This is an example. It's recommended to use LANGUAGE for player-facing strings.
    Inventory.PickupSound "dog/sight";
    Inventory.Amount 4;
    Inventory.MaxAmount 200;
    +COUNTITEM //means that the item counts toward item percentage
    +INVENTORY.ALWAYSPICKUP //means the item will be always picked up, even if the actor is already at max health
  }
  States
  {
  Spawn:
    AWI1 A -1;
    Stop;
  }
}

ZScript definition

Note: The ZScript definition below is for reference and may be different in the current version of GZDoom.The most up-to-date version of this code can be found on GZDoom GitHub.
class Health : Inventory
{
	transient int PrevHealth;
	meta int LowHealth;
	meta String LowHealthMessage;
	
	property LowMessage: LowHealth, LowHealthMessage;
	
	Default
	{
		+INVENTORY.ISHEALTH
		Inventory.Amount 1;
		Inventory.MaxAmount 0;
		Inventory.PickupSound "misc/health_pkup";
	}
	
	//===========================================================================
	//
	// AHealth :: PickupMessage
	//
	//===========================================================================
	override String PickupMessage ()
	{
		if (PrevHealth < LowHealth)
		{
			String message = LowHealthMessage;
			if (message.Length() != 0)
			{
				return message;
			}
		}
		return Super.PickupMessage();
	}

	//===========================================================================
	//
	// TryPickup
	//
	//===========================================================================

	override bool TryPickup (in out Actor other)
	{
		PrevHealth = other.player != NULL ? other.player.health : other.health;

		// P_GiveBody adds one new feature, applied only if it is possible to pick up negative health:
		// Negative values are treated as positive percentages, ie Amount -100 means 100% health, ignoring max amount.
		if (other.GiveBody(Amount, MaxAmount))
		{
			GoAwayAndDie();
			return true;
		}
		return false;
	}
}

DECORATE definition

Note: This is legacy code, kept for archival purposes only. DECORATE is deprecated in GZDoom and is completely superseded by ZScript. GZDoom internally uses the ZScript definition above.
ACTOR Health : Inventory native
{
  Inventory.Amount 1
  Inventory.MaxAmount 0
  Inventory.PickupSound "misc/health_pkup"
}