Classes:Key

From ZDoom Wiki
Jump to navigation Jump to search
Note: Wait! Stop! Before you copy this actor's definition into your mod, remember the following things:
  1. You do NOT need to copy that actor, since it is already defined.
  2. In fact, it's not just useless, it will cause problems.
  3. If you want to modify it, or use a modified version, using inheritance is the way to go.
  4. The actor definitions here are put on the wiki for reference purpose only. Learn from them, don't copy them.
Key
Actor type Internal Game MiniZDoomLogoIcon.png (ZDoom)
DoomEd Number None Class Name Key


Classes: InventoryKey
 →DoomKey
 →HereticKey
 →HexenKey
 →StrifeKey

Keys are items that are used to open locked doors. The base class Key is never used directly; it serves as the base class for predefined items (like Doom's Red Keycard) or for custom keys items defined in ZScript or DECORATE.

Use in ZScript and DECORATE

Keys use some of the basic Inventory properties but it is impossible to carry more than one of each key at any time. Therefore the amount property has no effect.

If the Species property is set to an existing key, the new key will function as a duplicate for the other key, opening the same locks work locks in the same way as the old one. For example, setting the key's species to RedCard will allow it to open locks as if it were a red keycard.

The Key class doesn't define any custom properties outside of thise defined in Inventory. If you want to define entirely new keys, you must use the LOCKDEFS lump to assign them to locks.

Methods

Non-static

The Key class uses the same non-static methods as the Inventory class.

Static

  • static native clearscope bool IsLockDefined(int locknum)
Returns true if the specified number matches an existing lock.
  • static native clearscope Color GetMapColorForLock(int locknum)
Returns the color that would be used on the automap to draw the color for the specified lock number.
Could be used to draw some kind of a custom element, for example in a ZScript HUD.
  • static native clearscope Color GetMapColorForKey(Key key)
Returns the color that would be used on the automap for the specified key.
Could be used to draw some kind of a custom element, for example in a ZScript HUD.
  • static native clearscope int GetKeyTypeCount()
Returns the total number of valid keys defined for the current game. For example, for vanilla Doom will return 6 (because there are 3 keycars, 3 skull keys).
  • static native clearscope class<Key> GetKeyType(int index)
Returns a pointer to a Key class in an index. The index is a position in an internal list, where the total size of the list can be obtained with GetKeyTypeCount

Examples

This defines a new key. Note, it'll only function if a lock is defined for it in LOCKDEFS:

class SilverKey : Key
{
	Default
	{
		Inventory.PickupMessage "Picked up the silver key.";
		Inventory.Icon "SLVKA0";
	}
	States
	{
	Spawn:
		SLVK A -1;
		stop;
	}
}

Defines a new key that functions the same was as RedCard:

class GoldKey : Key
{
	Default
	{
		Inventory.PickupMessage "Picked up the gold key.";
		Inventory.Icon "GLDKA0";
		Species "RedCard";
	}
	States
	{
	Spawn:
		GLDK A -1;
		stop;
	}
}

This is an example of how keys could be draw in a ZScript HUD in a vertial line at the top right corner of the screen:

void DrawAllKeys()
{
	vector2 pos = (0,0);
	double iconSize = 8;
	int totalKeys = Key.GetKeyTypeCount();
	for (int i = 0; i < totalKeys; i++)
	{
		Key k = Key(CPlayer.mo.FindInventory(Key.GetKeyType(i)));
		if (k)
		{
			DrawInventoryIcon(k, pos, DI_SCREEN_RIGHT_TOP|DI_ITEM_RIGHT_TOP, boxsize:(iconSize, iconSize));
			pos.y += iconsize;
		}
	}
}

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 Key : Inventory
{
	Default
	{
		+DONTGIB;		// Don't disappear due to a crusher
		Inventory.InterHubAmount 0;
		Inventory.PickupSound "misc/k_pkup";
	}

	static native clearscope bool IsLockDefined(int locknum);
	static native clearscope Color GetMapColorForLock(int locknum);
	static native clearscope Color GetMapColorForKey(Key key);
	static native clearscope int GetKeyTypeCount();
	static native clearscope class<Key> GetKeyType(int index);
	
	override bool HandlePickup (Inventory item)
	{
		// In single player, you can pick up an infinite number of keys
		// even though you can only hold one of each.
		if (multiplayer)
		{
			return Super.HandlePickup (item);
		}
		if (GetClass() == item.GetClass())
		{
			item.bPickupGood = true;
			return true;
		}
		return false;
	}

	override bool ShouldStay ()
	{
		return !!multiplayer;
	}
}

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 Key : Inventory native
{
  +DONTGIB
  +INVENTORY.INTERHUBSTRIP
  Inventory.PickupSound "misc/k_pkup"
}