Skip to content

Commit 966815c

Browse files
committed
Credit system BP: make the BP loader an UEngineSubsystem
so that we don't have to manage its lifecycle and especially not CesiumCreditSystemBP's lifecycle either, which was causing issues when unloading then reloading levels in-game (Shipping).
1 parent 780f05f commit 966815c

File tree

3 files changed

+8
-22
lines changed

3 files changed

+8
-22
lines changed

Diff for: Source/CesiumRuntime/Private/CesiumCreditSystem.cpp

+5-19
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "Modules/ModuleManager.h"
2222
#endif
2323

24-
/*static*/ UObject* ACesiumCreditSystem::CesiumCreditSystemBP = nullptr;
2524
namespace {
2625

2726
/**
@@ -89,22 +88,6 @@ FName ACesiumCreditSystem::DEFAULT_CREDITSYSTEM_TAG =
8988

9089
/*static*/ ACesiumCreditSystem*
9190
ACesiumCreditSystem::GetDefaultCreditSystem(const UObject* WorldContextObject) {
92-
// Blueprint loading can only happen in a constructor, so we instantiate a
93-
// loader object that retrieves the blueprint class in its constructor. We can
94-
// destroy the loader immediately once it's done since it will have already
95-
// set CesiumCreditSystemBP.
96-
if (!IsValid(CesiumCreditSystemBP)) {
97-
UCesiumCreditSystemBPLoader* bpLoader =
98-
NewObject<UCesiumCreditSystemBPLoader>();
99-
CesiumCreditSystemBP = bpLoader->CesiumCreditSystemBP.LoadSynchronous();
100-
// Prevent the GC from destroying the object when unloading a level, as it will be reused
101-
// if one subsequently loads another level. This kind of static object should probably be
102-
// replaced by a "subsystem"
103-
// (https://dev.epicgames.com/documentation/en-us/unreal-engine/programming-subsystems-in-unreal-engine)
104-
CesiumCreditSystemBP->AddToRoot();
105-
bpLoader->ConditionalBeginDestroy();
106-
}
107-
10891
UWorld* world = WorldContextObject->GetWorld();
10992
// This method can be called by actors even when opening the content browser.
11093
if (!IsValid(world)) {
@@ -159,8 +142,11 @@ ACesiumCreditSystem::GetDefaultCreditSystem(const UObject* WorldContextObject) {
159142
spawnParameters.SpawnCollisionHandlingOverride =
160143
ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
161144
spawnParameters.OverrideLevel = world->PersistentLevel;
162-
pCreditSystem = world->SpawnActor<ACesiumCreditSystem>(
163-
Cast<UClass>(CesiumCreditSystemBP),
145+
UCesiumCreditSystemBPLoader* bpLoader =
146+
GEngine->GetEngineSubsystem<UCesiumCreditSystemBPLoader>();
147+
UClass* CesiumCreditSystemBP =
148+
Cast<UClass>(bpLoader->CesiumCreditSystemBP.LoadSynchronous());
149+
CesiumCreditSystemBP,
164150
spawnParameters);
165151
// Null check so the editor doesn't crash when it makes arbitrary calls to
166152
// this function without a valid world context object.

Diff for: Source/CesiumRuntime/Private/CesiumCreditSystemBPLoader.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
#pragma once
44

5+
#include <Subsystems/EngineSubsystem.h>
6+
57
#include "CesiumCreditSystemBPLoader.generated.h"
68

79
UCLASS()
8-
class UCesiumCreditSystemBPLoader : public UObject {
10+
class UCesiumCreditSystemBPLoader : public UEngineSubsystem {
911
GENERATED_BODY()
1012

1113
public:

Diff for: Source/CesiumRuntime/Public/CesiumCreditSystem.h

-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ class CESIUMRUNTIME_API ACesiumCreditSystem : public AActor {
7575
#endif
7676

7777
private:
78-
static UObject* CesiumCreditSystemBP;
79-
8078
/**
8179
* A tag that is assigned to Credit Systems when they are created
8280
* as the "default" Credit System for a certain world.

0 commit comments

Comments
 (0)