Page 1 of 1

Problems with duplicate items on ES3.Load

Posted: Fri Jan 22, 2021 2:48 am
by zavros
I'm saving a list of custom unit objects(which I set up in the ES3 types) that contains all the unit info.
In the previous scene I edit some of unit info and save the list.
In the next scene I load in the unit list, generate instances of my units, then copy variables from the loaded list to the prefab's copy. (not sure if this is the best way to do this?)
What I'm seeing is that every Item in the loadList has the same variables that were set on the last item that was added to the load list before saving it. I've confirmed they were all unique before saving and not sure what I'm doing wrong.

Code: Select all

List<Unit> loadList = ES3.Load("playerUnitList", new List<Unit>());
				for(int i=0; i < loadList.Count;i++)
				{
					int unitIdx=UnitDB.GetPrefabIndex(loadList[i]);
					startingUnitList.Add(UnitDB.GetItem(unitIdx));
					//Debug.Log("loadlist name ["+i+"] of unit " + loadList[i].GetName());
				}
				for(int i=0; i<startingUnitList.Count; i++){
					if(startingUnitList[i]==null) continue;
					GameObject unitObj=(GameObject)MonoBehaviour.Instantiate(startingUnitList[i].gameObject, new Vector3(0, 99999, 0), Quaternion.identity);
					Unit tempUnit = unitObj.GetComponent<Unit>();
					Unit tempUnit2 = loadList[i];[code]
UnitSaver.copyUnitToUnit(tempUnit, tempUnit2);

deployingList.Add(tempUnit);
if(loadedFromCache) deployingList[deployingList.Count-1].loadedFromCache=true;

unitObj.transform.parent=UnitManager.GetInstance().transform;
}
[/code]

Re: Problems with duplicate items on ES3.Load

Posted: Fri Jan 22, 2021 8:40 am
by Joel
Hi there,

Please could you create a brand new project with a very basic scene which replicates your issue?

Also to simplify your code, you might also want to save the length of the playerUnitList with the data. Then you can use this length to create an array of Unit instances, and then use ES3.LoadInto("playerUnitList", instances), which loads the Units into these instances.

E.g.

Code: Select all

int length = ES3.Load("playerUnitListLength", 0);
var loadList = new List<Unit>();
for(int i=0; i<length; i++)
    loadList.Add( CreateYourUnitInstanceHere() );
ES3.LoadInto("playerUnitList", loadList);
All the best,
Joel

Re: Problems with duplicate items on ES3.Load

Posted: Sat Jan 23, 2021 8:47 am
by Joel
Hi there,

Thanks for sending that over, there seem to be a couple of major errors in your code.

Firstly, in the Types window you've only selected 'stat2' of your 'Stats' class, meaning your 'stat1' variable will not be saved.

Secondly, I think you might misunderstand what your code is doing. Your code is not creating 5 different Cube objects, it's just adding the same instance of a Cube to a List five times. This means that you're saving the same object five times.

If you want five different objects, you should use Instantiate to instantiate your prefab five times, rather than referring directly to your prefab. Note that you would also want to right-click the prefab and click Easy Save 3 > Enable Easy Save for Prefab.

All the best,
Joel