GetPowerupIcon

From ZDoom Wiki
Jump to navigation Jump to search

Powerup

virtual clearscope TextureID GetPowerupIcon() const

Usage

A virtual function of the Powerup class to obtain their Inventory.Icon. Can be overridden to add conditions.

Normally, used by HUDs to draw an icon for the active powerup. Doom powerups don't have icons, but the ones in Heretic and Hexen do.

Return values

Returns a TextureID of the icon.

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.

The base Powerup class defines it as follows:

	virtual clearscope version("2.5") TextureID GetPowerupIcon() const
	{
		return Icon;
	}

Examples

This example shows how a custom version of the base Powerup class can be created with a more robust handling of how it would obtain an icon. First, it tries to obtain Inventory.Icon defined in the powerup itself. If that failed, it looks for Inventory.AltHUDIcon. From there, it utilizes its new property, GiverClass. This is meant to be a PowerupGiver class name, filled in by the author, to be recognized as the powerup giver that is "responsible" for this powerup. The function will use GetDefaultByType to try and obtain its icon, altHUDicon, or Spawn state sprite with GetSpriteTexture:

class Powerup_Custombase : Powerup abstract
{
	class<PowerupGiver> giverClass;
	Property GiverClass : giverClass;

	// A custom function that returns true if the provided
	// TextureID is a valid, existing texture and is NOT
	// just TNT1A0:
	clearscope bool IsIconValid(TextureID check)
	{
		return check && check.IsValid() && TexMan.GetName(check) != 'TNT1A0';
	}

	override TextureID GetPowerupIcon()
	{
		// Try obtaining the icon first:
		if (IsIconValid(icon))
		{
			return icon;
		}
		// If that failed, try obtaining altHUDIcon:
		if (IsIconValid(altHUDIcon))
		{
			return altHUDIcon;
		}

		// Declare a new field:
		TextureID picon;

		// Null-check giverClass
		if (giverClass)
		{
			let def = GetDefaultByType(giverclass);
			// ...and try obtaining its icon:
			picon = def.icon;
			if (IsIconValid(picon))
			{
				return picon;
			}
			// If that failed, try obtaining giverClass's altHUDIcon:
			picon = def.altHUDIcon;
			if (IsIconValid(picon))
			{
				return picon;
			}
			// If that failed, try obtaining giverClass's Spawn state sprite:
			State sps = def.spawnState;
			picon = sps.GetSpriteTexture(0);
			if (IsIconValid(picon))
			{
				return picon;
			}
		}
		// If the code reached this place, none of the above worked, the
		// function failed to obtain a valid texture. So, explicitly set
		// picon to null, so that future IsValid checks will recognize
		// this pointer as null:
		picon.SetNull();
		return picon;
	}
}

// Example implementation:
class MyPowerup : Powerup_Custombase
{
	Default
	{
		// Defines Doom's Invulnerability sphere as the giver class
		// for this powerup. As such, if this isn't given an icon,
		// it'll return PINVA0, which is the spawn state sprite of
		// InvulnerabilitySphere (because it doesn't have icons):
		Powerup_Custombase.GiverClass 'InvulnerabilitySphere';
	}
}

See also