Restorestate

I dont understand how restorestate gets the state object or why we must assume it is of the same type returned by capturestate. I understand something is going on behind the scenes here we dont know about. Is it possible to get a quick explanation?

Welcome to the community, @Alex99

When the saving system loads a save, it finds all the SaveableEntity objects in the scene. It then loops through each one and calls RestoreState on it, passing the same object (unless you changed something in the code) into this function that it received when it originally saved it.

The SaveableEntity in turn expects this to be a Dictionary<string, object> because this is what it provided when CaptureState was called, so it casts the state to Dictionary<string, object> and processes this.

When processing, it does a similar thing as the saving system: It gets all the components on the game object that implements ISaveable and loops through each one. The key of the dictionary matches each ISaveable and the value (object) is the same data that this ISaveable supplied when CaptureState was called on it. Therefore it is relatively safe for the ISaveable to assume that the object it is receiving in RestoreState is the same as the object it sent in CaptureState.

This, of course, breaks when we make changes to the data we save which is why we sometimes have to delete the save file after we made changes

So, in summary, the RestoreState receives the same data from the saving system that it originally sent in CaptureState, and this is why we can assume it is of the same type.

It’s a mouthful, so don’t hesitate to ask for more clarity if you still don’t understand. I know I said a lot of things

1 Like

Jepp that cleared things up. thank you so much!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms