Keys exist but no values. With caching file

Discussion and help for Easy Save 3
Post Reply
BFRETHGIL
Posts: 23
Joined: Wed Jun 13, 2018 8:25 pm

Keys exist but no values. With caching file

Post by BFRETHGIL »

Every time I restart my app on Unity's editor (stop game mode, start again),
I can't get the correct stored values.

During game mode everything is fine-I can load and save without a problem, but when restart the game it's all gone.
An interesting thing is that If I try to view the es3 file using a notepad after I stop the game, and restart the game, then there's no problem

Code: Select all

private void Awake()
    {
       
        if (instance == null)
        {
            instance = this;
            file = new ES3File("myFile.es3");
            scores= LoadFromFileES3("scores", -1));
            Debug.Log("scores= "+ scores);
        }

        else if (instance != this)
        {
                  Destroy(gameObject);
        }

}

    void OnApplicationPause(bool boo)
    {
        file.Sync();
    }
  public void SaveToFile<T>(string key, T value)
    {
         file.Save<T>(key, value);
     }

public T LoadFromFileES3<T>(string key, T defaultValue)
    {

        T valueToReturn= defaultValue;
          if (!file.KeyExists(key))
            {
                Debug.Log(key + " tag doesn't exist");
                SaveToFile(key, defaultValue);
            }
            else
            {
                Debug.Log(key + "tag exists"); 
                valueToReturn= file.Load<T>(key, defaultValue);
                Debug.Log("value stored in file or default= "+valueToReturn); 
            }
        }

void SaveScores(){
Debug.Log("updating scores");
SaveToFile("scores",1000);
  scores= LoadFromFileES3("scores", -1));
 Debug.Log("playmode:  scores= " +scores);}

    }
Output first time (new file):
scores tag doesn't exists
scores=-1
updating scores
scores tag exists
value stored in file or default=1000
playmode: scores=1000

Output second time (file exists):
scores tag exists
value stored in file or default=-1
scores=-1

Why doesn't it get the correct value? scores=1000 on second launch? Why does it return a default value
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Keys exist but no values. With caching file

Post by Joel »

Hi there,

The most likely reason for this is because in OnApplicationPause you're not checking the bool to check whether it's been paused or not. You should only save whether this is true.

If this is not the issue, please could you create a very basic project with the minimal code required to replicate it, and PM it to me with instructions?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
BFRETHGIL
Posts: 23
Joined: Wed Jun 13, 2018 8:25 pm

Re: Keys exist but no values. With caching file

Post by BFRETHGIL »

Hey, thanks for your answer, I tried to check OnApplicationPause boolean as you suggested and it didn't solve the problem (I also don't understand why does it matter? perhaps I don't understand what SYNC does)
But- I deleted OnApplicationPause and created instead-
public void OnApplicationQuit()
{
file.Sync();
}
And now everything is OK. Why can't sync save the file on OnApplicationPause ? I placed it there (OnApplicationPause)to sync the file multiple time (just to be sure everything is saved)
Is it safer to sync it only on OnApplicationQuit? can I trust it to always save the information (even when the app crashes? Getting a call?)
Or maybe I don't understand what Sync really does
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Keys exist but no values. With caching file

Post by Joel »

Hi there,

Are you just pressing the Stop and Start buttons in the Unity Editor to test this? I don't believe this would trigger the OnApplicationPause event, which would cause the behaviour you're experiencing. However, the OnApplicationQuit event is triggered.

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