referencing variables not in a function

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
cluin14
Posts: 2
Joined: Wed Oct 28, 2020 2:45 pm

referencing variables not in a function

Post 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!
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: referencing variables not in a function

Post 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!"
cluin14
Posts: 2
Joined: Wed Oct 28, 2020 2:45 pm

Re: referencing variables not in a function

Post by cluin14 »

That was exactly what I was looking for. Thank you so much for your help and quick response. It's much appreciated!
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: referencing variables not in a function

Post by Tony Li »

Happy to help!
Post Reply