Different levels of "persistent" saving

Announcements, support questions, and discussion for Save System for Opsive Character Controllers.
Post Reply
robloxillian
Posts: 27
Joined: Mon Sep 13, 2021 6:55 pm

Different levels of "persistent" saving

Post by robloxillian »

I previously asked a variant of this question with a slightly different use case, but now I'm trying to set up a save system for my game that has two types of items: normal items and "quest" items. I have a somewhat usual plan for the saving system where saving and reloading is a form of time travel in that quest items travel back in time (similar to persistent items in Majora's Mask).

I have everything working for normal items using a persistent inventory component on the inventory containing normal items. I can do something like the following:

1. Start a new game.
2. Obtain an item.
3. Save and quit.
4. Reload the game.
5. The item I obtained is still in the inventory.

I can also do this:

1. Start a new game.
2. Save the game.
3. Obtain an item.
4. Reload the save from step 2.
5. I no longer have the item.

Both of these are correct for normal items, but for quest items, I want the item to still be present after reloading the earlier save (unless the whole save file is erased). Would it make sense to save quest items to some persistent slot like -1? If so, what methods can I use to save only the persistent inventory (or whatever component I should use instead) and reload from it (on top of other things I'm reloading from the specific slot) to get back the quest items? Alternately, is there a way to save just the persistent quest inventory (and nothing else) to the slot I'm about to load from right before I load it?
User avatar
Tony Li
Posts: 22195
Joined: Thu Jul 18, 2013 1:27 pm

Re: Different levels of "persistent" saving

Post by Tony Li »

Hi,

That should work.

Add a separate Inventory Saver component to the persistent inventory. Give it a unique key such as "Quest Items".

When you do a regular save to a slot, also do a save to slot -1. (If performance becomes an issue, you can later change it so this slot only saves the Quest Items inventory. Let's first just make sure your idea works.)

When you do a regular load, immediately after loading the game use SaveSystem.storer.RetrieveSavedGameData(-1) to get the saved data from slot -1. Then get the data for "Quest Items" and send it to the current scene's persistent inventory's InventorySaver.ApplyData() method. Rough example:

Code: Select all

SaveSystem.onSaveDataApplied += RetrieveAndApplyPersistentInventory;
...
void RetrieveAndApplyPersistentInventory()
{
    // Get the persistent inventory save data from slot -1:
    var gameData = SaveSystem.storer.RetrieveSavedGameData(-1);
    persistentInventoryData = gameData.GetData("Quest Items");
    
    // Now find the persistent inventory's InventorySaver in the current scene:
    foreach (InventorySaver saver in FindObjectsOfType<InventorySaver>())
    {
        if (saver.key == "Quest Items")
        {
            saver.ApplyData(persistentInventoryData);
            break;
        }
    }
}
robloxillian
Posts: 27
Joined: Mon Sep 13, 2021 6:55 pm

Re: Different levels of "persistent" saving

Post by robloxillian »

Thanks! I'll try that.

One other thing: I want to add a decimal amount of "money" that the player has which I'm currently just storing as a float attribute of the player. I want to be able to reference it in dialogue conditions (i.e., check whether it's above or below a certain amount). I also want to save it (in the same way as normal items, not quest items). Would it make sense to store this amount as a Lua variable, or will I not be able to save it that way?
User avatar
Tony Li
Posts: 22195
Joined: Thu Jul 18, 2013 1:27 pm

Re: Different levels of "persistent" saving

Post by Tony Li »

As long as you add a Dialogue System Saver component to your Dialogue Manager, all Lua variables will be saved.
Post Reply