CVar (ZScript)

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


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

WARNING: This works for server cvars only. Attempts to access user cvars will cause a desync in multiplayer.

Methods

Static

  • CVar FindCVar(Name name)
Searches for the console variable specified by name, returning it if it finds it. If the variable could not be found, the function returns null.
  • cvar GetCVar(name name, playerinfo player)
Returns a specific player's user cvar of that name. NOT to be confused with the ACS function or the DECORATE expression which return numeric values stored in the named cvar!

Non-static

  • int GetInt()
Returns the value of the console variable as an integer value.
  • double GetFloat()
Returns the value of the console variable as a floating-point value.
  • String GetString()
Returns the value of the console variable as a string.
  • void SetInt(int v)
Sets the value of an integer console variable to the specified value. Engine-defined console variables can only be changed from menus.
  • void SetFloat(double v)
Sets the value of a floating-point console variable to the specified value. Engine-defined console variables can only be changed from menus.
  • void SetString(String s)
Sets the value of a string console variable to the specified string. Engine-defined console variables can only be changed from menus.
  • int GetRealType()
Returns the type of the console variable. Refer to the ECVarType enum below for the available types the function returns.
  • int ResetToDefault()
Resets the console variable's value to its default value.

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 ()
    {
        CVar attacksetting = CVar.FindCVar('specialimpattack');

        if(attacksetting)
        {
            switch(attacksetting.GetInt())
            {
                default:
                case 0: 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;
    }
}

Definition

struct CVar native
{
    enum ECVarType
    {
        CVAR_Bool,
        CVAR_Int,
        CVAR_Float,
        CVAR_String,
        CVAR_Color,
    };

    native static CVar FindCVar(Name name);
    native int GetInt();
    native double GetFloat();
    native String GetString();
    native void SetInt(int v);
    native void SetFloat(double v);
    native void SetString(String s);
    native int GetRealType();
    native int ResetToDefault();
}