Page 1 of 1

Custom Lua methods not available with DontDestroyOnLoad

Posted: Sun May 06, 2018 3:41 pm
by Passero
I use custom Lua methods that are called from the Dialogue system but I notice when I move between scenes, these methods are no longer available.

For example I have a gameObject that has following code:

Code: Select all

void OnEnable() {
      
        Lua.RegisterFunction("AddItem", this, SymbolExtensions.GetMethodInfo(() => addItem(string.Empty, (double)0)));
        Lua.RegisterFunction("HasItem", this, SymbolExtensions.GetMethodInfo(() => hasItem(string.Empty, (double)0)));
    }

    void OnDisable() {
        Lua.UnregisterFunction("AddItem");
        Lua.UnregisterFunction("HasItem");
    }
That gameObject also is configured with DontDestroyOnLoad.

When I load another scene and the Dialogue system needs to access this method, I get following stack:

Code: Select all

Dialogue System: Lua code 'return CurrentQuestState("GrowCorn") == "active" and HasItem("CORN",5) == false' 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, Boolean, Boolean)
PixelCrushers.DialogueSystem.Lua:Run(String, Boolean, Boolean)
PixelCrushers.DialogueSystem.Lua:IsTrue(String, Boolean, Boolean)
PixelCrushers.DialogueSystem.ConversationModel:EvaluateLinksAtPriority(ConditionPriority, DialogueEntry, List`1, List`1, List`1, Boolean)
PixelCrushers.DialogueSystem.ConversationModel:EvaluateLinks(DialogueEntry, List`1, List`1, List`1, Boolean)
PixelCrushers.DialogueSystem.ConversationModel:GetState(DialogueEntry, Boolean, Boolean, Boolean)
PixelCrushers.DialogueSystem.ConversationModel:GetState(DialogueEntry)
PixelCrushers.DialogueSystem.ConversationController:OnFinishedSubtitle(Object, EventArgs)
PixelCrushers.DialogueSystem.ConversationView:FinishSubtitle()
PixelCrushers.DialogueSystem.ConversationView:OnFinishedSubtitle()
PixelCrushers.DialogueSystem.Sequencer:FinishSequence()
PixelCrushers.DialogueSystem.Sequencer:Update()
The first time around, everything is working fine.

What am I doing wrong?

Re: Custom Lua methods not available with DontDestroyOnLoad

Posted: Sun May 06, 2018 5:17 pm
by Tony Li
Is it possible that the GameObject with that script isn't actually sticking around? Or maybe the script is also on a second GameObject that's getting disabled/destroyed?

You could add a Debug.Log call to OnDisable to see if it's getting called. Or set the Dialogue Manager's Debug Level to Info, which will log when Lua functions get registered and unregistered.

Re: Custom Lua methods not available with DontDestroyOnLoad

Posted: Sun May 06, 2018 5:35 pm
by Passero
Ah yes it does work now.

I had this in my code:

Code: Select all

if (FindObjectsOfType(GetType()).Length > 1) {
            Destroy(gameObject);
        }
to deal with duplicates when I get back to my original scene.
This was deregistering the methods so I added the same if method around it make sure he's only deregistering them if it's the only instance.

Re: Custom Lua methods not available with DontDestroyOnLoad

Posted: Sun May 06, 2018 5:57 pm
by Tony Li
Great! I'm glad it's working now.