-
Notifications
You must be signed in to change notification settings - Fork 8
Config Variables
Config variable (cvar) represents a value you can change from the terminal window and save across play mode launches.
It's recommended to keep all your config variables in a single file: Scripts/CVars.cs
using LunarPlugin;
[CVarContainer]
public static class CVars
{
public static readonly CVar myVar = new CVar("myVar", 10);
...
public static readonly CVar myLastVar = new CVar("myLastVar", "Some string");
}
To define cvar you need to create and initialize an instance of class CVar
.
The type of variable is derived from it's default value:
using UnityEngine;
using LunarPlugin;
[CVarContainer]
public static class CVars
{
public static readonly CVar c_int = new CVar("c_int", 10);
public static readonly CVar c_float = new CVar("c_float", 10.0f);
public static readonly CVar c_string = new CVar("c_string", "Some string");
public static readonly CVar c_bool = new CVar("c_bool", false);
public static readonly CVar c_vector2 = new CVar("c_vector2", Vector2.zero);
public static readonly CVar c_vector3 = new CVar("c_vector3", Vector3.zero);
public static readonly CVar c_vector4 = new CVar("c_vector4", Vector4.zero);
}
IMPORTANT: You need to initialize CVars
class before Unity runs the first frame of the game (at this point the values of all variables are loaded from the config file). The best way of doing that is to add CVarContainer
attribute to the class.
You can list all available variables with cvarlist
command:
> cvarlist
c_bool 0
c_float 10
c_int 10
c_string "Some string"
c_vector2 "0 0"
c_vector3 "0 0 0"
c_vector4 "0 0 0 0"
Partial names and Tab
auto completions are supported as well:
> cvarlist c_vec
c_vector2 "0 0"
c_vector3 "0 0 0"
c_vector4 "0 0 0 0"
To set a new value to the variable - type its name followed by the new value:
> c_vector3 10 20 30
To get the current value - type just a name:
> c_vector3
c_vector3 is:"10 20 30" default:"0 0 0"
You can get the current variable value using one of the getter-properties:
-
.Value
-string
value -
.IntValue
-int
value or0
if not applicable -
.FloatValue
-float
value or0.0f
if not applicable -
.BoolValue
-bool
value orfalse
if not applicable -
.ColorValue
-UnityEngine.Color
value ordefault(Color)
if not applicable -
.RectValue
-UnityEngine.Rect
value ordefault(Rect)
if not applicable -
.Vector2Value
-UnityEngine.Vector2
value ordefault(Vector2)
is not applicable -
.Vector3Value
-UnityEngine.Vector3
value ordefault(Vector3)
is not applicable -
.Vector4Value
-UnityEngine.Vector4
value ordefault(Vector4)
is not applicable
You can register delegates and get notified whenever the value is changed:
myVar.AddDelegate(delegate(CVar cvar)
{
Debug.Log("New value is " + cvar.Value);
});
Don't forget to remove the delegate to avoid memory leaks (would be changed to weak references in the future)
myVar.RemoveDelegates(target);
You can quickly toggle boolean variable with toggle command:
> c_bool
c_bool is:"0" default:"0"
> toggle c_bool
> c_bool
c_bool is:"1" default:"0"
Tab
autocompletion is supported.
You can bind boolean variable to a key/mouse press: when binding key is pressed - the value is set to 1
; when key is released - 0
> bind mouse1 +c_bool
You can reset variable to its default value using reset
command:
> c_bool
c_bool is:"1" default:"0"
> reset c_bool
> c_bool
c_bool is:"0" default:"0"
Tab
autocompletion is supported.
You can reset ALL variables to their default values using reset
command:
> resetAll
Or only reset variables which names start with a prefix:
> resetAll c_v
Tab
autocompletion is supported.