Problem with registering variables/functions

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
pschroed
Posts: 20
Joined: Sun Mar 05, 2023 12:15 pm

Problem with registering variables/functions

Post by pschroed »

Hi,

so I am developing an adventure game where you have several stats which should increase after certain conversations.

I read up and watched the tutorial about registering variables and accustomed the template accordingly.

The code looks something like this:

Code: Select all

  
using UnityEngine;
using PixelCrushers.DialogueSystem;
using TMPro;
using UnityEngine.UI;


public class PlayerStats : MonoBehaviour // Rename this class.
{

    public int strenght = 15;
    public int intelligence = 15;
    public int MartialArts = 15;
    public int energy = 100;

    [SerializeField] TextMeshProUGUI strenghtvalue;
    [SerializeField] TextMeshProUGUI intelligencevaulue;
    [SerializeField] TextMeshProUGUI martialArtsvalue;
    [SerializeField] TextMeshProUGUI energyvalue;

    private void Update()
    {
        strenghtvalue.text = strenght.ToString();
        intelligencevaulue.text = intelligence.ToString();
        martialArtsvalue.text = MartialArts.ToString();
        energyvalue.text = energy.ToString();
    }

    
    public void GiveStrenght(double amount)
    {
        strenght += (int)amount;
    }
    public void GiveIntelligence(double amount)
    {
        intelligence += (int)amount;
    }
    public void GiveMartialArts(double amount)
    {
        MartialArts += (int)amount;
    }
    public void GiveEnery(double amount)
    {
        energy += (int)amount;
    }
    void OnEnable()
    {
        Lua.RegisterFunction("GiveStrenght", this, SymbolExtensions.GetMethodInfo(() => GiveStrenght((double)0)));
        Lua.RegisterFunction("GiveIntelligence", this, SymbolExtensions.GetMethodInfo(() => GiveIntelligence((double)0)));
        Lua.RegisterFunction("GiveMartialArts", this, SymbolExtensions.GetMethodInfo(() => GiveMartialArts((double)0)));
        Lua.RegisterFunction("GiveEnergy", this, SymbolExtensions.GetMethodInfo(() => GiveEnery((double)0)));
    }
}
The script is attached to the dialogue manager.

The problem is that I can't add these functions. I created a New Custom Lua Function and added them like shown in the tutorial video. But when i click on conditions on the specific dialogue node nothing show up under custom.

https://ibb.co/wdj1t2N
https://ibb.co/qgtwyJ3

Thanks
User avatar
Tony Li
Posts: 22133
Joined: Thu Jul 18, 2013 1:27 pm

Re: Problem with registering variables/functions

Post by Tony Li »

Hi,

You added the functions to the Script Functions section of your Custom Lua Function Info asset, which is correct.

But you're opening the Conditions field's dropdown, which will only show Condition Functions. Click Revert to Cancel that. Then click the Script field's "..." button.
pschroed
Posts: 20
Joined: Sun Mar 05, 2023 12:15 pm

Re: Problem with registering variables/functions

Post by pschroed »

Thanks that worked.

Another question:

How do you register a simple bool variable?

I want the player only to be able to increase the intelligence up to 30. So I wrote a small method which checks the intelligence and sets the boolean variable IntelligenceunderThirty to false when the intelligence is 30 or higher.

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;
using TMPro;
using UnityEngine.UI;


public class PlayerStats : MonoBehaviour // Rename this class.
{

    public int strenght = 15;
    public int intelligence = 15;
    public int MartialArts = 15;
    public int energy = 100;

    [SerializeField] TextMeshProUGUI strenghtvalue;
    [SerializeField] TextMeshProUGUI intelligencevaulue;
    [SerializeField] TextMeshProUGUI martialArtsvalue;
    [SerializeField] TextMeshProUGUI energyvalue;

    public bool intelligenceunderThirty;

    private void Update()
    {
        strenghtvalue.text = strenght.ToString();
        intelligencevaulue.text = intelligence.ToString();
        martialArtsvalue.text = MartialArts.ToString();
        energyvalue.text = energy.ToString();
        Intelligenceunderthirty();
    }

    public void Intelligenceunderthirty()
    {
        if (intelligence < 30)
        {
            intelligenceunderThirty = true;
        }
        else
        {
            intelligenceunderThirty = false;
        }

    }
But how do I register that boolean?

It's not the same as the robe example.

Thanks
User avatar
Tony Li
Posts: 22133
Joined: Thu Jul 18, 2013 1:27 pm

Re: Problem with registering variables/functions

Post by Tony Li »

Hi,

Write a C# method and register that:

Code: Select all

bool IsIntelligenceUnder30() { return intelligence < 30; }

...
Lua.RegisterFunction("IsIntelligenceUnder30", this, SymbolExtensions.GetMethodInfo(() => IsIntelligenceUnder30()));
pschroed
Posts: 20
Joined: Sun Mar 05, 2023 12:15 pm

Re: Problem with registering variables/functions

Post by pschroed »

Thanks. I registered that.

But somehow it throws me a TargetParameterCountException Error

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;
using TMPro;
using UnityEngine.UI;


public class PlayerStats : MonoBehaviour // Rename this class.
{

    public int strenght = 15;
    public int intelligence = 15;
    public int MartialArts = 15;
    public int energy = 100;

    [SerializeField] TextMeshProUGUI strenghtvalue;
    [SerializeField] TextMeshProUGUI intelligencevaulue;
    [SerializeField] TextMeshProUGUI martialArtsvalue;
    [SerializeField] TextMeshProUGUI energyvalue;

    public bool intelligenceunderThirty;

    private void Update()
    {
        strenghtvalue.text = strenght.ToString();
        intelligencevaulue.text = intelligence.ToString();
        martialArtsvalue.text = MartialArts.ToString();
        energyvalue.text = energy.ToString();
    }

    bool IsIntelligenceUnder30() { return intelligence < 30; }





    public void GiveStrenght(double amount)
    {
        strenght += (int)amount;
    }
    public void GiveIntelligence(double amount)
    {
        intelligence += (int)amount;
    }

    public void GiveMartialArts(double amount)
    {
        MartialArts += (int)amount;
    }
    public void GiveEnery(double amount)
    {
        energy += (int)amount;
    }
    void OnEnable()
    {
        Lua.RegisterFunction("GiveStrenght", this, SymbolExtensions.GetMethodInfo(() => GiveStrenght((double)0)));
        Lua.RegisterFunction("GiveIntelligence", this, SymbolExtensions.GetMethodInfo(() => GiveIntelligence((double)0)));
        Lua.RegisterFunction("GiveMartialArts", this, SymbolExtensions.GetMethodInfo(() => GiveMartialArts((double)0)));
        Lua.RegisterFunction("GiveEnergy", this, SymbolExtensions.GetMethodInfo(() => GiveEnery((double)0)));
        Lua.RegisterFunction("IsIntelligenceUnder30", this, SymbolExtensions.GetMethodInfo(() => IsIntelligenceUnder30()));
    }
}
I think it has something to do how I register the function under New Custom Lua Function. I register the Element 0 as a Bool because I don't have to enter a string or number like in the tutorial example.

https://ibb.co/m6r16Rr
https://ibb.co/qnTGsrn
User avatar
Tony Li
Posts: 22133
Joined: Thu Jul 18, 2013 1:27 pm

Re: Problem with registering variables/functions

Post by Tony Li »

In your Custom Lua Function Info, remove the Bool element from IsIntelligenceUnder30's Parameters list. IsIntelligenceUnder30() doesn't accept any parameters.

For Conditions, try just:

Code: Select all

IsIntelligenceUnder30()
Post Reply