Download

How to spawn meshes and actorns inside the editor

With blueprints i have been able to add meshes to an actor with blueprints that are visible directly in the level editor.
When i do the same in C++ the mesh will only become visible when i start the game, which is not so practical…
Is it possible to write a C++ class that can be used directly in the level editor without having to create a BP class based on the C++ class?

.h-file


UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mesh")
    UStaticMesh* mesh;

.cpp-constructor


UStaticMeshComponent* meshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
meshComponent->SetStaticMesh(mesh);
RootComponent = meshComponent;

probable that you still need to use your C++ class in a construction BP

Is that the case? So you cant write a game without BPs and still using functions from the editor, like placing actors in the level?

Solved

After hours and hours of googling, coding and hard work i finally managed to find the answer to my own question :slight_smile:

This is how you “spawn” an actor from C++ so that it will be visible inside the level editor:

Add this code to the constructor:



UChildActorComponent* ChildActor;
ChildActor = CreateDefaultSubobject<UChildActorComponent>(TEXT("Test"));
ChildActor->SetChildActorClass(*Actor class to "spawn"*::StaticClass());
ChildActor->CreateChildActor();
RootComponent = ChildActor; 


This example might not be so useful. But the idea can med used to to do procedural generated levels by placing several actors on the scene with one “master” actor. Quite useful if you ask me…