Storing randomly generated character stats

Discussion and help for Easy Save 3
Post Reply
Graithen
Posts: 2
Joined: Mon Aug 09, 2021 7:33 pm

Storing randomly generated character stats

Post by Graithen »

Hey everyone,

I'm trying to figure out how to save randomly generated characters using Easy Save. I was creating a list and storing character objects that contain the characters name and age, then saving that list into easy save... The problem with this is that I'm guessing a list of character objects doesn't actually store the variables stored in those objects?

I'm pretty confused by this problem to be honest, and would love some advice!
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Storing randomly generated character stats

Post by Joel »

Hi there,

I'm afraid it's not possible to tell what is happening from what you've said. Please could you show me the scripts you're trying to save, and the code you're using to save and load the List of them?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Graithen
Posts: 2
Joined: Mon Aug 09, 2021 7:33 pm

Re: Storing randomly generated character stats

Post by Graithen »

Ok so excuse the mess... I just threw some stuff together!

Code: Select all

public class SaveDataTest : MonoBehaviour
{
    public List<string> TestList;
    public List<TestAgents> TestAgentList = new List<TestAgents>();
    public int TestInt;
    public string TestString;
    public float TestFloat;

    //Character Generation
    [Header("Resources")]
    public int Population;
    public TextAsset FirstNames;
    public TextAsset SecondNames;
    public TextAsset PlaceNames;

    //Loaded Data
    public string[] firstNameList, secondNameList;

    //UI Stuff
    [Header("Character Text")]
    public TMP_Text text;


    private void Start()
    {
        InitialiseData();
    }

    public void SaveData()
    {
        ES3.Save("SavedTestInt", TestInt, "MainSave.es3");
        ES3.Save("SavedTestFloat", TestFloat, "MainSave.es3");
        ES3.Save("SavedTestString", TestString, "MainSave.es3");
        //ES3.Save("SavedTestList", TestList, "MainSave.es3");
        ES3.Save("SavedTestAgentList", TestAgentList, "MainSave.es3");
        Debug.Log("Saving data!");
    }

    public void LoadData()
    {
        //TestList = ES3.Load<List<string>>("SavedTestList", "MainSave.es3");
        TestAgentList = ES3.Load<List<TestAgents>>("SavedTestAgentList", "MainSave.es3");
        TestInt = ES3.Load<int>("SavedTestInt", "MainSave.es3");
        TestFloat = ES3.Load<float>("SavedTestFloat", "MainSave.es3");
        TestString = ES3.Load<string>("SavedTestString", "MainSave.es3");
        Debug.Log("Loading data!");
        Debug.Log(TestAgentList.Count);
    }

    public void ClearCurrentData()
    {
        TestList = null;
        TestAgentList = null;
        TestInt = 0;
        TestFloat = 0;
        TestString = null;
        text.text = "";
        Debug.Log("Clearing current data!");
    }

    public void DeleteData()
    {
        ES3.DeleteFile("MainSave.es3");
    }

    public void CreateCharacters()
    {
        int i = Population;

        while (i > 0)
        {
            TestAgents temp = new TestAgents();
            temp.FName = CreateFirstName();
            temp.SName = CreateSurname();
            temp.Age = Random.Range(15, 60);

            TestAgentList.Add(temp);
            i--;
        }
    }

    public void LoadRandomCharacter()
    {
        int rand = Random.Range(0, TestAgentList.Count);
        string str = (TestAgentList[rand].FName + "\n" + TestAgentList[rand].SName + "\n" + TestAgentList[rand].Age + "\n");
        text.text = str;
    }

    void InitialiseData()
    {
        string text = "";

        text = FirstNames.ToString();
        firstNameList = text.Split('\n');

        text = SecondNames.ToString();
        secondNameList = text.Split('\n');
    }

    string CreateFirstName()
    {
        string fName = firstNameList[Random.Range(0, firstNameList.Length - 1)];
        return fName;
    }

    string CreateSurname()
    {
        string sName = secondNameList[Random.Range(0, secondNameList.Length - 1)];
        return sName;
    }
}
Basically I am generating some simple random characters with the below script and trying to save them with the script above.

Code: Select all

public class TestAgents
{
    string fName, sName;
    private int age;

    public string FName
    {
        get { return fName; }
        set { fName = value; }
    }

    public string SName
    {
        get { return sName; }
        set { sName = value; }
    }

    public int Age
    {
        get { return age; }
        set { age = value; }
    }
}
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Storing randomly generated character stats

Post by Joel »

Hi there,

None of the fields on your TestAgents class are serializable by default, as they're all either private fields or properties. To make them serializable you would need to add a [SerializeField] attribute to each of them.

For more information please see the Supported Types guide:
https://docs.moodkie.com/easy-save-3/es ... ted-types/

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