Suggestion for saving an entire scene?

Discussion and help for Easy Save 3
WadeSacca
Posts: 4
Joined: Thu Jun 21, 2018 4:50 pm

Re: Suggestion for saving an entire scene?

Post by WadeSacca »

Thanks for the quick response. Glad I found this before the weekend.

I'm pretty sure I get it now and it was along the lines of what I was thinking but I still had a miss. So the transforms of the newly instantiated GO's are loaded into the transforms array and the LoadInto essentially replaces those transforms with the new ones. That makes sense.

One more note. You compile the pieces of code into one larger piece at the end of that post. That is the quick copy and paste piece but you did not update that piece to reflect the changes you made in the Load chunk. Just want to avoid any more confusion when someone like me copies the full code snippet instead of piece by piece.

Thanks again!
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Suggestion for saving an entire scene?

Post by Joel »

Thanks for noticing, I've updated the other pieces of code also.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
WadeSacca
Posts: 4
Joined: Thu Jun 21, 2018 4:50 pm

Re: Suggestion for saving an entire scene?

Post by WadeSacca »

Okay last time I promise. The updated code still didn't work for two reasons. First, there is an extra transform.add() in there that isn't ever going to work and you probably intended to delete it.

The second and much bigger issue is that you changed the array to a list for the transforms. This won't work because the original transforms are saved as an array so it throws an error saying that you cannot convert type Transform[] to type Transform.

So I changed it back to an array and added in int to cycle through the array positions and add the transforms to the array.

Here is the working code in it's entirety. I didn't take out my singleton and I changed the name because the name conflicts with one of the Easy Save 2 example scripts. This is tested and works as intended.

Code: Select all

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

public class LevelSaveManager : MonoBehaviour 
{
	#region Singleton

	public static LevelSaveManager Instance = null;

	void Awake()
	{
			Instance = this;
	}

	#endregion


	public UniqueID[] prefabs;
	public List<UniqueID> prefabInstances = new List<UniqueID>();
		
	public GameObject InstantiatePrefab(GameObject prefab)
	{
		var go = Instantiate(prefab);
		prefabInstances.Add(go.GetComponent<UniqueID>());
		return go;
	}

	public void Save() 
	{
		string[] guids = new string[prefabInstances.Count];
		Transform[] transforms = new Transform[prefabInstances.Count];
		for(int i = 0; i < guids.Length; i++)
		{
			guids [i] = prefabInstances [i].guid;
			transforms [i] = prefabInstances [i].transform;
		}
		ES3.Save<string[]>("guids", guids);
		ES3.Save<Transform[]>("transforms", transforms);
		Debug.Log ("Saved");
	}


	public void Load()
	{
		prefabInstances = new List<UniqueID> ();
		string[] guids = ES3.Load<string[]>("guids");
		Transform[] transforms = new Transform[guids.Length];

		int i = 0;
		foreach (var guid in guids)
		{
			// Find the prefab with this GUID.
			foreach(var prefab in prefabs)
			{
				if(prefab.guid == guid)
				{
					var go = InstantiatePrefab(prefab.gameObject);
					transforms[i] = go.transform;
					i += 1;
				}
			}
		}
		ES3.LoadInto("transforms", transforms);
	}
}
vectorxstudios
Posts: 16
Joined: Fri Jun 08, 2018 6:06 pm

Re: Suggestion for saving an entire scene?

Post by vectorxstudios »

I downloaded your package and added the ComponentsToSave component (as well as populated it.) Is there something I need to do in order to make the components save?

When I do ES3.Save<GameObject>("Name", theObject, filename) all that is stored is:

Code: Select all

