Save everything except position

Announcements, support questions, and discussion for Save System for Opsive Character Controllers.
Post Reply
qest43
Posts: 33
Joined: Thu May 26, 2022 9:22 am

Save everything except position

Post 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.
User avatar
Tony Li
Posts: 20704
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save everything except position

Post 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;
    }
}
Post Reply