How to Reset the Lua Environment?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
WeiYiHua
Posts: 16
Joined: Mon Mar 25, 2024 10:27 pm

How to Reset the Lua Environment?

Post by WeiYiHua »

Hello!

I’m using Dialogue System, and I’ve noticed that after exiting and loading a saved game, many unrelated assets remain loaded in memory. These include Texture, SpriteAtlas, DialogueDatabase instances, etc. After some investigation, I found that even after exiting the saved game and unloading the scene, some static dictionaries in the Lua environment are still holding references to these objects.

I tried calling methods similar to InitStaticVariables in the LuaInterpreter and Lua classes during unloading. As a result, those assets were no longer referenced and could be unloaded properly. (Calling DatabaseManager.Clear alone did not have this effect.)

Did I miss anything? If calling methods like InitStaticVariables is the correct approach, could you consider adding some similar public methods? Thank you!
User avatar
Tony Li
Posts: 23388
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to Reset the Lua Environment?

Post by Tony Li »

Hi,

Depending on what you want to clear, you can call DialogueManager.RemoveDatabase() to remove databases from memory that you added during play, or call SaveSystem.ResetGameState() to clear the saved game data in memory -- however, you shouldn't need this latter method if you're loading a saved game, since that will also clear and reload the saved game data.
WeiYiHua
Posts: 16
Joined: Mon Mar 25, 2024 10:27 pm

Re: How to Reset the Lua Environment?

Post by WeiYiHua »

Thank you for your response regarding saving and loading. That said, I wanted to point out that the main issue seems to occur after calling DatabaseManager.Reset, where some old references in the Lua environment are not fully cleared.

As described earlier, these lingering references are only completely cleaned up when methods similar to InitStaticVariables in the LuaInterpreter and Lua classes are called.
WeiYiHua
Posts: 16
Joined: Mon Mar 25, 2024 10:27 pm

Re: How to Reset the Lua Environment?

Post by WeiYiHua »

I think this issue could be considered a bug.
User avatar
Tony Li
Posts: 23388
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to Reset the Lua Environment?

Post by Tony Li »

I agree. I'll address this in version 2.2.56.
WeiYiHua
Posts: 16
Joined: Mon Mar 25, 2024 10:27 pm

Re: How to Reset the Lua Environment?

Post by WeiYiHua »

Looking forward to your fix!
Maybe some internal functions in Dialogue System are not being unregistered from Lua?

In my case, calling InitStaticVariables resolves the issue, since I also unregister all the Lua functions I registered when unloading the scene. However, other users might still want to use Lua functions after a scene has been unloaded.

In any case, this bug needs to be handled carefully — or perhaps there could be a method that allows users to manually reset the Lua environment when needed.
Post Reply