Page 1 of 1

Save everything except position

Posted: Sun Jan 15, 2023 1:38 pm
by qest43
So I have some checkpoints in game, I activate them when I interact with them by 'E' key. Then I trigger in script

Code: Select all

SaveSystem.SaveToSlot(GameManager.Instance.currentSaveSlot);
But sometimes I die, and I want to keep player inventory, and all this stuff and respawn him in last checkpoint. I could do that in code ofc, just refill HP and teleport etc. but I want to use this also when I exit game, I want to save everything but when player load save slot he will start in last checkpoint he visited with kept inventory and all this stuff.

Re: Save everything except position

Posted: Sun Jan 15, 2023 3:13 pm
by Tony Li
Hi,

Since you don't want to restore the saved data except for checkpoint location, I recommend handling the respawn in code (refill HP, teleport player). Get the checkpoint position from the UCCSaver component's saved data. Example:

Code: Select all

public Vector3 GetCheckpointLocation()
{ // Assume we run this on the player.
    var uccSaver = GetComponent<uccSaver>();
    var s = SaveSystem.currentSavedGameData.GetData(uccSaver.key);
    var data = SaveSystem.Deserialize<UCCSaver.Data>(s);
    return data.position;
}
To handle exiting the game, you can make a subclass of UCCSaver. Something like:

Code: Select all

public class MyUCCPlayerSaver : UCCSaver // Put this on player instead of UCCSaver.
{ // Assumes you're calling SaveSystem.SaveToSlotImmediate() or using AutoSaveLoad to save when quitting.
    private bool isQuitting = false;
    
    void OnApplicationQuit()
    {
        isQuitting = true;
    }
    
    public override string RecordData()
    {
        Vector3 checkpointLocation = GetCheckpointLocation();
        var s = base.RecordData();
        if (isQuitting)
        { // Store previous checkpoint position in save data:
            var data = SaveSystem.Deserialize<UCCSaver.Data>(s);
            data.position = checkpointLocation;
            s = SaveSystem.Serialize(data);
        }
        return s;
    }
}