Spawn new Box Brush in C++

Like a topic name. The best answer would be a function to spawn brushes during the game. I know the answer from:

but I am still not convinced. I want to test it myself. Also, one would consider creating complicated objects with brushes in editor (like fractal-like structures), which is obviously not possible without scripting.

1 Like

I have found it, and it took me (only) two days. To be honest it’s not that hard, but unnecessary complicated - maybe it will evolve into pull request of source code. Here is the code which I used to spawn new Box Brush:

UWorld *World = GEditor->GetEditorWorldContext().World();
ABrush *NewBrush = World->SpawnBrush();
NewBrush->BrushBuilder = NewObject<UBrushBuilder>(NewBrush, UCubeBuilder::StaticClass(), NAME_None, RF_Transactional);
NewBrush->Brush = NewObject<UModel>(NewBrush, NAME_None, RF_Transactional);
NewBrush->Brush->Initialize(NewBrush, false);
NewBrush->BrushType = EBrushType::Brush_Add;
NewBrush->BrushBuilder->Build(NewBrush->GetWorld(), NewBrush);
NewBrush->SetNeedRebuild(NewBrush->GetLevel());
GEditor->RebuildAlteredBSP(); 

Honestly, it needs some tutorial or explanation in Documentation. Single SpawnBrush function SHOULD get you complete and usable object, not empty uninitialized actor. Also, SpawnActor function is used only 3 (three!) times in source code, according to GitHub.

what’s the version of your UE4

hello, can you spawn a box brush? why did i can’t compile four line UCubeBulider must be a class or namespace name,seven line pointer to incomplete class type is not allowed…please help me …thanks

in the end can you solve this problem? if you have a good idea ,can you share for me?

You have the answer on the following link https://answers.unrealengine.com/questions/25346/how-to-use-editor-classes-from-within-an-actor-cla.html.

Basically you have to add the UnrealEd module Into your Build.cs file. You have to have something like this:

PublicDependencyModuleNames.AddRange(new string[] { “Core”, “CoreUObject”, “Engine”, “InputCore”, “UnrealEd” });

And recompile your project again.

How to change Brush SurfaceMaterial by C++ ?