Saving and Loading Entire Scene

Discussion and help for Easy Save 3
Zodsmar
Posts: 6
Joined: Sat Jan 16, 2021 6:57 pm

Saving and Loading Entire Scene

Post by Zodsmar »

I am new to Easy save and I am trying to save and load entire scenes.
Background:
I am building a fun small little game (literally for itch just for fun) and it's a grid-based builder game. The player uses the mouse and based on where they click an object can be spawned. At one point I want to be able to save all of those object positions (Which I am doing with the prefab and auto save). Once I do that save I place a few more prefabs around and then want to load back to my previous spot (So destroy anything new from the previous save and essentially reset the scene to exactly how it was when I saved).

I want to be able to do this because once I hit save (In Freeplay mode) I want to have those save files as Levels (Basically I can build game levels and save them to a file) Then on the main menu Scene (Different scene), I want to be able to load a previous save into a new scene.

I am not sure if this is possible, but currently, I have it working that I can save. Stop the unity editor. Play hit load and it loads what I had placed. If I keep placing (while still active and load) it doesn't delete new stuff. The idea behind the save is that I can then rename the different saves to level1.es3, etc. then load each level.

Any help would be appreciated.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading Entire Scene

Post by Joel »

Hi there,
Once I do that save I place a few more prefabs around and then want to load back to my previous spot (So destroy anything new from the previous save and essentially reset the scene to exactly how it was when I saved).
Generally you would do this you would keep track of what objects you have added since you last saved (i.e. List of objects). You can then iterate through this list and delete the objects.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Zodsmar
Posts: 6
Joined: Sat Jan 16, 2021 6:57 pm

Re: Saving and Loading Entire Scene

Post by Zodsmar »

Okay that could work actually thanks. But when it comes to loading the level itself. I have prefabs but then in the level is things like a grid, main camera, etc. (the not prefab always needed stuff) That stuff doesn't change so I can just leave that in terms of saving but how do I from a Main Menu, load a (Default scene that has all of the basics) wait for it to be loaded then load the Save file to build that level.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading Entire Scene

Post by Joel »

Hi there,

Assuming you're using Auto Save, there's information on triggering Auto Save from code and changing the filename that is loaded from at the bottom of the Auto Save guide.

You can put this in any method of a script which is called when the scene loads (e.g. Start()).

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Zodsmar
Posts: 6
Joined: Sat Jan 16, 2021 6:57 pm

Re: Saving and Loading Entire Scene

Post by Zodsmar »

Yeah so I tried that. I had

Code: Select all

 SceneManager.LoadScene(SceneManager.GetActiveScene().name);
       ES3AutoSaveMgr.Current.Load();
For now to simply just refresh the entire scene, then load it in. It refreshes I see the gameobjects spawn for a split second then get destroyed. Not sure why it does that
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading Entire Scene

Post by Joel »

Hi there,

This is because Unity waits until the end of the frame before loading the scene. This means that everything which is loaded will be destroyed. Instead you would need to put the code in an event which is called when the scene loads, rather than when your old scene quits.

As this regards Unity's functionality rather than ours, please see Unity's documentation for more information:
https://docs.unity3d.com/ScriptReferenc ... Scene.html

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Zodsmar
Posts: 6
Joined: Sat Jan 16, 2021 6:57 pm

Re: Saving and Loading Entire Scene

Post by Zodsmar »

Great thanks! I got that working. How would I go about loading a specific save file? I see for the autosave is

Code: Select all

ES3AutoSaveMgr.Current.settings.path = "level.es3";
So would I go about when I save set the path to the new level name. Then on load set the path again to the correct level name? Or is there a different way
Zodsmar
Posts: 6
Joined: Sat Jan 16, 2021 6:57 pm

Re: Saving and Loading Entire Scene

Post by Zodsmar »

I used the way above even if its not right and it works. My final question would be, can I change where the files are saved? For example a Levels folder (for ingame levels) then a user folder (For people to save there levels and then share with someone) I also want to them parse those 2 folders to find all of the levels that exist.

Edit: Ideally when I build the game I want the game Levels save data in the unity project folder (So like Assets/Levels) but when the game is built wherever the EXE is a folder is generated for user levels so like

Folder /
--- Game.exe
--- UserLevels/
--- --- myLevel.es3

The Assets/Levels (I can move and put them there my self I just need a way to load from that specific location. And anything saved saves to a different location (I am using the AutoSave Method)
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading Entire Scene

Post by Joel »

Hi there,

As mentioned before, there's information on setting the file path at the bottom of the Auto Save guide. You can use this to specify your own file path. Note that you can get the path containing the EXE using Application.dataPath. However, note that this is not guaranteed to be writable at runtime (see Unity's docs for more info).

With regards to getting files and folders, you can use the ES3.GetFiles and ES3.GetDirectories methods:
https://docs.moodkie.com/easy-save-3/es3-api/es3-class/

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Zodsmar
Posts: 6
Joined: Sat Jan 16, 2021 6:57 pm

Re: Saving and Loading Entire Scene

Post by Zodsmar »

Thats awesome. One thing I am still a bit confused about is the default save path.

Code: Select all

ES3AutoSaveMgr.Current.settings.path = "MyFile.es3";
From what I understand from the EasySave3 settings Autosave saves to Cache, how do I change the default save location of where ES3 looks for files.

Edit: Or more specifically when I call

Code: Select all

ES3AutoSaveMgr.Current.Load();
how do I tell ES3 where it is looking, if I need to change it somehow with

Code: Select all

ES3AutoSaveMgr.Current.settings.path
then I need to after I load a level set it back to the current location. (Basically loading levels for ingame levels is the only time the directory will change.
Last edited by Zodsmar on Sat Jan 16, 2021 10:10 pm, edited 1 time in total.
Post Reply