Data as String

Discussion and help for Easy Save 3
User avatar
frezno
Posts: 10
Joined: Mon Nov 13, 2017 8:53 pm

Re: Data as String

Post by frezno »

It seems LoadRawString doesn't return anything.

I'm using this code to make sure the keys are in the file.

Save function

Code: Select all

void Save(){
        dataFile.Save<samplePlayerData>("PlayerData", PlayerData);
        dataFile.Save<string>("myName", this.name);
        dataFile.Save<Transform>("myTransform", transform);

        string sample = "";

        foreach (string s in dataFile.GetKeys())
        {
            sample += " Key: " + s;
        }

        print("Size: " + dataFile.Size() + sample);
}
Here is a sample class I made to test saving random values. This class has an ES3Type setup also.

Code: Select all

public class samplePlayerData: MonoBehaviour
{

    public string key1 = "";
    public int value1 = 0;
    public bool bool1 = false;
    public float value2 = 0;
    public string key2 = "";
    public bool bool2 = true;
    public Vector2 vector1 = new Vector2(0, 2);
    public string key3 = "";
    public Vector3 vector2 = new Vector3(3, 4, 1);

    public void GenerateValues()
    {
        key1 = Path.GetRandomFileName();
        value1 = Random.Range(0, 100);
        bool1 = false;
        value2 = Random.value;
        key2 = Path.GetRandomFileName();
        bool2 = true;
        vector1 = new Vector2(Random.Range(0, 100), Random.Range(0, 100));
        key3 = Path.GetRandomFileName();
        vector2 = new Vector3(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100));
    }
}
It returns this, so I know the keys are in the file but it seems LoadRawString or LoadRawBytes still isn't returning anything.

Code: Select all

Size: 706 Key: PlayerData Key: myName Key: myTransform
UnityEngine.MonoBehaviour:print(Object)
cloudweight
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Data as String

Post by Joel »

Hi there,

Would you be able to create a basic script which replicates this so I can attempt to replicate it at my end? LoadRawString appears to be working fine at my end.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
User avatar
frezno
Posts: 10
Joined: Mon Nov 13, 2017 8:53 pm

Re: Data as String

Post by frezno »

Here is my PlayerDataLoader class that loads data.

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerDataLoader : MonoBehaviour
{
    [TextArea]
    public string toJson, fromJson;

    public samplePlayerData PlayerData;
    public static ES3File dataFile;

    public void Start()
    {
        dataFile = new ES3File();
        PlayerData.GenerateValues();
    }

    public void PlayerDataToJson()
    {
        dataFile.Save<samplePlayerData>("PlayerData", PlayerData);

        string sample = "";

        foreach (string s in dataFile.GetKeys())
        {
            sample += " Key: " + s;
        }

        print("Size: " + dataFile.Size() + sample);
        print(dataFile.LoadRawString());
        toJson = dataFile.LoadRawString();
    }

    public void PlayerDataFromJson(Dictionary<string, string> result)
    {
        if (!result.ContainsKey("PlayerData"))
        {
            Debug.LogError("No player data to sync, are you sure there is data on the server?");
        }
        else
            fromJson = result["PlayerData"];

        if (dataFile != null)
            dataFile = null;

        dataFile = new ES3File(System.Text.Encoding.UTF8.GetBytes(fromJson));
        dataFile.LoadInto<samplePlayerData>("PlayerData", PlayerData);
    }

}
Here is my samplePlayerData sample class that stores random data.

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class samplePlayerData : MonoBehaviour
{

    public string key1 = "";
    public int value1 = 0;
    public bool bool1 = false;
    public float value2 = 0;
    public string key2 = "";
    public bool bool2 = true;
    public Vector2 vector1 = new Vector2(0, 2);
    public string key3 = "";
    public Vector3 vector2 = new Vector3(3, 4, 1);

    public void GenerateValues()
    {
        key1 = Path.GetRandomFileName();
        value1 = Random.Range(0, 100);
        bool1 = false;
        value2 = Random.value;
        key2 = Path.GetRandomFileName();
        bool2 = true;
        vector1 = new Vector2(Random.Range(0, 100), Random.Range(0, 100));
        key3 = Path.GetRandomFileName();
        vector2 = new Vector3(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100));
    }

    public void Clear()
    {
        key1 = "";
        value1 = 0;
        bool1 = false;
        value2 = 0;
        key2 = "";
        bool2 = true;
        vector1 = new Vector2(0, 2);
        key3 = "";
        vector2 = new Vector3(3, 4, 1);
    }
}
cloudweight
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Data as String

Post by Joel »

Thanks for your patience, I've managed to track down the issue.

It looks like there was a small bug which was preventing the data from being pushed to the MemoryStream. I'll PM you a fix for this.

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