Page 1 of 1

Weird problem with LUA running partial method?

Posted: Thu Jun 03, 2021 11:57 am
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.

Re: Weird problem with LUA running partial method?

Posted: Thu Jun 03, 2021 1:49 pm
by Tony Li
Hi,

Did it log any errors or warnings to the Console window?

Re: Weird problem with LUA running partial method?

Posted: Thu Jun 03, 2021 2:25 pm
by Tentakero
No errors logged either.

Re: Weird problem with LUA running partial method?

Posted: Thu Jun 03, 2021 3:21 pm
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?

Re: Weird problem with LUA running partial method?

Posted: Thu Jun 03, 2021 5:00 pm
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!

Re: Weird problem with LUA running partial method?

Posted: Thu Jun 03, 2021 5:06 pm
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");

Re: Weird problem with LUA running partial method?

Posted: Thu Jun 03, 2021 5:32 pm
by Tentakero
Oh that helps a ton! I'll be using the list method to track them from now on. Thanks again!

Re: Weird problem with LUA running partial method?

Posted: Thu Jun 03, 2021 8:08 pm
by Tony Li
Glad to help!