- 저장하기: SaveGame 클래스로 Instance를 만들어 Slot에 정보를 저장한다
- 불러오기: 저장한 Instance를 불러온다.
- SaveGame: 저장하고자 하는 정보를 담는 오브젝트
📄 Save Game
✏️ 헤더
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/SaveGame.h"
#include "MyShootingSaveGame.generated.h"
UCLASS()
class MYSHOOTINGCPP_API UMyShootingSaveGame : public USaveGame
{
GENERATED_BODY()
public:
UPROPERTY()
int32 save_highScore;
};
📄 Game Mode
✏️ 헤더
public:
AMyShootingCppGameModeBase();
public:
FString saveFileName;
int32 saveUserIndex;
void DoSaveGame(int32 score);
int32 DoLoadGame();
✏️ 소스
AMyShootingCppGameModeBase::AMyShootingCppGameModeBase()
{
saveFileName = TEXT("HIGH_SCORE");
saveUserIndex = 0;
}
void AMyShootingCppGameModeBase::BeginPlay()
{
Super::BeginPlay();
highScore = DoLoadGame();
scoreWidget->TextBlock_HighScore->SetText(FText::AsNumber(highScore));
}
void AMyShootingCppGameModeBase::DoSaveGame(int32 value)
{
USaveGame* temp = UGameplayStatics::CreateSaveGameObject(UMyShootingSaveGame::StaticClass());
UMyShootingSaveGame* saveInst = Cast<UMyShootingSaveGame>(temp);
saveInst->save_highScore = value;
UGameplayStatics::SaveGameToSlot(saveInst, saveFileName, saveUserIndex);
}
int32 AMyShootingCppGameModeBase::DoLoadGame()
{
if (UGameplayStatics::DoesSaveGameExist(saveFileName, saveUserIndex) == false)
{
return 0;
}
USaveGame* temp = UGameplayStatics::LoadGameFromSlot(saveFileName, saveUserIndex);
UMyShootingSaveGame* saveInst = Cast<UMyShootingSaveGame>(temp);
return saveInst->save_highScore;
}
📄 참고자료