I’m trying to store a simple string in a UsaveGame class I make:
I’m using the following setup:
.h file of MySaveGame
#pragma once
#include "GameFramework/SaveGame.h"
#include "MySaveGame.generated.h"
/**
*
*/
UCLASS()
class DEEPDOWN_API UMySaveGame : public USaveGame
{
GENERATED_BODY()
public:
UMySaveGame();
FString PlayerName;
UPROPERTY(VisibleAnywhere, Category = Basic)
FString SaveSlotName;
UPROPERTY(VisibleAnywhere, Category = Basic)
uint32 UserIndex;
};
cpp file of MySaveGame
// Fill out your copyright notice in the Description page of Project Settings.
#include "DeepDown.h"
#include "MySaveGame.h"
UMySaveGame::UMySaveGame() {
SaveSlotName = TEXT("TestSaveSlot");
UserIndex = 0;
}
Code in the BeginPlay() of an actor in my game:
FString PlayerName = TEXT("PlayerOne");
UMySaveGame* SaveGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass()));
SaveGameInstance->PlayerName = PlayerName;
UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveGameInstance->SaveSlotName, SaveGameInstance->UserIndex);
UMySaveGame* LoadGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass()));
LoadGameInstance = Cast<UMySaveGame>(UGameplayStatics::LoadGameFromSlot(LoadGameInstance->SaveSlotName, LoadGameInstance->UserIndex));
FString PlayerNameToDisplay = LoadGameInstance->PlayerName;
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("OUTPUT"));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, PlayerNameToDisplay);
}
However PlayerNameToDisplay always is empty.
The code compiles and ‘OUTPUT’ gets displayed on screen.
I’ve put hours in trying to find the solution to my problem
What am I doing wrong?