Loading the same file twice

Discussion and help for Easy Save 3
Post Reply
Larry2013z
Posts: 8
Joined: Sun Mar 21, 2021 7:42 pm

Loading the same file twice

Post by Larry2013z »

Hi Joel,
I am using ES3 to save and load prefabs that I spawn at runtime. When I load two different files, the objects from both files are now in the scene. I want the behavior that only the objects from the second file are in the scene. I am having trouble with this. My approach is to delete all objects from the first load just prior to loading a new file. Here is how I do that.

var foundSpawnedObjects = FindObjectsOfType<ES3Prefab>();
foreach (ES3Prefab es3Prefab in foundSpawnedObjects)
{
if(es3Prefab.gameObject != null )
{
Destroy(es3Prefab.gameObject);
}
}

This works great as I can keep going back and forth between two files and just the most recent is in the scene.

But with another case, if I try and reload the same file twice in a row, the second time I load I have no objects in the scene. In other words, I delete the objects that the first file loaded in and then when I load the same file again I don’t get those objects in my scene. If I load yet one more time they appear.

Why is this happening? Is there a smarter way for me to do this?

Thanks,
Larry

TL;DR: If I delete the objects loaded by file01.es3 just prior to reloading file01.es3, the objects do not appear in scene.

Here are some things I tried and the results:
Case 1: Call ES3AutoSaveMgr.Current.Load(); on the same file multiple times
Result 1: Loads in the objects just once (Does not have any duplications).

Case 2: Call ES3AutoSaveMgr.Current.Load(); sequentially on two different files (MyFile01.es3 & MyFile02.es3) (Using ES3AutoSaveMgr.Current.settings.path to change the filenames).
Result 2: Keeps the objects from the first load in scene and loads the new objects. If either file is subsequently reloaded there are no duplications.

In Case 3, I don’t want the objects from MyFile01.es3 to stay in the scene when I load MyFile02.es3
Case 3: Call ES3AutoSaveMgr.Current.Load(); on MyFile01.es3. Delete those objects. Call ES3AutoSaveMgr.Current.Load(); on MyFile02.es3.
Result 2: Works as expected, only the objects from MyFile02.es3. are in the scene.

Case 4: Call ES3AutoSaveMgr.Current.Load(); on MyFile01.es3. Delete those objects. Call ES3AutoSaveMgr.Current.Load(); on MyFile01.es3 (the same file twice)
Result 2: Does NOT work as expected, no objects are in the scene. If I try loading a third time the objects finally load.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Loading the same file twice

Post by Joel »

Hi there,

This is expected behaviour because Unity doesn't destroy the objects immediately, they're just marked as destroyed and don't actually get destroyed until the end of the frame. For more info see Unity's documentation for Destroy: https://docs.unity3d.com/ScriptReferenc ... stroy.html

If you're not already, I recommend using the latest version of Easy Save as there are some situations where we can detect this. However, it's not possible to tell whether an object is marked for destruction in every situation. In these situations you can use ES3ReferenceMgr.Current.Remove(obj) to remove your objects from the manager.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Larry2013z
Posts: 8
Joined: Sun Mar 21, 2021 7:42 pm

Re: Loading the same file twice

Post by Larry2013z »

That makes sense. So I am starting to destroy the objects, ES3 sees that they are still there and so doesn't load them. The frame ends and the objects are actually destroyed and so I end up with no objects in scene.

Just to be clear, would I use your function like this?

var foundSpawnedObjects = FindObjectsOfType<ES3Prefab>();
foreach (ES3Prefab es3Prefab in foundSpawnedObjects)
{
if(es3Prefab.gameObject != null )
{
ES3ReferenceMgr.Current.Remove(es3Prefab.gameObject)
Destroy(es3Prefab.gameObject);
}
}
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Loading the same file twice

Post by Joel »

That's correct.

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