[HOWTO] How To: Connect C# Variables to Dialogue System's Lua
Posted: Wed Jul 26, 2023 2:33 pm
You can define Dialogue System variables in the Dialogue Editor's Variables section. At runtime, these Dialogue System variables will be accessible in the Dialogue System's Lua environment. You can also access them in your own C# code using the DialogueLua class.
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:
The getter and setter functions can be:
(Note: This is a numeric variable, so we'll use a 'double' number type because that's what Lua uses internally.)
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:
The final step is to register your methods with Lua. You can find a starter template script in Assets / Plugins / Pixel Crushers / Dialogue System / Templates / Scripts / TemplateCustomLua.cs. Below is a script based on the template that incorporates the info above:
Once your functions are registered, you can use them in dialogue entries' Conditions and Script fields.
You can also use the [lua(code)] markup tag to show them in dialogue text, such as:
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."