[Script] Access Variables


(Leslie Young) #1

The BGS variables can be accessed in script. They all share the same API and only differ in how you gain access to the “container” of the variables of interest.

Global Variables

Use GlobalVariables.Instance to get a reference to the global variables, for example

GlobalVariables.Instance.SetValue("variable-name", value);

Meta Properties (Object Variables)

You need a reference to the object that has this component on it.

// this is a simple example. you would have a better way to keep these references.
GameObject npc = GameObject.Find("Shopkeeper");

// now get a reference to the variables and make use of them
MetaProperties vars = npc.GetComponent<MetaProperties>();
vars.SetValue("variable-name", value);

API

public Variable GetVarRef(string name)
Get reference to a variable. The actual variable, not its value. Good to do when you know you will need to access the variable a lot and want to cache a reference to it.

public void SetValue(string name, object value, bool createIfNotExist = false)
Set the value of a variable. Optionally force it to be created if it does not yet exist.

public object GetValue(string name)
Returns the value of a variable.

public GameObject GetGameObjectValue(string name)
Get the value as a GameObject when you know it will be a GameObject type returned. This will work even if the variable is actually storing a component rather than GameObject.

public T GetComponentValue<T>(string name)
Get the value as some component,. This will work even if the variable is storing a reference to a GameObject or another Component, as long as the component type you are after is on the GameObject. Example var tr = GlobalVariables.Instance.GetComponentValue<Transform>("variable-name");

public T GetUnityObjectValue<T>(string name)
Get the value as some Unity type like Texture2D, or Sprite. Example var sprite = GlobalVariables.Instance.GetUnityObjectValue<Sprite>("variable-name");

Variable API

With a reference to a variable you may use these functions.

public void SetValue(object v)
Set value of this variable.

public object GetValue()
Get value from this variable.

public bool GetBoolValue()
Get value as a Boolean (True/False). Will attempt to convert value to Boolean if the variable is not actually storing a Boolean value. Also better to use when you know the value must be of this type so that casting is not needed with the general purpose GetValue() method.

public float GetFloatValue()
Get value as a Float. Will attempt to convert value to Float if the variable is not actually storing a Float value. Also better to use when you know the value must be of this type so that casting is not needed with the general purpose GetValue() method.

public string GetStringValue()
Get value as a String. Will attempt to convert value to String if the variable is not actually storing a String value. Also better to use when you know the value must be of this type so that casting is not needed with the general purpose GetValue() method.


0.2 visual novel plugin