Dynamically spawning actors in the editor

Hello everyone,

I want to make an actor that dynamically spawns other actors. For that my actor is calling GetWorld()->SpawnActor().

Also, I want that to happen in the Editor so I can easily see and work with the result. This means that I can, for instance, do that in BeginPlay(), so I tried doing it by overriding OnConstruction. It works - kinda:

  • There are many, many items being spawned, so OnConstruction is called numerous times, I still don’t know when. The documentation states “may get rerun in the editor when changing blueprints” but I’m not sure what that means - what blueprints are being changed?

  • If I set the current object to be the parent of the spawned objects, Unreal crashes. I do that by setting FActorSpawnParameters::Owner = this, which I’m not sure it’s the way to go about it.

2 Likes

Hi there! Can you tell more - for that purpose do you want to see spawned actors? To look at there locations, density, etc?Or do you want to edit their props, names and so on?

@Kehel18 hi! I just want to see them in the editor, actually. Those actors are based on another blueprint/C++ class.

Hi HeavyStorm

What you can do is create an editor utilities actor (or widget) to manage your actors in the world.

  1. Create a new blueprint actor called BP_EditorUtilities

324598-actors-1.png

  1. Add two variables ActorToSpawn (Actor class reference) and LocationToSpawn (Transform reference)
    Make sure both variables have “Expose and Spawn” and “Instance Editable” checked.

  1. Add an event to the event called “SpawnActor”
    Ensure “Call in editor” is checked

  2. Add the logic to spawn an actor using the variables.

  1. Add the BP_EditorUtiltiies to the level and use the exposed variables and event to spawn your desired actors.

Obviously, you can customize it make to make to much more than just spawning actors. Using tools like this can definitely help automate your level management.

EDIT: Oops I didn’t realize you were looking for a c++ solution. It should be fairly simple to migrate this blueprint to C++. I could write it out for you if you get stuck.

I hope this helps

Alex

2 Likes

Alekann01, thanks a lot for your very detailed solution. I think the crux of it is the “call in editor” option, exactly what I’m looking for. I’ll look into how to do it in C++. Also, sorry for taking this long to look into your answer - I’m currently learning many different things and moving between topics rather erratically.

Hi HeavyStorm.

Glad you found my answer helpful.

In your header:
UFUNCTION(BlueprintCallable, CallInEditor)

void SpawnActor();

and then in your cpp add the appropriate definition logic.

Good luck with the rest of your project(s).

Alex

Peace Alekann01,

Sorry I know it’s been awhile since this post, but I’m trying to make something similar and the Spawn Actor button doesn’t show in editor if I make it as an Editor widget/actor, it only shows if it’s a normal blueprint

So I wanted to ask in your screenshot the button for custom event does show on your editor utilities widget so is there an extra step to enable it other than setting the event as “call in editor”?