Calling Method with Lua

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
2538050250
Posts: 2
Joined: Mon Nov 14, 2022 12:13 pm

Calling Method with Lua

Post by 2538050250 »

Hi,

I'm trying to call a Method in a conversation to Open a Shop in another script :

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



public class ShopTrigger : MonoBehaviour
{
public ItemData[] itemsToSell;

public void TriggerShop()
{
ShopManager.instance.OpenShop(itemsToSell);
}

void OnEnable()
{

Lua.RegisterFunction("TriggerShop", this, SymbolExtensions.GetMethodInfo(() => TriggerShop()));
}

void OnDisable()
{

Lua.UnregisterFunction("TriggerShop");

}



As You can see, I use Item data to keep track of the items the NPC has to sell, and I wanted to trigger the shop a conversation. Using the tutorial on Youtube, that's what I came to, but in the console, as I run the game, I get this :

Dialogue System: Lua code ' ShopTrigger()' threw exception 'Tried to invoke a function call on a non-function value. If you're calling a function, is it registered with Lua?'

Can you tell me what I did wrong ?

Thank you very much,

Jean
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Calling Method with Lua

Post by Tony Li »

Hi,

You've registered the function name "TriggerShop()", but your conversation's Script field is trying to call "ShopTrigger()".


Side note: You can only register one function under a given name. So you can't register separate functions both named "TriggerShop" for NPC1 and also for NPC2. Here are two solutions to get around that:

1. Add the NPC's name to the function. Example:

Code: Select all

Lua.RegisterFunction(name + "_TriggerShop", this, SymbolExtensions.GetMethodInfo(() => TriggerShop()));
If the script is on a GameObject named NPC1, the function will be NPC1_TriggerShop(). Set your conversation node's Script field to: NPC1_TriggerShop()


2. Or move the Lua function to a separate script that can work with any NPC. Put it on your ShopManager or Dialogue Manager GameObject.

Code: Select all

public class ShopLua : MonoBehaviour
{
    public void TriggerShop(string npcName)
    {
        var npc = GameObject.Find(npcName);
        if (npc != null)
        {
            var shop = GetComponent<Shop>();
            if (shop != null)
            {
                ShopManager.instance.OpenShop(shop.itemsToSell);
            }
        }
    }

    void Awake()
    {
        Lua.RegisterFunction("TriggerShop", this, SymbolExtensions.GetMethodInfo(() => TriggerShop("")));
    }
}
On the NPCs:

Code: Select all

public class Shop : MonoBehaviour
{
    public ItemData[] itemsToSell;
}
2538050250
Posts: 2
Joined: Mon Nov 14, 2022 12:13 pm

Re: Calling Method with Lua

Post by 2538050250 »

Hi,
Thanks for your quick reply,, I, indeed didn't notice I was using the wrong command. It works fine now.
Thank you for warning me about the potential registering issue, I'll keep that in mind in the future.
Thanks again for your help,
Have a nice day,

Jean
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Calling Method with Lua

Post by Tony Li »

Glad to help!
Post Reply