GetAtkState

From ZDoom Wiki
Jump to navigation Jump to search

Weapon

virtual State GetAtkState (bool hold)

Usage

A virtual function used by the Weapon class. Returns the label of the state sequence that should be used when the player presses the attack button. By default returns either Fire or Hold based on the conditions. (See Weapon states).

Examples

This weapon has a singleShotMode boolean that is flipped when pressing the secondary attack button. For this purpose, A_MyWeaponReady is supposed to be used instead of the default A_WeaponReady, and GetAtkState is overridden so it can return a FireSingle state sequence if the above mentioned boolean is true:

class MyWeapon : Weapon
{
	bool singleShotMode;

	// a custom version of A_WeaponReady that switches modes when pressing
	// altfire (the AltFire state sequence is not invoked)
	action void A_MyWeaponReady(int flags = 0)
	{
		// flip modes if the player pressed Altfire but not holding it:
		if ((player.cmd.buttons & BT_ALTATTACK) && !(player.oldbuttons & BT_ALTATTACK))
		{
			A_StartSound("weapons/switchmodes");
			invoker.singleShotMode = !invoker.singleShotMode;
		}
		A_WeaponReady(flags|WRF_NOSECONDARY);
	}

	override State GetAtkState(bool hold)
	{
		if (singleShotMode == true)
		{
			return FindState("FireSingle");
		}
		return Super.GetAtkState(hold);
	}
}

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.


	virtual State GetAtkState (bool hold)
	{
		State s = null;
		if (hold) s = FindState('Hold');
		if (s == null) s = FindState('Fire');
		return s;
	}

See also