{"Animal_Rabbit_45e534a0-0b44-473c-9cb3-d0d66d1e0e06":{"__type":"UnityEngine.GameObject,UnityEngine.CoreModule","value":{"__type":"VectorXStudios.ZoneSaveMe,Assembly-CSharp","_ES3Ref":952125650034871047,"goID":2724588631273018486}},"Animal_Rabbit_072e412e-74f3-4611-9ef0-82480684a0fd":{"__type":"UnityEngine.GameObject,UnityEngine.CoreModule","value":{"__type":"VectorXStudios.ZoneSaveMe,Assembly-CSharp","_ES3Ref":6569023144503312338,"goID":8281692784273219645}},"Animal_Rabbit_67228dc1-c226-4726-b11d-04c9ec16dd43":{"__type":"UnityEngine.GameObject,UnityEngine.CoreModule","value":{"__type":"VectorXStudios.ZoneSaveMe,Assembly-CSharp","_ES3Ref":131459543840665315,"goID":5679232659393814288}},"Animal_Rabbit_d327c120-71f8-46f5-8531-a6852ac91220":{"__type":"UnityEngine.GameObject,UnityEngine.CoreModule","value":{"__type":"VectorXStudios.ZoneSaveMe,Assembly-CSharp","_ES3Ref":8971413706190462038,"goID":6784248902112730694}},"Animal_Rabbit_3588b12e-e058-4aa4-b20d-fef258d2fe5e":{"__type":"UnityEngine.GameObject,UnityEngine.CoreModule","value":{"__type":"VectorXStudios.ZoneSaveMe,Assembly-CSharp","_ES3Ref":4055512070812140942,"goID":5852817087030000581}},"Animal_Rabbit_66e6f077-db2b-4a94-a3a9-645291590919":{"__type":"UnityEngine.GameObject,UnityEngine.CoreModule","value":{"__type":"VectorXStudios.ZoneSaveMe,Assembly-CSharp","_ES3Ref":4590116675753630820,"goID":8557344474819045257}}}
WadeSacca
Posts: 4
Joined: Thu Jun 21, 2018 4:50 pm

Re: Suggestion for saving an entire scene?

Post by WadeSacca »

You are probably stuck until after the weekend. Just read through this entire thread again and I was curious about something. I am using a low poly style and not using terrain trees at all. I am using instantiated trees and other decoration objects. His original code, or at least the corrected version is working fine for me to save trees, buildings, bushes, rocks, etc. The point is, to be clear though, all of these objects are static and have no attached scripts. So they are merely the basic components, a collider and a rigidbody. But since the values for those are not changed outside of the prefab, then when they are loaded back in during the load function, they are once again instantiated from the prefab and all of those fixed values have not changed. So where I am going with this is, what changed components are you concerned about having to save? The only thing I could see would be a script component with some changeable data on it.

If that is the case, if you could make one item script class that includes all of the data you need regardless of the item type, whether a rabbit or a tree or a building(make the code function based on the tag) then you could include it in the same basic function he has listed above by just including an additional component like he has the transform.

I'm sure there would be some exceptions to this working for every item type but it might simplify the save/load functions but would require some more planning in your item class script and that script could get ugly. But consider that you could access other 'manager' style scripts for functionality and use the attached script as mostly a data container.

One more thing to consider in your game style as I have been working on a similar game off and on for a year or so (not right now), remember that anything that is outside of the players view that is not stationary at save time is unimportant and can dramatically cut down on your save depending on what it is. For example, enemies and wandering creatures. If the player doesn't know about them then they don't exist. The proverbial tree falls in a forest scenario. So don't save them. In a perfect world, they are only instantiated (pulled from a pool in a truly perfect world) when the player gets within a certain range.

Another thought although I had personally not gotten this far. It is common in these types of games to use Unity's standard terrain tree generator when creating terrain. Then when one is chopped down, it is replaced with an actual model while the terrain instance is destroyed. This allows you to take advantage of Unity's built in culling and not have to manually place a ton of higher poly trees which would also then have to be saved.

You may know all of this and I am just wasting your time but I am waiting for a massive package to load on my super slow PC so I had time to waste and I am just trying to think around your issue. He did not attache the part he sent to you so I cannot comment on it directly.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Suggestion for saving an entire scene?

Post by Joel »

vectorxstudios wrote:I downloaded your package and added the ComponentsToSave component (as well as populated it.) Is there something I need to do in order to make the components save?
Hi there,

Have you added support for the scripts in the Window > Easy Save 3 > Types panel? If so, please could you send me a quick project so I can try to work out what's going on?

@WadeSacca: Thanks for providing an updated version of the original script.

The script I sent vectorxstudios saves all of the Components on a GameObject that are added to an array in a ComponentsToSave Component. If you PM me your invoice number, I'll be happy to send the updated ES3Type_GameObject over, and the ComponentsToSave script.

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