Variable synchronization

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Fitbie
Posts: 44
Joined: Tue Dec 07, 2021 6:30 pm

Variable synchronization

Post by Fitbie »

Hi guys and Tony! I was wondering: is there a "smarter" way to do this.
I want to synchronize a variable that is stored in my GameManager with a variable in the dialogue system (to save it)
I came up with the idea of using the properties

Code: Select all

[SerializeField] [Range(0, 100)] private int _sanity;
    public int sanity
    {
        get
        {
            _sanity = DialogueLua.GetVariable("Sanity").AsInt;
            return _sanity;
        }
        set
        {
            _sanity = Mathf.Clamp(value, 0, 100);
            DialogueLua.SetVariable("Sanity", _sanity);
        }
    }
Is there a smarter way to do it? Or better just write a saver for my variables (given that they are not currently used in dialogs)
I also wanted to know if there is a way to backsynchronize the variables from the dialogue system

Thanks!
User avatar
Tony Li
Posts: 22131
Joined: Thu Jul 18, 2013 1:27 pm

Re: Variable synchronization

Post by Tony Li »

One catch with that approach is that you've marked the _sanity variable with [SerializeField]. This means you can change the value in the Inspector. However, that won't update the parallel Dialogue System variable. If you want to use a property, remove [SerializeField].

Alternatively, you can remove the property. In this case, don't define a parallel Dialogue System variable. Instead, register a Lua function to get the value of _sanity.
Post Reply