Skip to content

Credit system BP: fix crashes when unloading then reloading UE levels #1630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions Source/CesiumRuntime/Private/CesiumCreditSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "Modules/ModuleManager.h"
#endif

/*static*/ UObject* ACesiumCreditSystem::CesiumCreditSystemBP = nullptr;
namespace {

/**
Expand Down Expand Up @@ -89,17 +88,6 @@ FName ACesiumCreditSystem::DEFAULT_CREDITSYSTEM_TAG =

/*static*/ ACesiumCreditSystem*
ACesiumCreditSystem::GetDefaultCreditSystem(const UObject* WorldContextObject) {
// Blueprint loading can only happen in a constructor, so we instantiate a
// loader object that retrieves the blueprint class in its constructor. We can
// destroy the loader immediately once it's done since it will have already
// set CesiumCreditSystemBP.
if (!CesiumCreditSystemBP) {
UCesiumCreditSystemBPLoader* bpLoader =
NewObject<UCesiumCreditSystemBPLoader>();
CesiumCreditSystemBP = bpLoader->CesiumCreditSystemBP.LoadSynchronous();
bpLoader->ConditionalBeginDestroy();
}

UWorld* world = WorldContextObject->GetWorld();
// This method can be called by actors even when opening the content browser.
if (!IsValid(world)) {
Expand Down Expand Up @@ -154,8 +142,11 @@ ACesiumCreditSystem::GetDefaultCreditSystem(const UObject* WorldContextObject) {
spawnParameters.SpawnCollisionHandlingOverride =
ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
spawnParameters.OverrideLevel = world->PersistentLevel;
pCreditSystem = world->SpawnActor<ACesiumCreditSystem>(
Cast<UClass>(CesiumCreditSystemBP),
UCesiumCreditSystemBPLoader* bpLoader =
GEngine->GetEngineSubsystem<UCesiumCreditSystemBPLoader>();
UClass* CesiumCreditSystemBP =
Cast<UClass>(bpLoader->CesiumCreditSystemBP.LoadSynchronous());
CesiumCreditSystemBP,
spawnParameters);
// Null check so the editor doesn't crash when it makes arbitrary calls to
// this function without a valid world context object.
Expand Down
4 changes: 3 additions & 1 deletion Source/CesiumRuntime/Private/CesiumCreditSystemBPLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

#pragma once

#include <Subsystems/EngineSubsystem.h>

#include "CesiumCreditSystemBPLoader.generated.h"

UCLASS()
class UCesiumCreditSystemBPLoader : public UObject {
class UCesiumCreditSystemBPLoader : public UEngineSubsystem {
GENERATED_BODY()

public:
Expand Down
2 changes: 0 additions & 2 deletions Source/CesiumRuntime/Public/CesiumCreditSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ class CESIUMRUNTIME_API ACesiumCreditSystem : public AActor {
#endif

private:
static UObject* CesiumCreditSystemBP;

/**
* A tag that is assigned to Credit Systems when they are created
* as the "default" Credit System for a certain world.
Expand Down