Page 1 of 1

referencing variables not in a function

Posted: Wed Oct 28, 2020 2:57 pm
by cluin14
Hello, I watched the videos on how to import functions which was helpful knowledge, but not with this specific case.
In my dialogue I'm trying to reference strings from a script such as HairColor, EyeColor and the likes that aren't housed in a function.
I feel like the persistent data template is what I should be utilizing but I'm not 100% sure on that. Any help would be much appreciated!

Re: referencing variables not in a function

Posted: Wed Oct 28, 2020 4:10 pm
by Tony Li
Hi,

The recommended way is to write functions to get and set the script variable. Example:

Code: Select all

public string HairColor; // Can be "brown", "blond", "red", etc.

public string GetHairColor() { return HairColor; }
public void SetHairColor(string value) { HairColor = value; }

void Awake()
{
    Lua.RegisterFunction("GetHairColor", this, SymbolExtensions.GetMethodInfo(() => GetHairColor()));
    Lua.RegisterFunction("SetHairColor", this, SymbolExtensions.GetMethodInfo(() => SetHairColor(string.Empty)));
}
Alternatively, you can store the values in Dialogue System variables, either as the primary location for the values, or just to copy them to Dialogue System variables when a conversation starts so you can reference them in the conversation. Example:

Code: Select all

void OnConversationStart(Transform actor) // Called when a conversation starts. Must be on participant or Dialogue Manager.
{
    DialogueLua.SetVariable("HairColor", HairColor);
}
  • Dialogue Text: "What pretty [var=HairColor] you have!"

Re: referencing variables not in a function

Posted: Wed Oct 28, 2020 4:45 pm
by cluin14
That was exactly what I was looking for. Thank you so much for your help and quick response. It's much appreciated!

Re: referencing variables not in a function

Posted: Wed Oct 28, 2020 4:53 pm
by Tony Li
Happy to help!