simplify getworld implementation

This commit is contained in:
Mamadou Babaei
2024-06-07 13:39:32 +02:00
parent f956c8d5ce
commit 0c71ab9ab8
@@ -42,9 +42,6 @@
#include "Engine/GameViewportClient.h" #include "Engine/GameViewportClient.h"
#include "Engine/World.h" #include "Engine/World.h"
#include "Subsystems/EngineSubsystem.h" #include "Subsystems/EngineSubsystem.h"
#if WITH_EDITOR
#include "Subsystems/UnrealEditorSubsystem.h"
#endif /* WITH_EDITOR */
UWorld* FSGEngineUtils::GetWorld() UWorld* FSGEngineUtils::GetWorld()
{ {
@@ -55,40 +52,19 @@ UWorld* FSGEngineUtils::GetWorld()
{ {
if (IsValid(GEditor)) if (IsValid(GEditor))
{ {
const bool bIsPIE = GPlayInEditorID != -1;
if (!bIsPIE)
{ {
const FWorldContext* WorldContext{GEditor->GetPIEWorldContext(1)}; const bool bIsPIE = GPlayInEditorID != -1;
if (WorldContext) const int32 WorldPIEInstance = bIsPIE ? GPlayInEditorID : 1;
{ const FWorldContext* WorldContext{GEditor->GetPIEWorldContext(WorldPIEInstance)};
World = WorldContext->World();
}
}
else
{
const FWorldContext* WorldContext{GEditor->GetPIEWorldContext(GPlayInEditorID)};
if (WorldContext) if (WorldContext)
{ {
World = WorldContext->World(); World = WorldContext->World();
} }
} }
/// NOTE
// IsInGameThread() check is to avoid EngineScriptHelpers::CheckIfInEditorAndPIE emitting:
// "You are not on the main thread."
// !(GEditor->PlayWorld || GIsPlayInEditorWorld) check is to avoid
// EngineScriptHelpers::CheckIfInEditorAndPIE emitting:
// The Editor is currently in a play mode.
if (!IsValid(World) && IsInGameThread() && !(GEditor->PlayWorld || GIsPlayInEditorWorld)) if (!IsValid(World) && IsInGameThread() && !(GEditor->PlayWorld || GIsPlayInEditorWorld))
{ {
UUnrealEditorSubsystem* UnrealEditorSubsystem{GEditor->GetEditorSubsystem<UUnrealEditorSubsystem>()}; World = GEditor->GetEditorWorldContext(false).World();
if (IsValid(UnrealEditorSubsystem))
{
/// NOTE
// This is exactly UnrealEditorSubsystem->GetEditorWorld() without all the annoying warnings that
// pollutes the logs. It's safe since we've already done all the checks.
World = GEditor->GetEditorWorldContext(false).World();
}
} }
} }