Weird problem with LUA running partial method?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Tentakero
Posts: 48
Joined: Wed Sep 23, 2020 12:36 pm

Weird problem with LUA running partial method?

Post by Tentakero »

Hello, I'm trying to wrap my head around this. I seem to have fixed the issue but I don't understand why it was happening.

For some reason, when I tried to execute this method through LUA, it would only run until the first debug log and never do the rest of the loop.

Code: Select all

public void DespawnNPC(string NPCName)
    {
        Debug.Log(NPCName); //This prints in console normally, string verified to be correct.
        for (int i = 0; i < sceneNPCData.Count; i++) //This doesn't run through LUA but runs otherwise.
        {
            if (sceneNPCData[i].NPCName == NPCName)
            {
                Debug.Log("NPC Name Match found in list of scene NPC. Despawning +" + NPCName + "On screen fade.");
                sceneNPCData[i].gameObject.AddComponent<ObjectDespawner>();
                sceneNPCData[i].gameObject.GetComponent<ObjectDespawner>().SubscribeToFader();
                break;
            }
        }
    }
I solved the problem by changing the LUA method registration from Awake() to Start(), but I'm wondering why it was happening as I would like to avoid running into an issue like this later on.
User avatar
Tony Li
Posts: 22152
Joined: Thu Jul 18, 2013 1:27 pm

Re: Weird problem with LUA running partial method?

Post by Tony Li »

Hi,

Did it log any errors or warnings to the Console window?
Tentakero
Posts: 48
Joined: Wed Sep 23, 2020 12:36 pm

Re: Weird problem with LUA running partial method?

Post by Tentakero »

No errors logged either.
User avatar
Tony Li
Posts: 22152
Joined: Thu Jul 18, 2013 1:27 pm

Re: Weird problem with LUA running partial method?

Post by Tony Li »

Does the same method that registers the DespawnNPC with Lua also cache sceneNPCData? Maybe it's not valid in Awake but it is valid in Start.

If you want to pursue this further, try changing that first Debug.Log to:

Code: Select all

Debug.Log(NPCName + ", sceneNPCData.Count=" + sceneNPCData.Count);
and move the registration back to Awake. What does it report for sceneNPCData.Count?

Can you send a reproduction project to tony (at) pixelcrushers.com?
Tentakero
Posts: 48
Joined: Wed Sep 23, 2020 12:36 pm

Re: Weird problem with LUA running partial method?

Post by Tentakero »

Bah! I found the problem. I tried the debug stuff you mentioned but I realized it's because I had forgotten to add the UnresgisterFunction OnDisable(). During testing, I was changing scenes and I'm guessing the same LUA function was registering twice without ever being removed. I'm not sure at what point I ended up adding the OnDisable but it must have been when I was trying out the switch from Awake to Start since it's working as intended on both of them now.

Is there anywhere to check which LUA functions are registered at any given time or is that something not visible? In any case, thanks for the help!
User avatar
Tony Li
Posts: 22152
Joined: Thu Jul 18, 2013 1:27 pm

Re: Weird problem with LUA running partial method?

Post by Tony Li »

It's not visible. They get added as if they're native Lua functions, so it doesn't distinguish them from built-in Lua functions such as math.sqrt() and string.lower(). You can, however, manually keep track of your own Lua functions that you've registered. Example:

Code: Select all

public List<string> myRegisteredFunctions = new List<string>();
...
Lua.RegisterFunction("GameObjectExists", this, SymbolExtensions.GetMethodInfo(() => GameObjectExists(string.Empty)));
myRegisteredFunctions.Add("GameObjectExists");
...
Lua.UnregisterFunction("GameObjectExists");
myRegisteredFunctions.Remove("GameObjectExists");
Tentakero
Posts: 48
Joined: Wed Sep 23, 2020 12:36 pm

Re: Weird problem with LUA running partial method?

Post by Tentakero »

Oh that helps a ton! I'll be using the list method to track them from now on. Thanks again!
User avatar
Tony Li
Posts: 22152
Joined: Thu Jul 18, 2013 1:27 pm

Re: Weird problem with LUA running partial method?

Post by Tony Li »

Glad to help!
Post Reply