Page 1 of 1

Changing Variable not Updating

Posted: Wed Nov 13, 2019 9:41 pm
by slothins
Hello,

I have a variable named

Code: Select all

currentDay
that is accessed to display its current value.

This is the DayText that accesses it:

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using PixelCrushers.DialogueSystem;
public class DayText : MonoBehaviour] {

    public Text days;
    public string updatedText;
    // Update is called once per frame
    void Update() {
        updatedText = DialogueLua.GetVariable("currentDay").asString;
        days.text = updatedText;
    }
}
It displays the initial value of the variable but it isn't updating when the value changes.

Too change the value I have in the Script portion of the nodes written

Code: Select all

currentDay = 2;
or any other integer but it doesn't update. I've debugged the value on its own to make sure it wasn't the updating the text part that was failing, and it was doing the same only display initial value thing.

Re: Changing Variable not Updating

Posted: Wed Nov 13, 2019 10:15 pm
by Tony Li
Hi,

Use this in the Script field:

Code: Select all

Variable["currentDay"] = 2;
You don't have to type it manually; the "..." button will give you dropdowns where you can do it without any typing. It's also super handy for avoiding typos.

(You can skip this if you're not interested in a detailed explanation:)

When you define variables in your dialogue database, at runtime the Dialogue System's Lua stores them in an array named Variable[]. So Variable["currentDay"] corresponds to the variable in your dialogue database. If you run "currentDay = 2", this just sets a new Lua variable not specifically associated with anything in your dialogue database.

Re: Changing Variable not Updating

Posted: Thu Nov 14, 2019 3:59 pm
by slothins
Perfect, thanks! It was the way I was calling the variable in the dialogue tree that was wrong.

Re: Changing Variable not Updating

Posted: Thu Nov 14, 2019 4:35 pm
by Tony Li
Glad you got it working! :-)