References in RefMgr growing during gameplay.

Discussion and help for Easy Save 3
Post Reply
Kyper
Posts: 9
Joined: Sat Jun 18, 2022 8:11 pm

References in RefMgr growing during gameplay.

Post by Kyper »

I have an object that is an ES3 prefab that gets loaded. I then create instances of this object at a pretty high rate. They only live for a few seconds before they are destroyed. I am seeing these objects get added to the References list in the RefMgr and they are not getting removed when the object is destroyed. I can click Optimize to clean these out, but that isn't really a solution when they're being added during the gameplay. The list ends up with thousands of references to deleted objects.

I assume this is because I use "Add All Prefabs To Manager", but I really need this feature for upkeep. I have a ton of prefabs. Is there a reason this feature works during play as well?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: References in RefMgr growing during gameplay.

Post by Joel »

Hi there,

Just to clarify, are you saying that the references in the manager persist after exiting play mode?

The reference manager is a database of all of the references in your scene. If Easy Save needs to instantiated an object/reference, it needs to register it with the reference manager to be able to resolve it correctly.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Kyper
Posts: 9
Joined: Sat Jun 18, 2022 8:11 pm

Re: References in RefMgr growing during gameplay.

Post by Kyper »

No. Using Instantiate() on something loaded in that is an ES3Prefab adds it as a reference to the RefMgr. When it is destroyed, it does not get cleaned up from the RefMgr. I would assume that ES3Prefab class would do this on Destroy(), but it doesn't.

I must be doing something unusual if this hasn't come up before. I'm mainly looking for advice.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: References in RefMgr growing during gameplay.

Post by Joel »

Hi there,

When you destroy a GameObject, Unity should turn all of it's references to null. We don't automatically remove these in Destroy because it's quite common for save code to be called here, and there's no guarantee that the references would be removed from the manager before or after save code needs to be executed.

If you wish for these values to be removed you can call the ES3ReferenceMgr's RemoveNullOrInvalidValues() method.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Kyper
Posts: 9
Joined: Sat Jun 18, 2022 8:11 pm

Re: References in RefMgr growing during gameplay.

Post by Kyper »

Okay. This has me thinking I might be doing something wrong. What I have, is a player-customizable bullet that get's instantiated many times. In the "creator" I am saving an instance of it and during gameplay, I am treating it like a prefab after it is loaded. Perhaps this is not a smart way to use ES3?

I am afraid that "RemoveNullOrInvalidValues" is a little too heavy-weight to call upon the destruction of each bullet.
I see that the refIds are stored in a dictionary and ES3ReferenceMgrBase offers a Remove() method. Would it be reasonable for these bullets to .Remove() themselves from the ReferenceMgr OnDestroy? I don't know if this has any dependencies it could break.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: References in RefMgr growing during gameplay.

Post by Joel »

Hi there,
Would it be reasonable for these bullets to .Remove() themselves from the ReferenceMgr OnDestroy? I don't know if this has any dependencies it could break.
If you're not performing any saving/loading in OnDestroy then you could indeed add a script to the bullet which removes itself. We don't do this automatically ourselves because it's quite common for people to use OnDestroy for saving, and this would cause unpredictable behaviour depending on the order that OnDestroy is called for each GameObject (Unity doesn't guarantee order).
What I have, is a player-customizable bullet that get's instantiated many times. In the "creator" I am saving an instance of it and during gameplay, I am treating it like a prefab after it is loaded. Perhaps this is not a smart way to use ES3?
When you instantiate a GameObject with an ES3Prefab Component you're telling Easy Save that you'll be saving this instance. In response Easy Save populates the reference manager with all of it's references.

In your case it would be best to remove the ES3Prefab Component only save/load the aspects of the GameObject which change (i.e. Components or fields rather than the entire GameObject). Then during gameplay you can instantiate your own bullet and load the data into that. For Components you can use ES3.LoadInto to load data into an existing Component.

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