Saving state changes on new builds

Discussion and help for Easy Save 3
Post Reply
jayway
Posts: 2
Joined: Fri Aug 28, 2020 3:41 am

Saving state changes on new builds

Post by jayway »

Hello,

I am trying to pinpoint the exact cause of the problem I'm seeing. I've noticed sometimes on a new build, I'll run into alot of errors when loading an existing save from a previous build.

I am saving down the prefabs and a specific component on prefabs that holds all the relevant variables. On load, I load the component, find the prefab the instantiate it into the scene, and copy over the loaded component to the newly instantiated prefab. For the most part this works, but I've noticed when I make a lot of changes to the prefabs, push a new build, any existing saves tend to not find their saved down components or prefabs... I'm really not sure why this would be happening.

Anyone have any insight?

User avatar
Joel
Moodkie Staff
Posts: 2971
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving state changes on new builds

Post by Joel »

Hi there,

Please could you let me know what errors you are getting?

All the best,
Joel
Joel @ Moodkie Interactive
Twitter - Unity

jayway
Posts: 2
Joined: Fri Aug 28, 2020 3:41 am

Re: Saving state changes on new builds

Post by jayway »

Thanks for the quick response!

There's no error in the library. However the way objects are loaded is inconsistent. For example:

I have a component called TowerStats. I save all the TowerStats like so:

Code: Select all

TowerStats[] towers = FindObjectsOfType<TowerStats>();
ES3.Save<TowerStats[]>(towers_key, towers, profile_key);
I load the tower stats like so:

Code: Select all

	TowerStats[] towers = ES3.Load<TowerStats[]>(towers_key, profile_key);
	foreach (TowerStats t in towers) {
		// Instantiate prefabs with loaded stats
	}
Most of my tower prefabs are not in the scene, so I instantiate each one and then load in the TowerStats. This works fine.

However, there are some towers that are in the scene by default, and sometimes the TowerStats are automatically attached, while other times only a ES3 object with the TowerStats component is instantiated. In that case, I need to find existing instantiated tower prefab and attached the loaded TowerStats manually.

Do you know why this inconsistent behavior might be happening? I noticed the times they are not automatically attached is on new builds. All these prefabs are in the ES3 reference manager already.

User avatar
Joel
Moodkie Staff
Posts: 2971
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving state changes on new builds

Post by Joel »

Hi there,

This is expected behaviour. You would need to load the Components after instantiating the prefabs, because a Component cannot exist without a GameObject to attach it to.

Typically you would store an integer stating how many prefabs you need to instantiate, and then instantiate this many when loading. Then you would get the TowerStats Component from each of these and put them into an array, and then use ES3.LoadInto to load your saved array of TowerStats into the array of TowerStats instances.

All the best,
Joel
Joel @ Moodkie Interactive
Twitter - Unity

Post Reply