How to use editor classes from within an actor class

I’ve added the following to the top of my HelloWorldPrinter.h file (which was created from this programming starter documentation project). The goal is to auto create a new cube from code within the editor as a proof of concept.

#include "UnrealEd.h"

I then have the following in my .cpp, which intellisense seems to agree with just fine. Which was borrowed from Unreal’s own test code in the repo.

UWorld* World = GEditor->LevelViewportClients[0]->GetWorld();
UCubeBuilder* cubeBuilder = Cast<UCubeBuilder>(GEditor->FindBrushBuilder(UCubeBuilder::StaticClass()));
cubeBuilder->X = 4096.0f;
cubeBuilder->Y = 4096.0f;
cubeBuilder->Z = 128.0f;
cubeBuilder->Build(World);
GEditor->Exec(World, TEXT("BRUSH MOVETO X=0 Y=0 Z=0"));
GEditor->Exec(World, TEXT("BRUSH ADD"));

However, when I go to build I get the following error (yadayada = the long path to my project).

yadayada.....lib and object yadayada.exp

HelloWorldPrinter.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) private: static class UClass * __cdecl UCubeBuilder::GetPrivateStaticClass(wchar_t const *)" (__imp_?GetPrivateStaticClass@UCubeBuilder@@CAPEAVUClass@@PEB_W@Z) referenced in function "class UCubeBuilder * __cdecl Cast<class UCubeBuilder>(class UObject *)" (??$Cast@VUCubeBuilder@@@@YAPEAVUCubeBuilder@@PEAVUObject@@@Z)

HelloWorldPrinter.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class UBrushBuilder * __cdecl UEditorEngine::FindBrushBuilder(class UClass *)" (__imp_?FindBrushBuilder@UEditorEngine@@QEAAPEAVUBrushBuilder@@PEAVUClass@@@Z) referenced in function "public: virtual void __cdecl AHelloWorldPrinter::BeginPlay(void)" (?BeginPlay@AHelloWorldPrinter@@UEAAXXZ)

HelloWorldPrinter.cpp.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) class UEditorEngine * GEditor" (__imp_?GEditor@@3PEAVUEditorEngine@@EA)

yadayada.dll : fatal error LNK1120: 3 unresolved externals

Is there a better way to include the editor classes and have them resolved? Or am I doing something wrong.

Are you linking to the UnrealEd module?

1 Like

Probably not, how do I do that?

In the .Build.cs file for whatever you’re working on, they’ll be a list of PrivateDependencyModuleNames and you’ll likely just need to add “UnrealEd” to that list (alternatively it may need to go into PublicDependencyModuleNames depending on where/how you’re using it).

For more information on UBT module dependencies, see: What is the difference between PublicDependencyModuleNames and PrivateDependencyModuleNames - Plugins - Epic Developer Community Forums

I’m curious about how you managed to include UnrealEd.h without that though, so perhaps you already have it.

1 Like

That worked! I was messing with build.cs at one point, but it wasn’t in there just now, so maybe something got cached.

1 Like

hello,Can you give me a complete code?

So, this fixes the compilation error building the editor, but then if you add UnrealEd to your build.cs file you get errors when compiling non-editor builds like “Development”. is there another solution?

Can’t use editor code outside editor builds. Best not to rely on that code during runtime. You can use

#if WITH_EDITOR

#endif // WITH_EDITOR

to wrap your editor only code so it doesn’t try to compile, but if you gotta have it for your concept/feature/game to work, then best to move on to your own solution or to a different concept/feature/game.