Page 1 of 1

Grab varaibles outside of dialogue system

Posted: Sat May 08, 2021 12:16 pm
by jsebastian
For several variables in my game, it makes more sense to store them in other objects than inside of the Dialogue system itself. However, I still want Dialogue system to be able to refer to them, and display them to the user. For example, when you return from combat I want the general to refer to how many kills the player achieved. Though this variable is a part of the dialogue, it's really best served to be saved outside of the Dialogue System as part of a Scriptable Object, or even a Monobehavior.

Is there a way to achieve this? I suppose one solution would be to "push" the variable into a Dialogue System variable using

Code: Select all

SetVariable()
however this solution seems clunky. It raises more questions than it answers, such as how often I should call SetVariable, should it be even generated? Should it happen at specific times in my scene? Who's responsible for calling that function etc. What if I rename the variable on either end? It seems to be tightly coupled.

Re: Grab varaibles outside of dialogue system

Posted: Sat May 08, 2021 1:01 pm
by Tony Li
Hi,

I recommend making Get methods for your variables, and Set methods if conversations need to set their values. Example:

Code: Select all

public int killCount;

public double GetKillCount() { return killCount; }
Then register the function(s) with Lua. You can use them in dialogue entries' Conditions and Script fields, and also show them in dialogue text using the [lua(code)] tag. Example:
  • Dialogue Text: "Wow, [lua(GetKillCount())] kills! That's quite an achievement!"
  • Conditions: GetKillCount() > 10
Tutorial:


Re: Grab varaibles outside of dialogue system

Posted: Sat May 08, 2021 8:53 pm
by jsebastian
This is perfect, thank you.

Re: Grab varaibles outside of dialogue system

Posted: Sat May 08, 2021 9:09 pm
by Tony Li
Happy to help! :-)