simplify getworld implementation

This commit is contained in:
Mamadou Babaei
2024-08-16 16:37:24 +02:00
parent 2691f32492
commit 3594410478
@@ -42,9 +42,6 @@
#include "Engine/GameViewportClient.h"
#include "Engine/World.h"
#include "Subsystems/EngineSubsystem.h"
#if WITH_EDITOR
#include "Subsystems/UnrealEditorSubsystem.h"
#endif /* WITH_EDITOR */
UWorld* FSGEngineUtils::GetWorld()
{
@@ -55,40 +52,19 @@ UWorld* FSGEngineUtils::GetWorld()
{
if (IsValid(GEditor))
{
const bool bIsPIE = GPlayInEditorID != -1;
if (!bIsPIE)
{
const FWorldContext* WorldContext{GEditor->GetPIEWorldContext(1)};
if (WorldContext)
{
World = WorldContext->World();
}
}
else
{
const FWorldContext* WorldContext{GEditor->GetPIEWorldContext(GPlayInEditorID)};
const bool bIsPIE = GPlayInEditorID != -1;
const int32 WorldPIEInstance = bIsPIE ? GPlayInEditorID : 1;
const FWorldContext* WorldContext{GEditor->GetPIEWorldContext(WorldPIEInstance)};
if (WorldContext)
{
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))
{
UUnrealEditorSubsystem* UnrealEditorSubsystem{GEditor->GetEditorSubsystem<UUnrealEditorSubsystem>()};
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();
}
World = GEditor->GetEditorWorldContext(false).World();
}
}