Structs:CVar

From ZDoom Wiki
(Redirected from CVar (ZScript))
Jump to navigation Jump to search
Note: This feature is for ZScript only.


The CVar struct is a container of functions (methods) used to access and manipulate console variables.

Methods

Static

  • CVar FindCVar (Name name)
(Warning: using this function to get and manipulate user-scoped variables can cause demos and multiplayer games to desync. It's recommended to use GetCVar() instead and pass a specific player's info.)
Returns a pointer to the specified console variable by name. If the variable could not be found, the function returns null.
  • CVar GetCVar (Name name, PlayerInfo player = null)
Returns a pointer to the specified console variable by name. For user-scoped variables, player is the player whose copy of the variable to get. For server-scoped variables, player is meaningless and thus ignored. If the variable could not be found, or it is user-scoped and player is null, the function returns null.

Note: Server-scoped CVars and GZDoom's internal CVars can be directly accessed through their name. Find/GetCVar() are not necessary in those instances.

Non-static

  • bool GetBool ()
Returns the value of the console variable as a boolean value.
  • double GetFloat ()
Returns the value of the console variable as a floating-point value.
  • int GetInt ()
Returns the value of the console variable as an integer value. For color console variables this will return an RGB value in the form of three integers, such as 255, 0, 0 for red. It can then be directly passed to a color-type field.
  • int GetRealType ()
Returns the type of the console variable. The types are as follows:
  • CVAR_Bool
  • CVAR_Int
  • CVAR_Float
  • CVAR_String
  • CVAR_Color
  • String GetString ()
Returns the value of the console variable as a string. For color console variables, this is in the form of "RR GG BB", e.g. "00 ff 00" for green.
Note: Engine-defined console variables can only be set or reset from menus.


  • int ResetToDefault ()
Resets the console variable's value to its default value.
  • void SetBool (bool b)
Sets the value of a boolean console variable to the specified value.
  • void SetFloat (double v)
Sets the value of a floating-point console variable to the specified value.
  • void SetInt (int v)
Sets the value of an integer or color console variable to the specified value. For color console variables, this can be done by passing the value as a hexadecimal value, e.g. 0x00ff00 for green.
  • void SetString (String s)
Sets the value of a string or color console variable to the specified string. For color console variables, this can be done by passing the value in the form of "RR GG BB", e.g. "00 ff 00" for green. Color names from the X11R6RGB lump are also acceptable.

Handlers

Custom CVars are capable of having "handlers" associated with them, which directly control the variables of the CVars. There are five classes in total:

  • CustomIntCvar
  • CustomFloatCvar
  • CustomStringCvar
  • CustomBoolCvar
  • CustomColorCvar

In CVARINFO:

User HandlerClass(SomeValueClamper) Float SomeValueCVar

In ZScript:

class SomeValueClamper : CustomFloatCVar
{
    override double ModifyValue(Name CVarName, double val)
    {
        return clamp(val, 0.05, 1.0);
    }
}

This allows finer control over what values a cvar can be at any given time.

Examples

The attack this imp does depends on the value of the specialimpattack custom console variable.

// In CVARINFO:
server int specialimpattack;
// In ZScript:
class ExampleImp : DoomImp
{
    Default
    {
        +DONTHARMCLASS
    }

    void A_SpecialTroopAttack ()
    {
        switch (specialimpattack)
        {
            default:A_TroopAttack(); break;
            case 1: A_BruisAttack(); break;
            case 2: A_CyberAttack(); break;
        }
    }

    States
    {
    Melee:
    Missile:
        TROO EF 8 A_FaceTarget;
        TROO G 6 A_SpecialTroopAttack;
        Goto See;
    }
}