FindStateByString

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


Actor

clearscope native state FindStateByString(string st, bool exact = false)

Usage

Functions like FindState, except that it accepts a text string data type as the state to search for, instead of directly having to use a StateLabel type. The main use case is for dynamically creating states to jump to using the built-in String functions.

Note: just like FindState, must be prefixed with invoker. if called in PSprite context (i.e. from a Weapon state or action function), otherwise it won't know which actor's state (the weapon's or the owner's) must be found.

Parameters

  • string st
The string containing the name of the state sequence, such as "Spawn", "Missile", etc.
  • bool exact
Finds the exact state passed to st, this is useful for finding substates like the ones that the Pain and (X)Death states support, or custom substates like "Jump.Up:". For example, it can be used to find if the caller has a Pain.Electric state in particular.

Examples

This actor jumps to one of 5 states picked at random. State label is constructed with String.Format:

class RandomStringStateJumper : Actor
{
	States
	{
		Spawn:
			TNT1 A 0 NoDelay
			{				
				// Construct state label name from the string "Graphic"
				// and a randomly picked number in a 1-5 range:
				String pickState = String.Format ("Graphic%d", random(1,5));
				return FindStateByString (pickState);
			}
			Stop;
		Graphic1:
			PLAY A -1;
			Stop;
		Graphic2:
			POSS A -1;
			Stop;
		Graphic3:
			TRE2 A -1;
			Stop;
		Graphic4:
			TLMP A -1;
			Stop;
		Graphic5:
			SMT2 A -1;
			Stop;
	}
}

Note: This isn't a recommended method to do this, since a better option would arguably be to modify the sprite directly with GetSpriteIndex; this example is just for illustrative purposes.

See also