However, sometimes you might want to the other way -- define variables in your C# code and make those C# variables accessible to the Dialogue System's Lua environment.
To do this, you'll need to create "getter" and/or "setter" C# methods that get and set the variable's value. For example, say we define a C# variable named numCoins:
Code: Select all
public int numCoins;
Code: Select all
public int GetNumCoins() { return numCoins; }
public void SetNumCoins(double value) { numCoins = (int)value; }
The GetNumCoins() method returns the value of numCoins. SetNumCoins(value) sets the value of numCoins.
You might define this in a script named CoinTracker, such as:
Code: Select all
public class CoinTracker : MonoBehaviour
{
public int numCoins;
public int GetNumCoins() { return numCoins; }
public void SetNumCoins(double value) { numCoins = (int)value; }
}
Code: Select all
using UnityEngine;
using PixelCrushers.DialogueSystem;
public class CoinTracker : MonoBehaviour
{
public int numCoins;
public int GetNumCoins() { return numCoins; }
public void SetNumCoins(double value) { numCoins = (int)value; }
[Tooltip("If this script is on your Dialogue Manager, leave unticked so temporary Dialogue Managers don't unregister your functions.")]
public bool unregisterOnDisable = false;
void OnEnable() // Register the getter and setter methods with Lua.
{
Lua.RegisterFunction(nameof(GetNumCoins), this, SymbolExtensions.GetMethodInfo(() => GetNumCoins()));
Lua.RegisterFunction(nameof(SetNumCoins), this, SymbolExtensions.GetMethodInfo(() => SetNumCoins(0)));
}
void OnDisable() // Unregister the getter and setter methods.
{
if (!unregisterOnDisable) return;
Lua.UnregisterFunction(nameof(GetNumCoins));
Lua.UnregisterFunction(nameof(SetNumCoins));
}
}
You can also use the [lua(code)] markup tag to show them in dialogue text, such as:
- Dialogue Text: "I see you have [lua(GetNumCoins())] coins."