Skip to content

Commit 16baaee

Browse files
committed
Fixed bugs related to cross level loading
1 parent c61419f commit 16baaee

7 files changed

Lines changed: 52 additions & 13 deletions

File tree

Source/SaveExtension/Private/FileAdapter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ void FFileAdapter::DeserializeObject(UObject*& Object, FStringView ClassName, co
314314
Outer = GetTransientPackage();
315315
}
316316

317-
check(IsInGameThread());
318317
Object = NewObject<UObject>(const_cast<UObject*>(Outer), ObjectClass);
319318
}
320319
// Can only reuse object if class matches

Source/SaveExtension/Private/SaveManager.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,10 @@ void USaveManager::TryInstantiateInfo(bool bForced)
283283
void USaveManager::UpdateLevelStreamings()
284284
{
285285
UWorld* World = GetWorld();
286-
check(World);
286+
if(!World)
287+
{
288+
return;
289+
}
287290

288291
const TArray<ULevelStreaming*>& Levels = World->GetStreamingLevels();
289292

Source/SaveExtension/Private/Serialization/SlotDataTask_Loader.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <Components/PrimitiveComponent.h>
99
#include <UObject/UObjectGlobals.h>
1010

11+
#include "Misc/SlotHelpers.h"
1112
#include "SavePreset.h"
1213
#include "SaveManager.h"
1314
#include "Serialization/SEArchive.h"
@@ -62,7 +63,7 @@ void USlotDataTask_Loader::OnStart()
6263

6364
// Cross-Level loading
6465
// TODO: Handle empty Map as empty world
65-
FName CurrentMapName { World->GetMapName() };
66+
FName CurrentMapName { FSlotHelpers::GetWorldName(World) };
6667
if (CurrentMapName != NewSlotInfo->Map)
6768
{
6869
LoadState = ELoadDataTaskState::LoadingMap;
@@ -139,7 +140,12 @@ void USlotDataTask_Loader::OnMapLoaded()
139140
}
140141

141142
const UWorld* World = GetWorld();
142-
if (World->GetFName() == NewSlotInfo->Map)
143+
if(!World)
144+
{
145+
UE_LOG(LogSaveExtension, Warning, TEXT("Failed loading map from saved slot."));
146+
Finish(false);
147+
}
148+
else if (World->GetFName() == NewSlotInfo->Map)
143149
{
144150
if(IsDataLoaded())
145151
{

Source/SaveExtension/Private/Serialization/SlotDataTask_Saver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <GameFramework/GameModeBase.h>
66
#include <Serialization/MemoryWriter.h>
77

8+
#include "Misc/SlotHelpers.h"
89
#include "SaveManager.h"
910
#include "SlotInfo.h"
1011
#include "SlotData.h"
@@ -92,8 +93,7 @@ void USlotDataTask_Saver::OnStart()
9293
}
9394

9495
//Save Level info in both files
95-
// TODO: Only if map is an asset, otherway it must be empty
96-
SlotInfo->Map = FName{ World->GetMapName() };
96+
SlotInfo->Map = FName{ FSlotHelpers::GetWorldName(World) };
9797
SlotData->Map = SlotData->Map;
9898

9999
SerializeSync();

Source/SaveExtension/Public/Misc/SlotHelpers.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
#pragma once
44

5-
#include "CoreMinimal.h"
5+
#include <CoreMinimal.h>
66
#include <HAL/PlatformFile.h>
7-
#include "FileAdapter.h"
87

8+
#include "FileAdapter.h"
99

10-
struct FSlotHelpers {
1110

11+
struct FSlotHelpers
12+
{
1213
static void FindSlotFileNames(TArray<FString>& FoundSlots);
1314

1415
/** Used to find next available slot id */
@@ -23,4 +24,15 @@ struct FSlotHelpers {
2324

2425
virtual bool Visit(const TCHAR* FilenameOrDirectory, bool bIsDirectory) override;
2526
};
27+
28+
static FString GetWorldName(const UWorld* World)
29+
{
30+
check(World);
31+
const FString MapName = World->GetOutermost()->GetName();
32+
if (World->IsPlayInEditor())
33+
{
34+
return UWorld::RemovePIEPrefix(MapName);
35+
}
36+
return MapName;
37+
}
2638
};

Source/SaveExtension/Public/SaveManager.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,19 @@ class SAVEEXTENSION_API USaveManager : public UGameInstanceSubsystem, public FTi
195195

196196
/** Load game from a slot name */
197197
UFUNCTION(BlueprintCallable, Category = "SaveExtension|Loading",
198-
meta = (DisplayName = "Load Slot from Id", Latent, LatentInfo = "LatentInfo",
198+
meta = (DisplayName = "Load Slot", Latent, LatentInfo = "LatentInfo",
199199
ExpandEnumAsExecs = "Result", UnsafeDuringActorConstruction))
200200
void BPLoadSlot(FName SlotName, ELoadGameResult& Result, FLatentActionInfo LatentInfo);
201201

202202
/** Load game from a slot Id */
203203
UFUNCTION(BlueprintCallable, Category = "SaveExtension|Loading",
204-
meta = (DisplayName = "Load Slot from Id", Latent, LatentInfo = "LatentInfo",
204+
meta = (DisplayName = "Load Slot by Id", Latent, LatentInfo = "LatentInfo",
205205
ExpandEnumAsExecs = "Result", UnsafeDuringActorConstruction))
206206
void BPLoadSlotById(int32 SlotId, ELoadGameResult& Result, FLatentActionInfo LatentInfo);
207207

208208
/** Load game from a SlotInfo */
209209
UFUNCTION(BlueprintCallable, Category = "SaveExtension|Loading",
210-
meta = (DisplayName = "Load Slot", Latent, LatentInfo = "LatentInfo", ExpandEnumAsExecs = "Result",
210+
meta = (DisplayName = "Load Slot by Info", Latent, LatentInfo = "LatentInfo", ExpandEnumAsExecs = "Result",
211211
UnsafeDuringActorConstruction))
212212
void BPLoadSlotByInfo(const USlotInfo* SlotInfo, ELoadGameResult& Result, FLatentActionInfo LatentInfo);
213213

@@ -443,6 +443,25 @@ class SAVEEXTENSION_API USaveManager : public UGameInstanceSubsystem, public FTi
443443
public:
444444
/** Get the global save manager */
445445
static USaveManager* Get(const UObject* ContextObject);
446+
447+
448+
/***********************************************************************/
449+
/* DEPRECATED */
450+
/***********************************************************************/
451+
452+
UFUNCTION(Category = "SaveExtension|Saving", BlueprintCallable, meta = (DeprecatedFunction, DeprecationMessage="Use 'Save Slot by Id' instead.",
453+
AdvancedDisplay = "bScreenshot, Size", DisplayName = "Save Slot to Id", Latent, LatentInfo = "LatentInfo", ExpandEnumAsExecs = "Result", UnsafeDuringActorConstruction))
454+
void BPSaveSlotToId(int32 SlotId, bool bScreenshot, const FScreenshotSize Size, ESaveGameResult& Result, FLatentActionInfo LatentInfo, bool bOverrideIfNeeded = true)
455+
{
456+
BPSaveSlotById(SlotId, bScreenshot, Size, Result, LatentInfo, bOverrideIfNeeded);
457+
}
458+
459+
UFUNCTION(BlueprintCallable, Category = "SaveExtension|Loading", meta = (DeprecatedFunction, DeprecationMessage="Use 'Load Slot by Id' instead.",
460+
DisplayName = "Load Slot from Id", Latent, LatentInfo = "LatentInfo", ExpandEnumAsExecs = "Result", UnsafeDuringActorConstruction))
461+
void BPLoadSlotFromId(int32 SlotId, ELoadGameResult& Result, FLatentActionInfo LatentInfo)
462+
{
463+
BPLoadSlotById(SlotId, Result, LatentInfo);
464+
}
446465
};
447466

448467

Source/SaveExtension/Public/SavePreset.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class SAVEEXTENSION_API USavePreset : public UObject
155155

156156
USavePreset();
157157

158-
UFUNCTION(BlueprintNativeEvent, Category = "Slots", meta = (DisplayName="Generate Slot Name"))
158+
UFUNCTION(BlueprintNativeEvent, Category = "Slots", meta = (DisplayName="Get Slot Name from Id"))
159159
void BPGetSlotNameFromId(int32 Id, FName& Name) const;
160160

161161
protected:

0 commit comments

Comments
 (0)