Can someone explain, in-depth, why we execute SaveableEntity.cs in edit mode?

In this lecture Sam talks about the woes of not having Unity updating in edit mode and he shows examples of how the GUID is not being updated since it is set as a [SerializeField] and so we go through the process of making the SaveableEntity.cs attributed with [ExecuteAlways] and we edit the Update() to only run when the Application is not playing. Sure, but why? I really don’t understand it clearly. I I mean I understand the words said but I don’t feel like he explained why this was an issue in a clear way? Maybe it is brought up again further into the series but I have a hard time just leaving this and moving on until I understand why we are doing it this way.

I know this section is not for beginners but I have dealt with way more complex systems in c# prior to this and I am not unfamiliar with the code, I am very unfamiliar with this particular implementation though and if anyone had time to properly explain why we care about the scripts behavior in edit mode when the game never will be played in edit mode, please enlighten me.

Thank you

EDIT:

To clarify I can loosely describe what I am guessing the reason to be:

The way I (not really) understand it, we are going through all of this because we want to solve the issue of persistence between loads due to the fact that we are using [SerializeField] to achieve that.

And why we are using [SerializeField] to do that is because once we build the Application we want the GUID to have been set for all SaveableEntities and not be generated again at any point during the Applications lifetime.

The first enemy the player faces will have the same GUID for every player playing our game every time they restart and it will be the same as what we set it to now. But since the GUID won’t update correctly due to Unity not reliably updating the SerializeField in Edit mode we can’t 100% ensure that all the GUIDs will be correctly set by the time we decide to build the application unless we can update them while in Edit mode?

1 Like

You’re heading in the right direction.

We need each SaveableEntity in the scene to have a unique UUID because that UUID is how we identify the entities for the purposes of saving. The problem is that when you bring the prefab into the scene it won’t have a UUID… and if we duplicate the gameobject in the scene it will have the same UUID as the one we duplicated… meaning it won’t be a unique UUID…

So we need to change the UUID if it is empty, or if there are any other objects with the same UUID. That’s not something we want to have happen when we’re running the game, it needs to be established in the editor. Now we could simply create a GUI button “CreateUUID” that generates a new UUID if the button is clicked in the inspector, but that puts the responsibility on the designer to make sure each SaveableEntity has a unique UUID, and as such is not at all ideal.

Enter our [ExecuteAlways] code, specifically the Update…
We have a static Dictionary to maintain a list of all SaveableEntities in the scene indexed by their UUID.
Then in the Update() loop, first we check to make sure we’re not actually running the game, then we check to see if the UUID exists in the Dictionary… if it doesn’t exist, then we’re unique, and we add ourselves to the dictionary. If it DOES exist, then we check to see if we’re the entity. If we are the entity in the dictionary, we do nothing. If we’re NOT the entity in the dictionary, then we change our UUID and add ourselves to the dictionary.

The confusing part is the editor code… the SerializedObject and SerializedProperty code is most commonly used to make custom editors and custom inspectors.

SerializedObject serializedObject = new SerializedObject(this) simply creates a wrapper around the object so we can access it’s properties.
SerializedProperty property = serializedObject.FindProperty(“uniqueIdentifer”); uses this wrapper to reach into the object and get the uniqueIdentifier field.

We use this because simply changing the value in code doesn’t tell Unity’s Editor that the value of the property has changed and needs to be saved.

2 Likes

Thank you Brian, cleared it right up. Appreciated.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms