Save profiles
This page shows how to save your game when you are using the profile functionality. This allows a player to have multiple profiles and keep their saves intact across multiple games.
Marking items to be saved
Marking simple items to be saved can be very easily done by adding the Saved attribute to a property. Safeguard currently only supports properties.
public class MyBehaviour : MonoBehaviour
{
[Saved]
public string PropertyToSave { get; set; }
}
This is the easiest and most common way to save values for an object in your game. It is important to note that the path towards your script component should be unique. This means that your scene hierarchy can not contain duplicate GameObjects with the same name at the same level in the hierarchy. This method also does not support objects created at runtime. This meaning that if u have a manager class that keeps track of multiple GameObjects the saving should happen on the manager class, and the manager class should re-instantiate these objects. For this you can use this structure as reference.
[Saved]
public List<string> SavedIds
{
get
{
// Return the values you want to save
return moduleData.Select(x=> x.MyId).ToList();
}
set
{
//Actually do something useful with your saved data here
foreach (var id in value) new GameObject(id);
}
}
public List<MyClass> SavedObjects { get; private set; } = new();
Save the game
SaveSystem.Save(SaveType.Quick, "SceneName", "profileName");
Load the game
There are multiple ways to load a save. Depending on how you want it to function you can use one of the examples from below.
If you want to load the latest save for a profile
SaveSystem.LoadLatestSave("profileName");
If you want to load the latest save across all profiles
SaveSystem.LoadLatestSaveAcrossProfiles();
If you have a specific save you want to load you can pass the name of the save to the Load method
SaveSystem.Load("profileName", saveName);