Conversation Variables won't update

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
aadri3
Posts: 2
Joined: Wed Apr 02, 2025 4:00 pm

Conversation Variables won't update

Post by aadri3 »

This is my first time working with the Dialogue System and I have been having some issues, finding the documentation hard to navigate so I am really at a loss.

I am basically trying to make a job aptitude quiz for a VR game and I can't seem to get the Lua function to register correctly. I've got it to work for other variables just fine but it just wont work, I've tested the code with taking only one variable instead of two and the problem persists! Any help would be greatly appreciated.

this is my code

Code: Select all

using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.DialogueSystem;

public class JobSelector : MonoBehaviour
{
    private Dictionary<string, float> jobScores = new Dictionary<string, float>()
    {
        { "Leader", 0 },
        { "Explorer", 0 },
        { "Midwife", 0 },
        { "Machinist", 0 },
        { "Farmer", 0 },
        { "Helper", 0 }
    };

    public string bestJob;
    public string worstJob;
    public float highScore;
    public float lowScore;

    void OnEnable()
    {

        Lua.RegisterFunction("UpdateJobScore", this, SymbolExtensions.GetMethodInfo(() => UpdateJobScore(string.Empty, (double)0)));
    }

    void OnDisable()
    {
        Lua.UnregisterFunction("UpdateJobScore");

    }

    public void ResetScores()
    {
        foreach (var key in jobScores.Keys)
        {
            jobScores[key] = 0;
        }

        highScore = 0;
        lowScore = 0;
        bestJob = "";
        worstJob = "";
    }

    
    public void UpdateJobScore(string job, double amount)
    {
        float score = (float)amount;

        if (jobScores.ContainsKey(job))
        {
            jobScores[job] += (float)score;
            CalculateScores();
        }
        else
        {
            Debug.LogError("Invalid job name: " + job);
        }

    }



    private void CalculateScores()
    {
        highScore = float.MinValue;
        lowScore = float.MaxValue;

        foreach (var job in jobScores)
        {
            if (job.Value > highScore)
            {
                highScore = job.Value;
                bestJob = job.Key;
            }

            if (job.Value < lowScore)
            {
                lowScore = job.Value;
                worstJob = job.Key;
            }
        }

        // Store best & worst job in Dialogue System
        DialogueLua.SetVariable("BestJob", bestJob);
        DialogueLua.SetVariable("WorstJob", worstJob);
    }


}
and this is the error I am getting consistently
Lua code 'UpdateJobScore("Explorer", 2)' threw exception 'Tried to invoke a function call on a non-function value. If you're calling a function, is it registered with Lua?'
UnityEngine.Debug:LogError (object)
PixelCrushers.DialogueSystem.Lua:RunRaw (string,bool,bool) .... etc a bunch of file locactions that make this post look like spam
User avatar
Tony Li
Posts: 23254
Joined: Thu Jul 18, 2013 1:27 pm

Re: Conversation Variables won't update

Post by Tony Li »

Hi,

Did you add your JobSelector script to a GameObject in your scene?

If you temporarily set the Dialogue Manager's Other Settings > Debug Level to Info, and play in the Unity editor's play mode, it will log when C# methods get registered and unregistered from Lua.
aadri3
Posts: 2
Joined: Wed Apr 02, 2025 4:00 pm

Re: Conversation Variables won't update

Post by aadri3 »

Thank so much!!!! I've added so many scripts I must have forgotten to add this one. :?
User avatar
Tony Li
Posts: 23254
Joined: Thu Jul 18, 2013 1:27 pm

Re: Conversation Variables won't update

Post by Tony Li »

Glad to help!

If you notice any issues -- for example, if you add it to a "persistent singleton" GameObject such as the Dialogue Manager instead of a regular scene GameObject -- take a look at the CustomLuaTemplate.cs starter script for more tips.
Post Reply