I know this thread is very old but the solution I found gives very precise results through the enumerator “EWorldType::Type” present in “Engine/EngineTypes.h” and you can easily extend it for PIE, Game, GamePreview …
Additionally, you can ask every UObject where it is currently running.
// Utils.h
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Utils.generated.h"
UCLASS()
class TTCOMMON_API UUtils : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
// returns true if the given 'Object' is instantiated and running in Editor Preview (e.g. Persona, AnimBP...)
UFUNCTION(BlueprintCallable, Category = "Utils", meta = (Keywords = "editor tools"))
static bool IsRunningInEditorPreview(UObject* Object);
};
// Utils.cpp
#include "UUtils.h"
#include "Engine/EngineTypes.h"
bool UUtils::IsRunningInEditorPreview(UObject* Object)
{
if (!Object)
{
return false;
}
if (auto world = Object->GetWorld())
{
return world->WorldType == EWorldType::EditorPreview;
}
return false;
}