Can't Spawn Actors in Construction, so now what?

So lets take a simple example.

Blueprint only I assume?


Blueprint - House
Blueprint - Factory
Blueprint - Farm

Blueprint - Town Generator
Blueprint - Spawner

Lets say, for arguments sake when I press the “1” key it spawns a bunch of buildings, randomly. I can then individually edit the houses.

Inside “Town Generator” create a function that chooses random meshes and creates an array of meshes.

Inside “Spawner” create a function for when the “1” even is fired off.

Inside each Blueprint i.e. “House” etc. Create a new function for “Onclicked” so that you can change the mesh(building type) etc.

I already did try this with procedurally generated blocks and it works.

Wait you want to do it from inside the editor only?

I don’t get it. Your not trying to make a game? Are you trying to make tools?

The answer is, you can’t spawn actors in the constructor, so you won’t be able to spawn the actors in the editor.

You need to create an editor plugin. Effectively a tool to generate villages.

Well one example would be a village where you would want to spawn a number of buildings procedurally. You want to be able to move the whole thing as a whole or quickly place a number of these at different places. After they’re placed you might want to go in and change gameplay related properties like what kind of building this is (e.g House / Factory / Farm).

Are you doing this without starting the game in the editor? If so I will definitely try this as soon as I get home.

Ok lets start with just having a Village BP. If you drag this out in the Editor it’ll procedurally place a bunch of meshes for buildings around. This makes it quick and easy to create “villages”. This is also not hard to do with ChildActorComponent or StaticMeshComponent.

The problem with this is that the Village Actor will act as one object. You can’t select one of these ChildActors inside the Editor and tweak it’s properties.

If you on the other hand drag a bunch of House BPs out in the world randomly, you can have a different Village BP look for these and use Attach Actor to Actor to attach them to the Village Actor (ie. itself). If done this way you can still select each House individually, but you can also move all of them with the Village Actor. This is what I want, and I think joessu too, but the size of the proverbial village may be too large to effectively drag each house out manually etc.

Excellent, now we’re getting somewhere. I will look into this when I get a chance, thank you.

Seems weird to me that actors can’t be spawned in the construction script, even if added as children. I noticed that child actors can’t be selected in the hierarchy either, which is interesting.

Plugins? really? I think i would just modify the unreal source to allow spawning actors at construction time. Maybe inherit from a class that tags the actors that are created and deletes any actors of that type anytime construction is rerun.

Sure you can edit the engine source code. It would be better to make a plugin and then you could easily maintain it from engine version to version, and you could share it easily, plus you will most likely want to add more features.

Have you found a solution so far? I have the same problem here

We worked around it. Luckily our game is small enough to warrent manual placement of actors. Basically we have an array of actors that we set, and then the construction script loops through and positions them accordingly.

So to be clear, no we do not have a solution for this, but managed to not need it.

Can you post some skeleton of your solution to help people with their constant struggle?

Imagine that I need a picture made of that Cells with different colors. Something like pixel art. Should I do it by hand cell by cell?

If you’re really really sure you want to spawn actors in a construction script (you’ll have to manage their lifetime yourself), you can put this helper function into a blueprint function library:

UFUNCTION(BlueprintCallable, Category="Actor|Utilities", meta=(Keywords="spawn actor"))
AActor* SpawnActor(UWorld* World, UClass* Class)
{
    FActorSpawnParameters fp;
    fp.bAllowDuringConstructionScript = true;
    return World->SpawnActor<AActor>(Class, fp);
}

You can’t use this code for a BP Function, because BP doesnt allow you to set the world parameter, it just wont save the bp.
What you have to do instead, is to get a reference to an Object, from this object get the world. Replace UWorld* World with UObject InputObject, and World-> with InputObject->getWorld()->

Same problem here, this is really annoying. I need this to switch between different lights attached to a blueprint in the editor.

Blutilities have come out since this was posted… which is what we use now. At the time this was posted there was no way

This worked for me!!! This is the answer ive been looking for thank you!