WITH_EDITOR vs GIsEditor. What is the difference?

WITH_EDITOR vs GIsEditor. What is the difference?

The preprocessor will “remove” code when packaging standalones.

WITH_EDITOR is code that gets compiled when building for the editor. GIsEditor is set to true when you are running the editor (or PIE within the editor).

2 Likes

WITH_EDITOR is a macro.
GIsEditor is a global boolean variable.



#if WITH_EDITOR
//Code in here won't be present in standalone builds. It will be "removed"
#endif

if(GIsEditor)
{
//Code in here will be present in standalone builds, it just won't execute because GIsEditor will always be false.
}


1 Like

Why would someone use GIsEditor over WITH_EDITOR? For example, look at this code https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/Runtime/AIModule/Private/AIModule.cpp#L55-L66



#if WITH_EDITOR 
    FModuleManager::LoadModulePtr< IModuleInterface >("AITestSuite");

    if (GIsEditor)
    {
        // Register asset types
        IAssetTools& AssetTools = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools").Get();

        // register AI category so that AI assets can register to it
        AIAssetCategoryBit = AssetTools.RegisterAdvancedAssetCategory(FName(TEXT("AI")), LOCTEXT("AIAssetCategory", "Artificial Intelligence"));
    }
#endif // WITH_EDITOR 


If GIsEditor is never true unless WITH_EDITOR is defined, why use both?

Some Editor modules are still loaded and usable on Development builds of a game, in Shipping builds they are always stripped out.

WITH_EDITOR means you build with the Editor (compiling)

GIsEditor means you are running the Editor (running)

You can build with the Editor, but can run standalone (WITH EDITOR true, GIsEditor false)

3 Likes

Interesting, thank you. I think it is a subtlety that I have not yet needed, but I understand now.