Custom Lua methods not available with DontDestroyOnLoad

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Passero
Posts: 32
Joined: Thu Jan 18, 2018 1:19 pm

Custom Lua methods not available with DontDestroyOnLoad

Post 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?
User avatar
Tony Li
Posts: 22059
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom Lua methods not available with DontDestroyOnLoad

Post 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.
Passero
Posts: 32
Joined: Thu Jan 18, 2018 1:19 pm

Re: Custom Lua methods not available with DontDestroyOnLoad

Post 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.
User avatar
Tony Li
Posts: 22059
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom Lua methods not available with DontDestroyOnLoad

Post by Tony Li »

Great! I'm glad it's working now.
Post Reply