UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SaveGameSystem.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "HAL/FileManager.h"
7#include "Misc/FileHelper.h"
8#include "Misc/Paths.h"
11#include "Tasks/Pipe.h"
13
20{
21public:
22
23 // Possible result codes when using DoesSaveGameExist.
24 // Not all codes are guaranteed to be returned on all platforms.
26 {
27 OK, // Operation on the file completely successfully.
28 DoesNotExist, // Operation on the file failed, because the file was not found / does not exist.
29 Corrupt, // Operation on the file failed, because the file was corrupt.
30 UnspecifiedError // Operation on the file failed due to an unspecified error.
31 };
32
34 virtual bool PlatformHasNativeUI() = 0;
35
38
40 virtual bool DoesSaveGameExist(const TCHAR* Name, const int32 UserIndex) = 0;
41
43 virtual ESaveExistsResult DoesSaveGameExistWithResult(const TCHAR* Name, const int32 UserIndex) = 0;
44
46 virtual bool GetSaveGameNames(TArray<FString>& FoundSaves, const int32 UserIndex)
47 {
48 return false;
49 }
50
52 virtual bool SaveGame(bool bAttemptToUseUI, const TCHAR* Name, const int32 UserIndex, const TArray<uint8>& Data) = 0;
53
55 virtual bool LoadGame(bool bAttemptToUseUI, const TCHAR* Name, const int32 UserIndex, TArray<uint8>& Data) = 0;
56
58 virtual bool DeleteGame(bool bAttemptToUseUI, const TCHAR* Name, const int32 UserIndex) = 0;
59
60
61 // indicates that an async savegame operation has completed. SlotName, PlatformUserId, bool=success. Should always be called from the gamethread.
63
64 // indicates that an async savegame load has completed. SlotName, PlatformUserId, bool=success, SavedData. Should always be called from the gamethread.
66
67 // indicates that an async savegame exists check has completed. SlotName, PlatformUserId, save exists result. Should always be called from the gamethread.
69
70 // indicates that an async savegame initialize has completed. PlatformUserId, bool=success. Should always be called from the gamethread.
72
73 // indicates that an async savegame list names has completed. PlatformUserId, bool=success, FoundSaves. Should always be called from the game thread
75
76
77 /* Asyncronously checks if the named savegame exists. Default implementation runs blocking version on a background thread */
78 ENGINE_API virtual void DoesSaveGameExistAsync(const TCHAR* Name, FPlatformUserId PlatformUserId, FSaveGameAsyncExistsCallback Callback);
79
80 /* Saves the named savegame asyncronously. Default implementation runs blocking version on a background thread */
81 ENGINE_API virtual void SaveGameAsync(bool bAttemptToUseUI, const TCHAR* Name, FPlatformUserId PlatformUserId, TSharedRef<const TArray<uint8>> Data, FSaveGameAsyncOpCompleteCallback Callback);
82
83 /* Load the named savegame asyncronously. Default implementation runs blocking version on a background thread */
84 ENGINE_API virtual void LoadGameAsync(bool bAttemptToUseUI, const TCHAR* Name, FPlatformUserId PlatformUserId, FSaveGameAsyncLoadCompleteCallback Callback);
85
86 /* Combines DoesSaveGameExistAsync and LoadGameAsync into one operation for simpler usage, treats all errors as the save game not existing */
88
89 /* Delete the named savegame asyncronously. Default implementation runs blocking version on a background thread */
90 ENGINE_API virtual void DeleteGameAsync(bool bAttemptToUseUI, const TCHAR* Name, FPlatformUserId PlatformUserId, FSaveGameAsyncOpCompleteCallback Callback);
91
92 /* Gets a list of all known saves if the platform supports it. Default implementatino runs blocking version on a background thread */
94
95 /* (Optional) initialise the save system for the given user. Useful if the platform may display UI on first use - this can be called as part of the user login flow, for example.
96 If this is not used, any UI may be displayed on the first use of the other save api functions */
98
99protected:
100
101 // save task pipe prevents multiple async save operations happening in parallel. note that the order is not guaranteed
103
104 // helper function for calling back to the game thread when an async save operation has completed
105 ENGINE_API void OnAsyncComplete(TFunction<void()> Callback);
106};
107
108
111{
112public:
113 virtual bool PlatformHasNativeUI() override
114 {
115 return false;
116 }
117
119 {
120 return false;
121 }
122
123 virtual ESaveExistsResult DoesSaveGameExistWithResult(const TCHAR* Name, const int32 UserIndex) override
124 {
125 if (IFileManager::Get().FileSize(*GetSaveGamePath(Name)) >= 0)
126 {
128 }
130 }
131
132 virtual bool DoesSaveGameExist(const TCHAR* Name, const int32 UserIndex) override
133 {
135 }
136
137 virtual bool GetSaveGameNames(TArray<FString>& FoundSaves, const int32 UserIndex) override
138 {
139 TArray<FString> FoundFiles;
140 const FString SaveGameDirectory = FPaths::ProjectSavedDir() / TEXT("SaveGames/");
141 IFileManager::Get().FindFiles(FoundFiles, *SaveGameDirectory, TEXT("*.sav"));
142
143 for (FString& File : FoundFiles)
144 {
146 }
147
148 return true;
149 }
150
151 virtual bool SaveGame(bool bAttemptToUseUI, const TCHAR* Name, const int32 UserIndex, const TArray<uint8>& Data) override
152 {
154 }
155
156 virtual bool LoadGame(bool bAttemptToUseUI, const TCHAR* Name, const int32 UserIndex, TArray<uint8>& Data) override
157 {
159 }
160
161 virtual bool DeleteGame(bool bAttemptToUseUI, const TCHAR* Name, const int32 UserIndex) override
162 {
164 }
165
166protected:
167
169 virtual FString GetSaveGamePath(const TCHAR* Name)
170 {
171 return FString::Printf(TEXT("%sSaveGames/%s.sav"), *FPaths::ProjectSavedDir(), Name);
172 }
173};
174
175
176
179{
180public:
181
182 virtual bool DoesSaveGameExist(const TCHAR* Name, const int32 UserIndex) override
183 {
185 }
186
187 // syncronous save functions
188 ENGINE_API virtual ESaveExistsResult DoesSaveGameExistWithResult(const TCHAR* Name, const int32 UserIndex) override;
189 ENGINE_API virtual bool GetSaveGameNames(TArray<FString>& FoundSaves, const int32 UserIndex) override;
190 ENGINE_API virtual bool SaveGame(bool bAttemptToUseUI, const TCHAR* Name, const int32 UserIndex, const TArray<uint8>& Data) override;
191 ENGINE_API virtual bool LoadGame(bool bAttemptToUseUI, const TCHAR* Name, const int32 UserIndex, TArray<uint8>& Data) override;
192 ENGINE_API virtual bool DeleteGame(bool bAttemptToUseUI, const TCHAR* Name, const int32 UserIndex) override;
193
194 // asyncronous save functions
195 ENGINE_API virtual void DoesSaveGameExistAsync(const TCHAR* Name, FPlatformUserId PlatformUserId, FSaveGameAsyncExistsCallback Callback) override;
196 ENGINE_API virtual void SaveGameAsync(bool bAttemptToUseUI, const TCHAR* Name, FPlatformUserId PlatformUserId, TSharedRef<const TArray<uint8>> Data, FSaveGameAsyncOpCompleteCallback Callback) override;
197 ENGINE_API virtual void LoadGameAsync(bool bAttemptToUseUI, const TCHAR* Name, FPlatformUserId PlatformUserId, FSaveGameAsyncLoadCompleteCallback Callback) override;
198 ENGINE_API virtual void LoadGameIfExistsAsync(bool bAttemptToUseUI, const TCHAR* Name, FPlatformUserId PlatformUserId, FSaveGameAsyncLoadCompleteCallback Callback) override;
199 ENGINE_API virtual void DeleteGameAsync(bool bAttemptToUseUI, const TCHAR* Name, FPlatformUserId PlatformUserId, FSaveGameAsyncOpCompleteCallback Callback) override;
200 ENGINE_API virtual void GetSaveGameNamesAsync(FPlatformUserId PlatformUserId, FSaveGameAsyncGetNamesCallback Callback) override;
201
202protected:
203 // internal async helpers that require implementation. NB. OutResult will be null for async calls
209
210 // specializations can override this
212
213};
214
219{
220public:
221
223};
224
225
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition SaveGameSystem.h:179
virtual ENGINE_API void GetSaveGameNamesAsync(FPlatformUserId PlatformUserId, FSaveGameAsyncGetNamesCallback Callback) override
Definition SaveGameSystem.cpp:310
virtual bool DoesSaveGameExist(const TCHAR *Name, const int32 UserIndex) override
Definition SaveGameSystem.h:182
virtual ENGINE_API ESaveExistsResult DoesSaveGameExistWithResult(const TCHAR *Name, const int32 UserIndex) override
Definition SaveGameSystem.cpp:219
virtual ENGINE_API void LoadGameIfExistsAsync(bool bAttemptToUseUI, const TCHAR *Name, FPlatformUserId PlatformUserId, FSaveGameAsyncLoadCompleteCallback Callback) override
Definition SaveGameSystem.cpp:298
virtual ENGINE_API bool SaveGame(bool bAttemptToUseUI, const TCHAR *Name, const int32 UserIndex, const TArray< uint8 > &Data) override
Definition SaveGameSystem.cpp:231
virtual ENGINE_API void LoadGameAsync(bool bAttemptToUseUI, const TCHAR *Name, FPlatformUserId PlatformUserId, FSaveGameAsyncLoadCompleteCallback Callback) override
Definition SaveGameSystem.cpp:292
virtual UE::Tasks::FTask InternalDoesSaveGameExistAsync(const TCHAR *Name, FPlatformUserId PlatformUserId, FSaveGameAsyncExistsCallback Callback, TSharedPtr< ESaveExistsResult > OutResult=nullptr)=0
virtual ENGINE_API bool LoadGame(bool bAttemptToUseUI, const TCHAR *Name, const int32 UserIndex, TArray< uint8 > &Data) override
Definition SaveGameSystem.cpp:243
virtual ENGINE_API void DoesSaveGameExistAsync(const TCHAR *Name, FPlatformUserId PlatformUserId, FSaveGameAsyncExistsCallback Callback) override
Definition SaveGameSystem.cpp:282
virtual UE::Tasks::FTask InternalGetSaveGameNamesAsync(FPlatformUserId PlatformUserId, TSharedRef< TArray< FString > > FoundSaves, FSaveGameAsyncGetNamesCallback Callback, TSharedPtr< bool > OutResult=nullptr)=0
virtual ENGINE_API void WaitForAsyncTask(UE::Tasks::FTask AsyncSaveTask)
Definition SaveGameSystem.cpp:319
virtual ENGINE_API void SaveGameAsync(bool bAttemptToUseUI, const TCHAR *Name, FPlatformUserId PlatformUserId, TSharedRef< const TArray< uint8 > > Data, FSaveGameAsyncOpCompleteCallback Callback) override
Definition SaveGameSystem.cpp:287
virtual UE::Tasks::FTask InternalSaveGameAsync(bool bAttemptToUseUI, const TCHAR *Name, FPlatformUserId PlatformUserId, TSharedRef< const TArray< uint8 > > Data, FSaveGameAsyncOpCompleteCallback Callback, TSharedPtr< bool > OutResult=nullptr)=0
virtual ENGINE_API void DeleteGameAsync(bool bAttemptToUseUI, const TCHAR *Name, FPlatformUserId PlatformUserId, FSaveGameAsyncOpCompleteCallback Callback) override
Definition SaveGameSystem.cpp:305
virtual UE::Tasks::FTask InternalDeleteGameAsync(bool bAttemptToUseUI, const TCHAR *Name, FPlatformUserId PlatformUserId, FSaveGameAsyncOpCompleteCallback Callback, TSharedPtr< bool > OutResult=nullptr)=0
virtual UE::Tasks::FTask InternalLoadGameAsync(bool bAttemptToUseUI, const TCHAR *Name, FPlatformUserId PlatformUserId, TSharedRef< TArray< uint8 > > Data, FSaveGameAsyncLoadCompleteCallback Callback, TSharedPtr< bool > OutResult=nullptr)=0
virtual ENGINE_API bool GetSaveGameNames(TArray< FString > &FoundSaves, const int32 UserIndex) override
Definition SaveGameSystem.cpp:267
virtual ENGINE_API bool DeleteGame(bool bAttemptToUseUI, const TCHAR *Name, const int32 UserIndex) override
Definition SaveGameSystem.cpp:256
Definition SaveGameSystem.h:111
virtual bool SaveGame(bool bAttemptToUseUI, const TCHAR *Name, const int32 UserIndex, const TArray< uint8 > &Data) override
Definition SaveGameSystem.h:151
virtual ESaveExistsResult DoesSaveGameExistWithResult(const TCHAR *Name, const int32 UserIndex) override
Definition SaveGameSystem.h:123
virtual bool GetSaveGameNames(TArray< FString > &FoundSaves, const int32 UserIndex) override
Definition SaveGameSystem.h:137
virtual bool DoesSaveSystemSupportMultipleUsers() override
Definition SaveGameSystem.h:118
virtual bool DoesSaveGameExist(const TCHAR *Name, const int32 UserIndex) override
Definition SaveGameSystem.h:132
virtual bool DeleteGame(bool bAttemptToUseUI, const TCHAR *Name, const int32 UserIndex) override
Definition SaveGameSystem.h:161
virtual bool PlatformHasNativeUI() override
Definition SaveGameSystem.h:113
virtual FString GetSaveGamePath(const TCHAR *Name)
Definition SaveGameSystem.h:169
virtual bool LoadGame(bool bAttemptToUseUI, const TCHAR *Name, const int32 UserIndex, TArray< uint8 > &Data) override
Definition SaveGameSystem.h:156
static CORE_API const FString & ProjectSavedDir()
Definition Paths.cpp:496
static CORE_API FString GetBaseFilename(const FString &InPath, bool bRemovePath=true)
Definition Paths.cpp:1033
static CORE_API IFileManager & Get()
Definition FileManagerGeneric.cpp:1072
virtual void FindFiles(TArray< FString > &FileNames, const TCHAR *Filename, bool Files, bool Directories)=0
virtual bool Delete(const TCHAR *Filename, bool RequireExists=0, bool EvenReadOnly=0, bool Quiet=0)=0
Definition ModuleInterface.h:14
Definition SaveGameSystem.h:219
virtual ISaveGameSystem * GetSaveGameSystem()=0
Definition SaveGameSystem.h:20
TFunction< void(FPlatformUserId, bool, const TArray< FString > &)> FSaveGameAsyncGetNamesCallback
Definition SaveGameSystem.h:74
virtual bool DoesSaveSystemSupportMultipleUsers()=0
TFunction< void(FPlatformUserId, bool)> FSaveGameAsyncInitCompleteCallback
Definition SaveGameSystem.h:71
virtual ENGINE_API void SaveGameAsync(bool bAttemptToUseUI, const TCHAR *Name, FPlatformUserId PlatformUserId, TSharedRef< const TArray< uint8 > > Data, FSaveGameAsyncOpCompleteCallback Callback)
Definition SaveGameSystem.cpp:40
ENGINE_API void OnAsyncComplete(TFunction< void()> Callback)
Definition SaveGameSystem.cpp:200
virtual ENGINE_API void InitAsync(bool bAttemptToUseUI, FPlatformUserId PlatformUserId, FSaveGameAsyncInitCompleteCallback Callback)
Definition SaveGameSystem.cpp:185
virtual ENGINE_API void LoadGameIfExistsAsync(bool bAttemptToUseUI, const TCHAR *Name, FPlatformUserId PlatformUserId, FSaveGameAsyncLoadCompleteCallback Callback)
Definition SaveGameSystem.cpp:97
virtual ENGINE_API void DoesSaveGameExistAsync(const TCHAR *Name, FPlatformUserId PlatformUserId, FSaveGameAsyncExistsCallback Callback)
Definition SaveGameSystem.cpp:13
virtual ENGINE_API void GetSaveGameNamesAsync(FPlatformUserId PlatformUserId, FSaveGameAsyncGetNamesCallback Callback)
Definition SaveGameSystem.cpp:158
static ENGINE_API UE::Tasks::FPipe AsyncTaskPipe
Definition SaveGameSystem.h:9
virtual bool GetSaveGameNames(TArray< FString > &FoundSaves, const int32 UserIndex)
Definition SaveGameSystem.h:46
TFunction< void(const FString &, FPlatformUserId, bool, const TArray< uint8 > &)> FSaveGameAsyncLoadCompleteCallback
Definition SaveGameSystem.h:65
virtual ESaveExistsResult DoesSaveGameExistWithResult(const TCHAR *Name, const int32 UserIndex)=0
virtual bool LoadGame(bool bAttemptToUseUI, const TCHAR *Name, const int32 UserIndex, TArray< uint8 > &Data)=0
virtual ENGINE_API void LoadGameAsync(bool bAttemptToUseUI, const TCHAR *Name, FPlatformUserId PlatformUserId, FSaveGameAsyncLoadCompleteCallback Callback)
Definition SaveGameSystem.cpp:70
virtual bool DeleteGame(bool bAttemptToUseUI, const TCHAR *Name, const int32 UserIndex)=0
ESaveExistsResult
Definition SaveGameSystem.h:26
virtual bool SaveGame(bool bAttemptToUseUI, const TCHAR *Name, const int32 UserIndex, const TArray< uint8 > &Data)=0
TFunction< void(const FString &, FPlatformUserId, ISaveGameSystem::ESaveExistsResult)> FSaveGameAsyncExistsCallback
Definition SaveGameSystem.h:68
TFunction< void(const FString &, FPlatformUserId, bool)> FSaveGameAsyncOpCompleteCallback
Definition SaveGameSystem.h:62
virtual bool PlatformHasNativeUI()=0
virtual bool DoesSaveGameExist(const TCHAR *Name, const int32 UserIndex)=0
virtual ENGINE_API void DeleteGameAsync(bool bAttemptToUseUI, const TCHAR *Name, FPlatformUserId PlatformUserId, FSaveGameAsyncOpCompleteCallback Callback)
Definition SaveGameSystem.cpp:132
Definition Array.h:670
Definition AndroidPlatformMisc.h:14
Definition SharedPointer.h:692
Definition SharedPointer.h:153
Definition Pipe.h:29
static CORE_API bool LoadFileToArray(TArray< uint8 > &Result, const TCHAR *Filename, uint32 Flags=0)
Definition FileHelper.cpp:39
static CORE_API bool SaveArrayToFile(TArrayView64< const uint8 > Array, const TCHAR *Filename, IFileManager *FileManager=&IFileManager::Get(), uint32 WriteFlags=0)
Definition FileHelper.cpp:632
Definition CoreMiscDefines.h:470