Page 1 of 1

Variable synchronization

Posted: Sun May 14, 2023 10:44 am
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!

Re: Variable synchronization

Posted: Sun May 14, 2023 11:08 am
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.