Broke My Game Trying to Load Array

Discussion and help for Easy Save 3
Post Reply
jleven22
Posts: 8
Joined: Wed Jan 27, 2021 5:32 pm

Broke My Game Trying to Load Array

Post by jleven22 »

I've been stuck on this issue for almost a month and today I finally narrowed it down to the fact that I'm just using array saving/ loading wrong.

Basically, I have two arrays, one string and one int. They are storing values and I want to be able to save and load the same values from level to level - this is for an inventory system.

When I'm not using Easy Save they work fine!

As soon as I try and load, I get nothing back and get all sorts of errors.

Here is how I'm calling it:

Code: Select all

        if (ES3.KeyExists("Items Held"))
        {
            CharacterTracker.instance.itemsHeld = ES3.Load<string[]>("Items Held");
        }
        else
        {
            CharacterTracker.instance.itemsHeld = new string[0];
            CharacterTracker.instance.itemsHeld = new string[30];
        }

        if (ES3.KeyExists("Number Of Items"))
        {
            CharacterTracker.instance.numberOfItems = ES3.Load<int[]>("Number Of Items");
        }
        else
        {
            CharacterTracker.instance.numberOfItems = new int[0];
            CharacterTracker.instance.numberOfItems = new int[30];

        }
The "else" is to reset the contents of the arrays back to 0. Not really relevant to this post.

If it helps (don't think it does), here's how I'm saving.

Code: Select all

        ES3.Save<int[]>("Number Of Items", CharacterTracker.instance.numberOfItems);
        ES3.Save<string[]>("Items Held", CharacterTracker.instance.itemsHeld);
I'm new to Easy Save so I suspect I'm doing this all wrong, but would really appreciate some guidance. Thank you! :D
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Broke My Game Trying to Load Array

Post by Joel »

Hi there,

Please could you let me know what errors you're getting?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
jleven22
Posts: 8
Joined: Wed Jan 27, 2021 5:32 pm

Re: Broke My Game Trying to Load Array

Post by jleven22 »

Joel wrote: Wed Jan 27, 2021 6:01 pm Hi there,

Please could you let me know what errors you're getting?

All the best,
Joel
Unfortunately it's too complex for me to try and reiterate. The errors have more to do with my own code, but the origin of these errors is the way I am loading. As long as I don't load (aka comment out the code I already pasted), everything plays fine.

Am I loading correctly or not?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Broke My Game Trying to Load Array

Post by Joel »

Hi there,

The code works fine in my tests. If there are no Easy Save errors thrown then the issue is very likely to be within your logic. I recommend running the following code to test:

Code: Select all

using UnityEngine;

public class TestArrays : MonoBehaviour
{
    public string[] itemsHeld = new string[] { "one", "two", "three" };
    public int[] numberOfItems = new int[] { 1, 2, 3 };

    void Start()
    {
        Save();
        Load();
    }

    void Load()
    {
        if (ES3.KeyExists("Items Held"))
            itemsHeld = ES3.Load<string[]>("Items Held");

        if (ES3.KeyExists("Number Of Items"))
            numberOfItems = ES3.Load<int[]>("Number Of Items");
    }

    void Save()
    {
        ES3.Save<int[]>("Number Of Items", numberOfItems);
        ES3.Save<string[]>("Items Held", itemsHeld);
    }
}
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
jleven22
Posts: 8
Joined: Wed Jan 27, 2021 5:32 pm

Re: Broke My Game Trying to Load Array

Post by jleven22 »

You were right, it was my logic. Thanks!
Post Reply