Page 1 of 1

Registered Custom Function Stops Working After Scene Change

Posted: Sun Aug 11, 2024 11:42 pm
by 2linescrossed
Hello!
Currently dealing with a strange issue with a custom function I've registered.

Basically, it grabs a reference to the instanced Sound Manager Singleton in order to load & play a sound from the Resourced folder.
I've tested the script & confirmed that it works - but ONLY on the first scene that the DialogueManager exists in.

Code: Select all

    public void PlaySFX(string sfxName)
    {
        AudioClip sound = Resources.Load<AudioClip>($"Sounds/{sfxName}");
        AudioManager.Instance.PlaySound(sound);
        Debug.Log("Playing sound via lua script");
    }
I figure it's happening because the DialogueManager doesn't know how to get the AudioManager when scenes change - the debug one I'm using has the Dialogue & Audio Manager exist at the same time, whereas the one in the real game only has the Dialogue Manager show up later after loading into the game. The problem is, since this is wired into the DialogueManager itself via the CustomLuaFunction info & the Dialogue nodes, I don't actually now how to 'give' the DialogueManager the AudioManager's reference again.
There's also the issue that triggering this script efect doesn't show the Debug.Log at all, so it's in this strange realm of encapsulation I can't see into to diagnose the issue.

If you could give me some direction, that would be appreciated!

Re: Registered Custom Function Stops Working After Scene Change

Posted: Mon Aug 12, 2024 8:25 am
by Tony Li
Hi,

Is the script that register your Lua function is on the Dialogue Manager, register it in Awake, OnEnable, or Start, but don't unregister it (e.g., in OnDisable or OnDestroy). When you change scenes into a scene with another Dialogue Manager, the original Dialogue Manager from the first scene will destroy the new Dialogue Manager to ensure there's only one. This will call OnDisable and OnDestroy methods in the scripts on the new Dialogue Manager, which will inadvertently unregister your function.

If that doesn't resolve it, please let me know what the error message is.

Re: Registered Custom Function Stops Working After Scene Change

Posted: Tue Aug 13, 2024 7:12 am
by 2linescrossed
Hi Tony, thanks for the suggestion!
It turns out that I did in fact have something screwed up with the DialogueManager not having the script on it properly.
I guess part of me thought that if the function was registered, it would be registered globally, but that's not the case.
I fixed it simply by re-adding the script to the core dialogue manager. Thanks!

Re: Registered Custom Function Stops Working After Scene Change

Posted: Tue Aug 13, 2024 8:16 am
by Tony Li
Hi,

Lua functions are registered globally. But that means they can also be unregistered globally (i.e., from another instance of the script on a different Dialogue Manager instance ).