Hi! I want to detect in my code if the play mode is set to “Play” (Selected Viewport) or “Simulate in Editor”. I know I can get information about the world type through “GetWorld()->WorldType” but it returns “PIE” for both “Play” and “Simulate in Editor”. Is there any other way to distinguish them?
You can check the UEditorEngine’s bIsSimulatingInEditor property, like this:
if (UEditorEngine* editor = CastChecked<UEditorEngine>(GEngine))
{
return editor->bIsSimulatingInEditor;
}
return false;
Not working at all. If there is Cast - project not compiling with LNK2019, if you write just (UEditorEngine*)GEngine - then it’s always nullptr.
Are you including EditorEngine.h?
It’s more like this actually:
bool myClass::IsPIE() const {
return ( GEditor->bIsSimulatingInEditor || ( GEditor->PlayWorld != NULL ) );
}
Thank you! That works perfectly.
Also include for this “Editor.h” and module “UnrealEd” goes to .Build.cs file.
And now the relevant variable changed to IsSimulateInEditorInProgress
:
// ProjectName.Build.cs
if (Target.bBuildEditor)
{
PublicDependencyModuleNames.Add("UnrealEd");
}
// somewhere else in your project:
#if WITH_EDITOR
#include "Editor.h"
#endif
// ...
#if WITH_EDITOR
if(GetWorld()->WorldType == EWorldType::PIE && GEditor->IsSimulateInEditorInProgress())
{
return nullptr;
}
#endif
EDIT: if you don’t check for WorldType == EWorldType::PIE
first, you create a crash. Running the game as “standalone game” from within the editor is running an editor built and WITH_EDITOR
evaluates to true, but GEditor->IsSimulateInEditorInProgress()
then causes an access violation. Maybe GEditor
isn’t available.