StoreCachedFile is not updating my file with expected values

Discussion and help for Easy Save 3
Post Reply
funtomata
Posts: 26
Joined: Tue Feb 23, 2021 4:25 pm

StoreCachedFile is not updating my file with expected values

Post by funtomata »

Hi,
I recently switched from purely saving and loading directly from files to using a cache file instead.

Saving and loading values from the cache works perfectly fine, however when I call StoreCachedFile, the file is updated but doesn't contain any of the expected keys.

When I create a new game I do the following:

Code: Select all

//Create a SaveData object which contains some information (game creation date etc) and generates a unique file name.
CurrentSave = new SaveData(SaveSlotsPath);
//Save the SaveData Object as a key in a new file
ES3.Save(SaveDataKey, CurrentSave, saveFilePath);
//Loading that file into cache
ES3.CacheFile(saveFilePath);
//Create a new ES3Settings
cacheSettings = new ES3Settings(ES3.Location.Cache);
loading and saving keys into the cache when the game runs works perfectly fine

Code: Select all

        public static T Load<T>(string key, T defaultValue = default)
        {
            return CurrentSave == null ? defaultValue : ES3.Load(key, defaultValue,cacheSettings);
        }
        public static void Save<T>(string key, T value)
        {
                ES3.Save(key, value, cacheSettings);
        }
However, when I want to dump the cache back into the file by calling this method, my file is updated (modification date of the file is updated), but it still only contains the "SaveData" key, and nothing else.

Code: Select all

        public static void SaveToCurrentFile()
        {
            if (CurrentSave != null)
                ES3.StoreCachedFile(saveFilePath, cacheSettings); //I tried with and without the cacheSettings parameter
        }
I suppose somehow I'm reading from the wrong cache or something. It's not quite clear to me what's going on. I'm 100% positive the cache does contain my keys, otherwise my game wouldn't be able to remember that an object is gone when I go from a scene to another and back.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: StoreCachedFile is not updating my file with expected values

Post by Joel »

Hi there,

I'm confused by how you're using the cache here, and it will likely cause unexpected behaviour. You're saving data to file, but then you're loading it into the cache, removing the benefit of using the cache.

Instead you should save the data to the cache (by setting the save location to Cache), and then store it to a file using ES3.StoreCachedFile at an appropriate time. Then when your game loads, you load the entire file from storage into the cache using ES3.CacheFile.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
funtomata
Posts: 26
Joined: Tue Feb 23, 2021 4:25 pm

Re: StoreCachedFile is not updating my file with expected values

Post by funtomata »

Hi,
I do save my data into the cache, as per the documentation, I use ES3.Save(key, value, ES3Settings)
I only save using ES3.Save(key, value, filePath) when I initially create the save file:
- Create a savefile by saving some initial data in it
- Load that savefile into the cache using ES3.CacheFile (I'm doing this in order to clear the cache from whatever was in there before, since there doesn't seem to be another way to clear the cache)
- During gameplay, data is only saved into the cache with ES3.Save(key, value, ES3Settings) with my ES3Settings being configured to Location.Cache
- every once in a while (OnApplicationFocus, OnApplicationQuit and on via some in-game triggers), I call StoreCachedFile

I guess there's a few things I don't understand from the doc.

edit: also here's an extra question: what happens if I want to save different things in different files. For example, I have a game save system that saves stuff about the current game, but as the player plays the game, he unlocks new stuff, so the keys related to the game (which are overwritten if you start a new game) are saved in one file and the keys related to the player profile (which are permanent things) are saved in another file. From my understanding, there's only one single cache file, right? so I can't use cache for both those things? or is there actually a way to work with two separate cache files?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: StoreCachedFile is not updating my file with expected values

Post by Joel »

Hi there,

Please could you create a script which I can drop into a new, empty project which replicates your issue so that I can see what is happening from my end?

Regarding saving to multiple files in the cache, you need to specify a different filename for the filePath parameter when saving to the cache.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
funtomata
Posts: 26
Joined: Tue Feb 23, 2021 4:25 pm

Re: StoreCachedFile is not updating my file with expected values

Post by funtomata »

Hey Joel,
in testing out some stuff to make you a sample project, I an starting to realise I was understanding something backwards...

when doing

Code: Select all

ES3.StoreCachedFile(saveFile);
I thought that "saveFile" was the path to the file where I want to dump the cache. And that I could use just any file name I wanted.

But I was getting an error saying that the file didn't exist, which made no sense to me because it should be creating the file if it didn't exist.

But that's not the case, it's the name of the file in the cache, which apparently defaults to SaveFile.es3. So I think I now understand how to correctly work with the cache. Your previous comment is what finally made everything click together:

I'm going to test this on a more thorough use case before I update my game, but it looks promising.

Here's the code I wrote for my test

Code: Select all

using UnityEngine;
using UnityEngine.UI;

public class SimpleSaveTest : MonoBehaviour
{
    [SerializeField] private Text inputText;
    [SerializeField] private Text displayText;

    private ES3Settings _cacheSettings;

    public void Awake()
    {
        _cacheSettings = new ES3Settings("Welcome.es3", ES3.Location.Cache);
    }

    public void LoadFromCache()
    {
        displayText.text = ES3.Load<string>("text", "Welcome.es3" ,_cacheSettings);
    }
    public void LoadFromFile()
    {
        displayText.text = ES3.Load("text", "Welcome.es3", "Default");
    }

    public void SaveToCache()
    {
        ES3.Save("text", inputText.text, "Welcome.es3",_cacheSettings);
    }

    public void SaveToFile()
    {
        ES3.StoreCachedFile("Welcome.es3");
    }
}
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: StoreCachedFile is not updating my file with expected values

Post by Joel »

Hi there,

Glad things are a bit clearer now.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply