Writing files to Player Data Storage
Before you can retrieve per-user data from Player Data Storage, you need to store data first.
Writing a file to Player Data Storage
- C++
- Blueprints
To upload data to Player Data Storage, first get the user cloud interface:
#include "OnlineSubsystem.h"
#include "OnlineSubsystemUtils.h"
// ...
IOnlineSubsystem* Subsystem = Online::GetSubsystem(this->GetWorld());
IOnlineUserCloudPtr UserCloud = Subsystem->GetUserCloudInterface();
Register the event handler so you know when data writing has completed. WriteUserDataDelegateHandle
is declared as an FDelegateHandle
.
this->WriteUserDataDelegateHandle =
UserCloud->AddOnWriteUserFileCompleteDelegate_Handle(FOnWriteUserFileComplete::FDelegate::CreateUObject(
this,
&UMyClass::HandleWriteUserFileComplete));
Then call WriteUserFile
with the content that you want to write:
TArray<uint8> FileData;
// If you wanted to write a Save Game to Player Data Storage, you could
// populate FileData from a USaveGame* instance:
UGameplayStatics::SaveGameToMemory(SaveGame, FileData);
UserCloud->WriteUserFile(
*LocalUserId,
FileName,
FileData);
When the callback fires, you'll want to handle any errors (check bWasSuccessful
), and then deregister the event handler:
void UMyClass::HandleWriteUserFileComplete(
bool bWasSuccessful,
FUniqueNetId UserId,
const FString &FileName)
{
// Deregister the event handler.
IOnlineSubsystem *Subsystem = Online::GetSubsystem(this->GetWorld());
IOnlineUserCloudPtr UserCloud = Subsystem->GetUserCloudInterface();
UserCloud->ClearOnWriteUserFileCompleteDelegate_Handle(this->WriteUserDataDelegateHandle);
this->WriteUserDataDelegateHandle.Reset();
if (!bWasSuccessful)
{
// Data write didn't succeed, return error.
}
else
{
// Successfully wrote data.
}
}