Build-in and runtime objects at the same time


What if we want to have objects that can be either spawned by a spawner and added to scene in editor view?

I will use an example of EnemyPrefab & EnemyController.cs here.

What I want requires of course the SaveableEntity component for the EnemyPrefab, so the SavingManager will save all build-in enemies on scene that where added in editor. Then we need a Spawner to save all the enemies that it spawns or a controller that will search for all object with EnemyController.cs. But there will be a collision / problem with saving single enemy twice - by SavingManager (becouse prefab must have a SaveableEntity and SavingManager will always capture it) and by spawner/controller (becouse we want to have spawned enemies also).

I’ve come up with an option to solve it, but it doesn’t sound like a best sollution, so I hope for some other solutions :slight_smile:

Potential Solution: Create prefab variant of EnemyPrefab called for eg. “EnemyPrefabSaveableEntity” that will have SavebaleEntity component attached. The first prefab can’t have SaveableEntity. When you add an enemy on the scen then you use second prefab with SaveableEntity, when you’re adding enemy prefab for spawner then you use the normal prifab, not the variant. The problem with this way is to always remeber to use proper prefab when adding to scene and when adding in inspector for spawner.

Also I was thinking about allowing the scripts to create UUIDs in the runtime and then use other scripts to save/load the objects that were spawned and the SavingManager will ignore them (eg. if the prefab implements the IEnemy interface, then SavingManager will ignore it or pass the save/load to the proper script or something like that). But I’m less concerned about this solution than the previous one.

It should be possible to leave the SaveableEntity component on the enemies. No need for two Warrior prefabs, for example (unless you have two different warriors).

So here’s the rough cut of what you’ll need to do:
For enemies placed within the scene, these will be picked up as SaveableEntities and saved/loaded as normal.
For enemies spawned by an EnemySpawner, the Spawner should destroy the new Enemy’s SaveableEntity component, and instead maintain a list of the enemies that it has spawned. In CaptureState, it will create a record for each surviving enemy, manually capturing the values from each of the components on the enemy (foreach ISaveable saveable in enemy.GetComponents())

In RestoreState, it will spawn an enemy based on each record in the State.

We’ll actually be dealing with a similar model in the Inventory course to deal with the dropping of random items when enemies are killed.

1 Like


Thanks for the answer. I came up with same solution after posting the question, but it’s good to know that is a good way to handle it. I’ve already bought the Inventory course system, so I will look forward to that lecture :slight_smile:

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