Changing Variable not Updating

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
slothins
Posts: 13
Joined: Thu Oct 24, 2019 11:18 am

Changing Variable not Updating

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

Re: Changing Variable not Updating

Post 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.
slothins
Posts: 13
Joined: Thu Oct 24, 2019 11:18 am

Re: Changing Variable not Updating

Post by slothins »

Perfect, thanks! It was the way I was calling the variable in the dialogue tree that was wrong.
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Changing Variable not Updating

Post by Tony Li »

Glad you got it working! :-)
Post Reply