Properly Disposing DS Through Code
Properly Disposing DS Through Code
Title says it all!
In our game we are using a combination of Adventure Creator and Dialogue System. During the game we are transitioning from AC using DS to another mini-game we have created that uses neither. I am wondering what is the appropriate way to save and destroy the instance of DS for maximum clean up potential. Additionally, if anyone knows how to do the same for Adventure Creator that would be awesome, however I am going to go ahead and post on their forums as well regarding that.
Any help is greatly appreciated!
In our game we are using a combination of Adventure Creator and Dialogue System. During the game we are transitioning from AC using DS to another mini-game we have created that uses neither. I am wondering what is the appropriate way to save and destroy the instance of DS for maximum clean up potential. Additionally, if anyone knows how to do the same for Adventure Creator that would be awesome, however I am going to go ahead and post on their forums as well regarding that.
Any help is greatly appreciated!
Re: Properly Disposing DS Through Code
That's a good question! I'm surprised no one's asked it before, since there is a little something you can do for maximum cleanup potential.
For the most part, just destroy the Dialogue Manager GameObject. This will clean almost everything up.
However, the Lua environment exists separately from the Dialogue Manager. To clear the contents of the Lua environment, call:
Keep in mind that destroying the Dialogue Manager and clearing Lua will get rid of all of your Dialogue System runtime data. If you weren't using Adventure Creator, I'd recommend serializing the data first using:
Then save the data string somewhere. When you bring back the Dialogue System, you can reapply the data using:
Since you're using Adventure Creator, you may just want to save your AC game instead. The Dialogue System ties into AC's save system, so it does that GetSaveData()/ApplySaveData() stuff behind the scenes for you.
Instead of all of this, you may find it easier to keep the Dialogue Manager in memory during the whole play session. The biggest memory consumer may be your dialogue UI, with all its textures. You can destroy it when you switch to the minigame. Just remember to reinstantiate it and assign the instance to DialogueManager.DialogueUI before running any conversations or alerts. If you're using legacy Unity GUI, you can assign a prefab; you don't have to instantiate it.
For the most part, just destroy the Dialogue Manager GameObject. This will clean almost everything up.
However, the Lua environment exists separately from the Dialogue Manager. To clear the contents of the Lua environment, call:
Code: Select all
DialogueLua.InitializeChatMapperVariables();
Code: Select all
string data = PersistentDataManager.GetSaveData();
Code: Select all
PersistentDataManager.ApplySaveData(data);
Instead of all of this, you may find it easier to keep the Dialogue Manager in memory during the whole play session. The biggest memory consumer may be your dialogue UI, with all its textures. You can destroy it when you switch to the minigame. Just remember to reinstantiate it and assign the instance to DialogueManager.DialogueUI before running any conversations or alerts. If you're using legacy Unity GUI, you can assign a prefab; you don't have to instantiate it.
Re: Properly Disposing DS Through Code
Thanks for the detailed response. We are definitely going to look into this. I would like to get the most memory that I can squeeze out as possible by clearing as much as possible. After switching to the non-AC minigame we will be coming back to the main game. As such would you say the following steps are correct in terms of order of operations.
- Save AC Game First
- Clear LUA Environment. This part should be safe since we saved the AC game first correct?
- Finally destroy the Dialogue Manager GameObject to free up texture memory and DS itself
Re: Properly Disposing DS Through Code
Yup, your Lua is safe if you save the AC game first. That's a good order of operations. You might not need to destroy the Dialogue Manager manually (see below).Clear LUA Environment. This part should be safe since we saved the AC game first correct?
The Dialogue Manager (actually the Dialogue System Controller component on it) has two relevant checkboxes:Additionally each one of our scenes has a Dialogue Manager GameObject in it by default. Would you consider this a good approach so that if we accidentally forget to initialize it will still be there? Presumably if there are two DS instances DS destroys the other?
- Dont Destroy On Load
- Allow Only One Instance
If Allow Only One Instance is ticked, when the Dialogue Manager starts, it will look for an existing Dialogue Manager. If it finds an existing Dialogue Manager, it will destroy itself, leaving only the original Dialogue Manager.
If the Dialogue Manager sticks around (either because the checkbox is unticked or because there's no pre-existing Dialogue Manager), the Dialogue Manager will clear the Lua environment and populate it with its Initial Database info. You can repopulate it with the runtime values by grabbing the AC variable that holds the Dialogue System saved data, and run it through Lua.Run(). (The AC variable actually contains Lua code to recreate the saved Lua environment.) This is why it might be easiest to keep the Dialogue Manager around. You don't need to fiddle with repopulating Lua.
A lot of devs tick both checkboxes (which is the default) and still add a Dialogue Manager to each scene. This lets them test the Dialogue System in that scene without having to come from another scene that has the Dialogue Manager. But if you are coming from another scene with an existing Dialogue Manager, that Dialogue Manager will continue to live and destroy the new scene's Dialogue Manager, keeping all your Lua data happily intact.
Re: Properly Disposing DS Through Code
Sounds good. We've actually been doing that for testing as well so we don't have to keep coming from other scenes.
I will switch our order of operations to the following then after setting Dialogue Manager GameObject to Don't Destroy on Load and Allow Only Instance
I will switch our order of operations to the following then after setting Dialogue Manager GameObject to Don't Destroy on Load and Allow Only Instance
- Save AC Game first
- Clear LUA Environment
- Get rid of the UI but keep the Manager around an re-initialize the UI
Re: Properly Disposing DS Through Code
Don't clear the Lua environment out from underneath the Dialogue Manager unless you repopulate Lua before doing anything Dialogue System-related.
Try this:
Another way to save memory is to untick Include Sim Status. The default is unticked. When this is ticked, the Lua environment holds a field for every single dialogue entry that keeps track of whether it's been offered in a response menu or spoken by a character. Unless you're using this info, you might as well untick it.
Try this:
- Save AC Game first
- Clear Lua
- Destroy the Dialogue Manager
- When returning to a DS scene that has a Dialogue Manager, feed the AC variable to Lua.Run() after one frame (to allow the Dialogue Manager to run its Start method first)
- (Don't bother to save; don't clear Lua)
- Get rid of the UI but keep the Dialogue Manager and Lua
- When returning to the DS scene, instantiate the UI and assign it to DialogueManager.DialogueUI
- Nothing (just keep it all in memory)
Another way to save memory is to untick Include Sim Status. The default is unticked. When this is ticked, the Lua environment holds a field for every single dialogue entry that keeps track of whether it's been offered in a response menu or spoken by a character. Unless you're using this info, you might as well untick it.
Re: Properly Disposing DS Through Code
Tony,
We were able to free up around 30 - 40mb of RAM using this approach! Thanks a lot!
We were able to free up around 30 - 40mb of RAM using this approach! Thanks a lot!
Re: Properly Disposing DS Through Code
Great! Was it mostly in UI textures?
Re: Properly Disposing DS Through Code
I'm not 100% sure tbh. I would say that a larger portion is more likely from the textures then clearing the LUA. We did have quite a few UI elements.