Something not mentioned here, but I will add this for the future readers.
There are always two parts to the build system. Including the correct file via the include directive is the first step.
The second step is to also include the correct module that the include directive is referring to. This is done via your modules YourModule.build.cs file, just outside your modules source directory.
For Projects this is Source/ModuleName/*build.cs
For plugins it’s Plugins/PluginName/Source/ModuleName/*build.cs
There are two main distinctions for modules, which are defined either in a .uplugin or .uproject files Json. They can either be Runtime or Editor modules.
Runtime modules can reference all types of modules, while editor modules can only reference editor modules. Since you are looking for GEditor, this will be defined in an editor module, and so if you are linking against this inside a runtime module - you’ll need to do some extra work to ensure that the runtime module doesn’t try to reference things OUTSIDE the Editor.
Lets look at a build.cs file for a runtime module needed for referencing GEditor.
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "Slate", "SlateCore", "UMG", "DeveloperSettings", "Projects", });
PrivateDependencyModuleNames.AddRange(new string[] { });
if (Target.Type == TargetType.Editor)
{
PrivateIncludePathModuleNames.AddRange(
new string[]
{
"Settings",
"UnrealEd",
});
PublicIncludePathModuleNames.AddRange(
new string[]
{
"Settings",
"UnrealEd",
});
PrivateDependencyModuleNames.AddRange(
new string[]
{
"UnrealEd"
}
);
PublicDependencyModuleNames.AddRange(
new string[]
{
"UnrealEd"
});
}
}
IncludePathModuleNames are names of other modules that your .cpp (private) or .h (public) files are include directives are referencing. This makes it so that other modules that include YOUR module also get these paths available to them.
The DependencyModuleNames are the same idea, except this is to tell the linker that you are referencing types defined in those modules.
Notice how I’ve blocked my modules includes off to only happen when the runtime module is being build for the Editor and at no other time.
This means both your include directive and your code referencing GEditor directly need to be wrapped in the #if WITH_EDITOR macro so it also doesn’t get compiled outside the editor context, as those modules will not exist because they are Editor modules.
So to get the GEditor global var to work in a runtime module, you need to modify the build.cs to support it and do something like this
#if WITH_EDITOR
#include "Editor/EditorEngine.h"
#endif
static bool IsEditorEngine()
{
#if WITH_EDITOR
if(GEditor) return true;
#endif
return false;
}