InStateSequence

From ZDoom Wiki
Jump to navigation Jump to search
Note: This feature is for ZScript only.

Actor

clearscope static bool InStateSequence (State newstate, State basestate)

Usage

Used to check that an actor's current state is in a specific state sequence. More specifically, it takes a state pointer passed to the first argument, and then checks if it's currently anywhere in the state sequence passed to the second argument. The state sequence (second argument) in this case is obtained through a pointer to its first state, e.g. ResolveState("Pain") is a pointer to the first state in the "Pain" state sequence.

For example, InStateSequence(<actorpointer>.curstate, <actorpointer>.ResolveState("Pain")) will return true if the <actorpointer> actor is currently anywhere in its "Pain" state sequence. Note, this is different from checking if <actorpointer>.curstate == <actorpointer>.ResolveState("Pain"), because the latter will only return true if curstate is in the first state of the actor's "Pain" sequence.

Since the function is defined as static, it doesn't need to be prefixed with an actor pointer; however, the states passed for the first and second arguments must be retrieved from a pointer to a specific actor. Note, however, that this function is defined in the Actor class, so if it's called outside of an actor (for example, from an EventHandler), it has to be prefxied with Actor..

The State struct also has a non-static version of this function, which must be prefixed with a state pointer, such as <actorpointer>.curstate.InStateSequence(<actorpointer>.ResolveState("Pain")).

When you need to check the state of a Weapon animation, remember that its animation states are handled by the PSprite class, while the weapon itself doesn't use them. As such, in this case you first need a PSprite pointer:

let psp = player.FindPSprite(PSP_WEAPON); //obtain a pointer to the PSprite of the main layer
if (psp && InStateSequence(psp.curstate, ResolveState("Ready")))
{
  // This block is executed if the PSprite pointer is non-null,
  // and it's currently in the weapon's Ready state sequence
}

Note: To get a better idea of what exactly a state sequence is, see here.

Parameters

  • State newstate
The state to check. If you need to check the current state of an actor, pass <actorpointer>.curState. For PSprite, similarly, pass <PSpritePointer>.curState.
  • State basestate
Pointer to the first state in the state sequence you need to check. Can be obtained with ResolveState, for example <actorpointer>.ResolveState("Pain").
Note, actors have a spawnState field that already contains a pointer to the actor's first Spawn state, so that can be passed as well if the need is to check if an actor is in its Spawn state sequence.

Examples

This DoEffect override, if inserted into a Weapon, will print "<weaponname> is currently ready" whenever this weapon is selected and is in its Ready state sequence:

override void DoEffect()
{
	super.DoEffect();
	if (!owner || !owner.player)
	{
		return;
	}
	let weap = owner.player.readyweapon;
	if (!weap || weap != self)
	{
		return;
	}

	let psp = owner.player.FindPSprite(PSP_WEAPON);
	if (psp && InStateSequence(psp.curstate, ResolveState("Ready")))
	{
		Console.MidPrint(smallfont, String.Format("Weapon %s is currently ready", GetTag()));
	}
}

This plasma ball will announce when it has gibbed what it hits:

class GibAnnouncer : PlasmaBall replaces PlasmaBall
{
    States
    {
    XDeath:
        PLSX A 0
        {
            if(blockingMObj && InStateSequence(blockingMObj.curState, blockingMObj.ResolveState("XDeath")))
            {
                A_Log("%s was gibbed!", blockingMObj.GetTag());
            }
        }
        Goto Death;
    }
}

See also