ZScript global variables

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


ZScript possesses several global variables accessible by all classes.

Type Name Writable? Use/meaning
Array<class> AllClasses no An array that contains all classes currently available.
Array<class<Actor> > AllActorClasses no An array that contains all Actor classes currently available.
Array<@PlayerClass> PlayerClasses no An array that contains objects related to all player classes currently available.
Array<@PlayerSkin> PlayerSkins no An array that contains objects related to all player skins currently available.
Array<@Team> Teams no An array that contains objects related to all teams currently available.
int validcount yes Internal variable used to avoid processing of the same data during one render frame.
bool multiplayer no Set to true if the game has multiple players.
@KeyBindings Bindings yes Object that contains functionality related to getting keybindings for gameplay.
@KeyBindings AutomapBindings yes Object that contains functionality related to getting keybindings for the automap.
@DehInfo deh yes Play-scoped struct that contains internal values used by DeHackEd-parsing code.
@GameInfoStruct gameinfo no A struct that contains information related to the game info set in MAPINFO.
bool netgame no UI-scoped variable that's set to true if the game is networked.
bool automapactive no Set to true if the player's automap is active.
uint gameaction no Deeply internal variable used to get/set the current engine state.
int gamestate no Internal variable that dictates the current state of the game.
TextureID skyflatnum no Stores the TextureID for the flat that sets skies in maps.
Font smallfont no Default font type for small fonts.
Font smallfont2 no Default font type for secondary small fonts.
Font bigfont no Default font type for big fonts.
Font confont no Default font type for console fonts.
Font NewConsoleFont no Default font type for new console fonts.
Font NewSmallFont no Default font type for new small fonts.
Font AlternativeSmallFont no Default font types for alternative small fonts.
Font OriginalSmallFont no Default font type for vanilla small fonts.
Font OriginalBigFont no Default font type for vanilla big fonts.
Font intermissionfont no Default font type for intermission fonts.
int CleanXFac no X scale for best fitting a 320x200 area to the screen without fractional steps
int CleanYFac no Y scale for best fitting a 320x200 area to the screen without fractional steps
int CleanWidth no Virtual width of the screen given CleanXFac
int CleanHeight no Virtual height of the screen given CleanYFac
int CleanXFac_1 no Same as CleanXFac but subtracted by 1. Will not go below a value of 1.
int CleanYFac_1 no Same as CleanYFac but subtracted by 1. Will not go below a value of 1.
int CleanWidth_1 no Virtual width of the screen given CleanXFac_1
int CleanHeight_1 no Virtual height of the screen given CleanYFac_1
int menuactive yes UI-scoped variable that's set to true if a menu is open.
@FOptionMenuSettings OptionMenuSettings no A struct that contains information about settings related to the options menu.
int gametic no Stores how many tics have passed since the game started.
bool demoplayback no Set to true if currently playing back a demo file.
int BackbuttonTime yes UI-scoped variable that tracks the alpha fading of the menu back button image.
float BackbuttonAlpha yes UI-scoped variable that stores the alpha value of the menu back button image.
int Net_Arbitrator no The player number for the player currently considered the host of a net game.
BaseStatusBar StatusBar yes UI-scoped variable that stores a pointer to the status bar.
Weapon WP_NOCHANGE no An internal constant to describe the case when PlayerInfo.PendingWeapon doesn't have a value.
int LocalViewPitch yes Stores the player's current pitch change during a tic.
@MusPlayingInfo musplaying no A struct that contains information about the current music playing.
bool generic_ui no If true generic fonts are used instead of game-specific ones.
@PlayerInfo[] players yes Play-scoped array containing all PlayerInfo structs of players in the level.
bool[] playeringame no An array that defines whether the specified PlayerInfo struct in players is set.
int consoleplayer no The player number of the current client.
LevelLocals level yes Play-scoped struct that contains various level-related data.

Creating Global Variables

There are two methods available for handling global variables: EventHandlers and Thinkers. Each has their advantages and disadvantages.

EventHandlers come in two flavors: EventHandler, which is only available within maps but are saved, and StaticEventHandler, which persist outside of maps but are never saved to savegames. See Events and handlers for more information on how to set up an event handler.

Individual EventHandler classes can be referenced using the Find() function, as below.

// The EventHandler itself.
Class MyEventHandler : EventHandler
{
	int MyGlobalInt;
}

// Example of accessing said EventHandler from an actor.
Class MyCustomClass : Actor
{
	MyEventHandler Event;
	override void PostBeginPlay()
	{
		Super.PostBeginPlay();
		// Be sure to call the proper `Find()` method for the type of EventHandler you are using or problems may occur.
		// `EventHandler.Find()` for EventHandler and `StaticEventHandler.Find()` for StaticEventHandler
		Event = MyEventHandler(EventHandler.Find("MyEventHandler"));
		if (Event)
		{
			Event.MyGlobalInt = 1;
			Console.Printf("My global int is: %d", Event.MyGlobalInt);
		}
	}
}

All Thinkers, by contrast, are saved to savegames. Using a ThinkerIterator is slower than the StaticEventHandler's Find function, but a Thinker with the statnum STAT_STATIC is the only way to store information for the duration of a play session, saved and loaded or not. This performance hit can be mitigated by keeping a reference to the Thinker being used for global variables in either the actors needing to reference it or another, higher global variable keeper, such as an EventHandler.

// An example global variables thinker.
class MyGlobalVariables : Thinker
{
	int testVar;

	MyGlobalVariables Init()
	{
		ChangeStatNum(STAT_INFO); // Change this to STAT_STATIC if persisting between maps is desired.
		return self;
	}

	static MyGlobalVariables Get()
	{
		ThinkerIterator it = ThinkerIterator.Create("MyGlobalVariables",STAT_INFO); // Change this to STAT_STATIC if persisting between maps is desired.
		let p = MyGlobalVariables(it.Next());
		if (p == null)
		{
			p = new("MyGlobalVariables").Init();
		}
		return p;
	}
}

// Example of an actor accessing the above thinker's variables.
Class MyCustomClass : Actor
{
	override void PostBeginPlay()
	{
		Super.PostBeginPlay();
		let globalvars = MyGlobalVariables.Get();
		if (globalvars)
		{
			globalvars.testVar++;
			Console.Printf("My global int is: %d", globalvars.testVar);
		}
	}
}