Loading the same file twice
Posted: Fri Apr 02, 2021 4:28 pm
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.
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.