- Home /
Saving data question
Hi. I'm trying to save data whenever the player triggers a checkpoint, but the process takes 0.2 seconds or so and causes a lag spike that players shouldn't see. How can I make it so the process works without stopping everything else?
Answer by TheSOULDev · Sep 12, 2017 at 12:13 AM
Try using a Coroutine or saving only specific stuff. Even though the Coroutine approach will most likely fix it, you could create properties for every variable save that could run some kind of function that registers that a value has been changed. When saving, you can then deserialize, update only the values that were changed, and then serialize. Alternatively, you could write your own serializer as people generally consider C#'s default serializers to be unnecessarily slow.
The thing is I'm using Easy Save and Inventory pro, they work with each other fine, I think they use serializers, at least Inventory Pro does, I'm not really pro at coding but I can probably understand a line when I read it. What I can't find is any info about using coroutines to make a process work on many frames. I mean, practically every game saves data, but I can't find any info or tutorial about how they do it. The only thing I need is for the saving to be done on the background, and I would show a logo at the top of the screen until it finishes (like the fire in dark souls or the logo in borderlands 2).
Also I kinda had it working with a thread, but every time I activated the saving, it would save twice, then thrice. I used thread.Start() and I think that was the problem. Also I read that using threads in unity isn't safe, so I don't know if I should be doing that.
I would do what TheSOULDev said, Unity does not handle $$anonymous$$ulti Threading
Your answer
Follow this Question
Related Questions
Threads? Or just Coroutines? 0 Answers
Random lag spikes any time processor intensive tasks a scene 0 Answers
Alternative for semaphores in Unity? 2 Answers
NetworkStream.GetStream() Coroutine 1 Answer
Instantiate + Coroutine = problem 0 Answers