Page 1 of 1

Conversation Variables won't update

Posted: Wed Apr 02, 2025 11:35 pm
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

Re: Conversation Variables won't update

Posted: Thu Apr 03, 2025 7:05 am
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.

Re: Conversation Variables won't update

Posted: Thu Apr 03, 2025 12:37 pm
by aadri3
Thank so much!!!! I've added so many scripts I must have forgotten to add this one. :?

Re: Conversation Variables won't update

Posted: Thu Apr 03, 2025 2:22 pm
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.