Grab varaibles outside of dialogue system

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
jsebastian
Posts: 5
Joined: Sun Apr 04, 2021 10:01 pm

Grab varaibles outside of dialogue system

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

Re: Grab varaibles outside of dialogue system

Post 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:

jsebastian
Posts: 5
Joined: Sun Apr 04, 2021 10:01 pm

Re: Grab varaibles outside of dialogue system

Post by jsebastian »

This is perfect, thank you.
User avatar
Tony Li
Posts: 22159
Joined: Thu Jul 18, 2013 1:27 pm

Re: Grab varaibles outside of dialogue system

Post by Tony Li »

Happy to help! :-)
Post Reply