ZScript global variables

ZScript possesses several global variables accessible by all classes.

TypeNameWritable?Use/meaning
Array<class>AllClassesnoAn array that contains all classes currently available.
Array<class<Actor> >AllActorClassesnoAn array that contains all Actor classes currently available.
Array<@PlayerClass>PlayerClassesnoAn array that contains objects related to all player classes currently available.
Array<@PlayerSkin>PlayerSkinsnoAn array that contains objects related to all player skins currently available.
Array<@Team>TeamsnoAn array that contains objects related to all teams currently available.
Array<@TerrainDef>TerrainsnoAn array that contains all available TerrainDefs in the game.
intvalidcountyesInternal variable used to avoid processing of the same data during one render frame.
boolmultiplayernoSet to true if the game has multiple players.
booldeathmatchnoSet to true if the game mode is deathmatch.
boolteamplaynoSet to true if the game mode is team deathmatch.
@KeyBindingsBindingsyesObject that contains functionality related to getting keybindings for gameplay.
@KeyBindingsAutomapBindingsyesObject that contains functionality related to getting keybindings for the automap.
@DehInfodehyesPlay-scoped struct that contains internal values used by DeHackEd-parsing code.
@GameInfoStructgameinfonoA struct that contains information related to the game info set in MAPINFO.
boolnetgamenoUI-scoped variable that's set to true if the game is networked.
boolautomapactivenoSet to true if the player's automap is active.
uintgameactionnoDeeply internal variable used to get/set the current engine state.
intgamestatenoInternal variable that dictates the current state of the game.
TextureIDskyflatnumnoStores the TextureID for the flat that sets skies in maps.
FontsmallfontnoDefault font type for small fonts.
Fontsmallfont2noDefault font type for secondary small fonts.
FontbigfontnoDefault font type for big fonts.
FontconfontnoDefault font type for console fonts.
FontNewConsoleFontnoDefault font type for new console fonts.
FontNewSmallFontnoDefault font type for new small fonts.
FontAlternativeSmallFontnoDefault font types for alternative small fonts.
FontOriginalSmallFontnoDefault font type for vanilla small fonts.
FontOriginalBigFontnoDefault font type for vanilla big fonts.
FontintermissionfontnoDefault font type for intermission fonts.
intCleanXFacnoX scale for best fitting a 320x200 area to the screen without fractional steps
intCleanYFacnoY scale for best fitting a 320x200 area to the screen without fractional steps
intCleanWidthnoVirtual width of the screen given CleanXFac
intCleanHeightnoVirtual height of the screen given CleanYFac
intCleanXFac_1noSame as CleanXFac but subtracted by 1. Will not go below a value of 1.
intCleanYFac_1noSame as CleanYFac but subtracted by 1. Will not go below a value of 1.
intCleanWidth_1noVirtual width of the screen given CleanXFac_1
intCleanHeight_1noVirtual height of the screen given CleanYFac_1
intmenuactiveyesUI-scoped variable that's set to true if a menu is open.
@FOptionMenuSettingsOptionMenuSettingsnoA struct that contains information about settings related to the options menu.
intgameticnoStores how many tics have passed since the game started.
booldemoplaybacknoSet to true if currently playing back a demo file.
intBackbuttonTimeyesUI-scoped variable that tracks the alpha fading of the menu back button image.
floatBackbuttonAlphayesUI-scoped variable that stores the alpha value of the menu back button image.
intNet_ArbitratornoThe player number for the player currently considered the host of a net game.
BaseStatusBarStatusBaryesUI-scoped variable that stores a pointer to the status bar.
WeaponWP_NOCHANGEnoAn internal constant to describe the case when PlayerInfo.PendingWeapon doesn't have a value.
intLocalViewPitchyesStores the player's current pitch change during a tic.
@MusPlayingInfomusplayingnoA struct that contains information about the current music playing.
boolgeneric_uinoIf true generic fonts are used instead of game-specific ones.
@PlayerInfo[]playersyesPlay-scoped array containing all PlayerInfo structs of players in the level.
bool[]playeringamenoAn array that defines whether the specified PlayerInfo struct in players is set.
intconsoleplayernoThe player number of the current client.
LevelLocalslevelyesPlay-scoped struct that contains various level-related data.
intpausednoIf true, Doom's special pause mode is active.
uint8ConsoleStateno(New from 4.10.0)
The current state of ZDoom's console. Can be c_up, c_down, c_falling, or c_rising.
Note: This feature is for ZScript only.

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. If you have global data that only needs to be loaded once at the game's launch, consider using a StaticEventHandler so that the data doesn't need to be recalculated every time a new level is loaded.

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

class MyEventHandler : EventHandler
{
    int myGlobalInt;
}

// Example of accessing MyEventHandler from an actor.
class MyClass : 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.

class MyGlobalVariables : Thinker
{
    int testVar;

    static MyGlobalVariables Get()
    {
        ThinkerIterator it = ThinkerIterator.Create("MyGlobalVariables", STAT_INFO); // Change this to STAT_STATIC if your thinker persists between maps.
        let p = MyGlobalVariables(it.Next());
        if (!p)
        {
            p = new("MyGlobalVariables");
            p.ChangeStatNum(STAT_INFO); // Change this to STAT_STATIC if persisting between maps is desired.
        }

        return p;
    }
}

// Example of an actor accessing the above thinker's variables.
class MyClass : Actor
{
    MyGlobalVariables globalVars;

    override void PostBeginPlay()
    {
        super.PostBeginPlay();

        globalVars = MyGlobalVariables.Get();
        if (globalVars)
        {
            globalVars.testVar++;
            Console.Printf("My global int is: %d", globalVars.testVar);
        }
    }
}

This article is issued from Zdoom. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.