Saving on background thread

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Saving on background thread

Post by VoodooDetective »

I was just wondering if the save system uses a thread to save game data. I couldn't find any threading, and I want to make sure I don't cause hiccups by saving. Just curious if that's something I need to implement or not.
User avatar
Tony Li
Posts: 22158
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving on background thread

Post by Tony Li »

Hi,

Saving occurs in two phases:

1. Recording saved game data.

2. Storing saved game data to persistent storage (e.g., disk file).

For (1), in games with huge dialogue databases that use SimStatus, the operation that takes the longest is saving the Lua environment. But you can configure PersistentDataManager to run asynchronously, in which case it runs on the main thread but in a coroutine that batches a limited amount of activity in each frame. Generally speaking, though, this isn't enough to cause any hiccups so you rarely need to run PersistentDataManager asynchronously.

On the other hand, (2) can cause hiccups if you're writing to specialized storage such as cloud storage, since you may need to establish a network connection, do some handshaking, etc. Consoles in particular have a requirement that saves must run asynchronously or on a separate thread. SavedGameDataStorer has a method StoreSavedGameDataAsync that can store data asynchronously. The built-in saved game data storers save synchronously, but you can subclass them to kick off the save in a separate thread or coroutine.
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Re: Saving on background thread

Post by VoodooDetective »

Ahhh ok that all makes sense. I think I'll test this out on different devices before I start worrying about implementing async then. Thanks so much for all the information!
User avatar
Tony Li
Posts: 22158
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving on background thread

Post by Tony Li »

Sounds good! If you're saving to the cloud, you'll probably want to set up async. And if you're saving on a console platform, the platform requirements will almost certainly require it. But the nice thing about the save system is that, since it's modular, you can cross that bridge when you come to it.
bennett
Posts: 1
Joined: Wed Sep 01, 2021 12:26 pm

Re: Saving on background thread

Post by bennett »

We were trying to figure out why we couldn't get async saving to happen on this system, and wondering why PersistentDataManager.cs has a GetSaveDataAsync() method that is never called by anything. The code in SaveGameDataStorer was illuminating:

Code: Select all

        public virtual IEnumerator StoreSavedGameDataAsync(int slotNumber, SavedGameData savedGameData)
        {
            StoreSavedGameData(slotNumber, savedGameData);
            progress = 1;
            yield break;
        }
In other words, the system never does anything async, it just has async methods that are either never called or which are named async but just do everything all in one call. Was the idea that these things would be turned into async functions in some later update?
User avatar
Tony Li
Posts: 22158
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving on background thread

Post by Tony Li »

Hi,

No, the idea is that your custom subclass will implement StoreSavedGameDataAsync as needed for whatever platform you're on. The code will be different for each platform.
Post Reply