Custom Saving/Loading
Last updated
Was this helpful?
Last updated
Was this helpful?
It is quite easy to expand upon the existing save/load system, thanks to our and the power of Instanced Structs!
Lets demonstrate this with a few examples.
For this example, we're going to add a new variable to our Character blueprint (B_Soulslike_Character). You can also add it to any component you want.
Next, we'll add some example events to alter/view its value:
Now to save this variable, all we need to do is trigger the RequestAddToSaveData() message in our Player Controller (PC_SoulslikeFramework):
This message has 2 inputs. It might look a bit overwhelming at hindsight. However it provides a great amount of modularity when it gets to saving/loading data:
Save Tag (Gameplay Tag): Tag used for tracking save entry. Can be used to find data by tag and/or update/remove save entry data.
Data[] (Instanced Struct): The actual data that is going to be saved/loaded.
In our case, we'll create an array which contains a single element - the value of our new property "MyVariable". This will update our save data and trigger an autosave whenever we change this value. We should also make the saving part a custom event if we want to call it from anywhere else.
SerializeDataForSaving(): Serializes a specific data by tag and adds it to the final save data.
SerializeAllDataForSaving(): Serializes all possible data and sets it to the final save data.
You're all set! Now our new property will get saved whenever it's value has changed. Additionally, it will also get saved whenever we bulk-save data (specifically on quits/crashes etc).
The reason it takes a few extra steps to save a simple property in Soulslike Framework is due to the save system being designed for handling more complex sorts of data.
You can always check out the existing saving/loading functionality for more information.
Loading the saved data is as easy as this:
For this example, we're going to create an actor which adjusts its scale when player overlaps its trigger. Then we'll save its "overlapped" state and new scale.
Lets start by creating a new actor and setting it up accordingly:
Next, we'll need a new Struct to keep track of the 3 properties this actor has:
Id (GUID): the unique identifier of the actor (important)
Scale (Vector): the 3d scale of the actor
HasBeenOverlapped (Bool): the state of the actor
And now, when the timeline for scaling up is finished, we can request to update the save data with the new entry:
Finally, ensure that the actor instance you want to save in your world/level has a valid GUID:
That's it! Now we're saving this actor.
That's basically it. Now when you play, you'll notice that this actor now saves its properties!
Finally, there are 2 methods in that we need to adjust:
To load, we just need to adjust our Event BeginPlay slightly and bind to 's OnDataLoaded() delegate: