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?