Dynamic Actor Spawn / Respawn

Hi,

I am currently working on a JRPG-like system where I have switches which should be usable to control actors.

Think of a switch as a global boolean variable which can be set to either On or Off and is automatically saved and restored from save games. The switch system is already working, and my switch class is called UJRPGSwitch, which is a PrimaryDataAsset for now.

I can then set or unset / reset a switch at any time during gameplay, for example in dialogues etc. So far so good.

Now I made a UJRPGSwitchComponent class, which is an ActorComponent. In this component, the user can provide an array of switches which need to be set to ON in order for the owning actor of the component to be spawned.

So what this component should do exactly is:

  • On BeginPlay: Check if all provided switches are ON
  • If at least one of them is OFF, Destroy the owning actor immediately
  • Otherwise: Start ticking with an interval (no need for realtime tick. Also, later I might change the behavior from tick to events when a switch is changed, but for now tick works fine at the current scale)
  • On Component Tick:
    • Check all provided switches
    • If one of them is OFF, Destroy owning actor

This works so far for destroying the owning actor if any given switch is set to OFF either when the owning actor spawns or during gameplay, great.

But I also want the opposite to be true: If I set all required switches to ON, I want the owning actor to be spawned again. And this is where it becomes tricky.

I came up with a solution which works in all cases except for one, and THIS is my actual question. My solution was:

When the owning actor is destroyed by the UJRPGSwitchComponent class, it also spawns a new invisible actor in place, which I called AJRPGActorRespawner. This actor gets a list of the switches from the UJRPGSwitchComponent as well as the Actor class.

The AJRPGActorRespawner class then also has logic in it’s spawn and tick methods, similar to the UJRPGSwitchComponent, but in reverse:

  • Check if all provided switches are ON
  • If all are ON, spawn a new actor from the class stored as a property and destroy the AJRPGActorRespawner

This actually works, but it only spawns an actor with it’s default property set (obviously). For now, I would be fine with that, if it wasn’t for in-level-actors, which I kind of want to support. That is, if I would add a static mesh into the world, and then attach it a JRPGSwitchComponent so that it will despawn if certain switches were not set on spawn, and then I would like it to respawn once all required switches are set, it would simply just spawn an empty static mesh actor right now.

I tried saving the destroyed actor as TemplateActor property in the AJRPGActorRespawner class, but this didn’t work either because actor would still be garbage collected after some time, so this behavior is not suitable.


So after A LOT of context:
Is there a clever way to also support these in-level actors, without touching a lot of major engine systems?

Two ideas:

  1. I might be good to have a central spawner in the level. Because, what deals with the case where the switches are ON, but nothing has been spawned yet?

  2. When actors are spawned, and others are attached to them, or some other variation, I think it would be good to put this in the save game. Keep a log of the current config there. Then, when you respawn or restart the game, the central spawner can read the save game and spawn enough in the world to get things back to how they were. ( It would not make all the corrections / attachments, that would be up to the individual actors to regenerate ).

Hi @ClockworkOcean,

thanks for your reply. As for the first case: The actor is always initially spawned, as each Actor will be placed in the editor. Afterwards, it might get configured with the switch component to tell when it should stay spawned after BeginPlay. So there will never be the need to spawn entirely new actors using the switch system during runtime, only respawn potentially destroyed actors that have been spawned during development time in the editor.

And second: I currently save the switch state in the save games, but you are certainly right - I would also need to save any custom actor properties there as well. So my best guess is then to not only write the switch state but to find a way to serialize all kind of additional data that could make up a dynamic actor - or stay with the default state of an actor after spawning, but then have every actor diff as a separate blueprint.

You could save the switch state, too. But, also any customizations on the actors.

You would also need some sort of grace period ( after spawning ), I’m guessing, where the actors have time to adapt to possible inconsistencies between the switch state, and their customizations, just after spawn.