Add Save functions to NakatomiGameInstance

This commit is contained in:
baz 2024-05-21 22:07:26 +01:00
parent a3120e27f3
commit c941024ecf
2 changed files with 109 additions and 0 deletions

View File

@ -3,6 +3,9 @@
#include "NakatomiGameInstance.h" #include "NakatomiGameInstance.h"
#include "PlayerCharacter.h"
#include "Kismet/GameplayStatics.h"
UNakatomiAIAttackTokenManager* UNakatomiGameInstance::GetAIAttackTokenManager() UNakatomiAIAttackTokenManager* UNakatomiGameInstance::GetAIAttackTokenManager()
{ {
return IsValid(AIAttackTokenManager) ? AIAttackTokenManager : (AIAttackTokenManager = NewObject<UNakatomiAIAttackTokenManager>(this, TEXT("AI Attack Token Manager"))); return IsValid(AIAttackTokenManager) ? AIAttackTokenManager : (AIAttackTokenManager = NewObject<UNakatomiAIAttackTokenManager>(this, TEXT("AI Attack Token Manager")));
@ -19,3 +22,89 @@ void UNakatomiGameInstance::SetCurrentLevelManager(UNakatomiLevelManager* NewLev
{ {
currentLevelManager = NewLevelManager; currentLevelManager = NewLevelManager;
} }
UNakatomiSaveGame* UNakatomiGameInstance::LoadGameFromSlot(FString SaveSlotName)
{
if (UNakatomiSaveGame* Save = Cast<UNakatomiSaveGame>(UGameplayStatics::LoadGameFromSlot(SaveSlotName, 0)))
{
SaveGameObject = Save;
return SaveGameObject;
}
return nullptr;
}
bool UNakatomiGameInstance::SaveGame(bool ResetDefaults)
{
if (SaveGameObject == nullptr)
{
UE_LOG(LogTemp, Warning, TEXT("SaveGame called without a loaded save game object."));
return false;
}
if (ResetDefaults)
{
SaveGameObject->ResetPlayerValuesToDefault();
}
else
{
APlayerCharacter* Player = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
SaveGameObject->PlayerHealth = Player->GetCurrentHealthCount();
SaveGameObject->WeaponInventory = Player->WeaponInventory;
SaveGameObject->CurrentInventorySlot = Player->GetCurrentInventorySlot();
}
SaveGameObject->LevelName = GetWorld()->GetMapName();
SaveGameObject->LevelName.RemoveFromStart(GetWorld()->StreamingLevelsPrefix);
return UGameplayStatics::SaveGameToSlot(SaveGameObject, SaveGameObject->PlayerName, 0);
}
UNakatomiSaveGame* UNakatomiGameInstance::GetSaveGameObject()
{
return SaveGameObject;
}
// TODO: This should either fail or append number if a save already exists with the same name
UNakatomiSaveGame* UNakatomiGameInstance::CreateNewSaveGame(FString PlayerName)
{
if (UNakatomiSaveGame* Save = Cast<UNakatomiSaveGame>(
UGameplayStatics::CreateSaveGameObject(UNakatomiSaveGame::StaticClass())))
{
SaveGameObject->PlayerName = PlayerName;
if (UGameplayStatics::SaveGameToSlot(Save, PlayerName, 0))
{
SaveGameObject = Save;
return SaveGameObject;
}
}
return nullptr;
}
TArray<FNakatomiSaveFileInfo> UNakatomiGameInstance::GetAllSaveFilesFromDisk()
{
TArray<FNakatomiSaveFileInfo> Files;
IFileManager& FileManager = IFileManager::Get();
FString SavedGamesFolder = FString{FPaths::ProjectSavedDir()} + "SaveGames/";
TArray<FString> FileNames;
FileManager.FindFiles(FileNames, *SavedGamesFolder, TEXT("*.sav"));
for (FString FileName : FileNames)
{
FileName.RemoveFromEnd(".sav");
if (UNakatomiSaveGame* saveGameObject = Cast<UNakatomiSaveGame>(UGameplayStatics::LoadGameFromSlot(FileName, 0)))
{
Files.Add({FileName,
saveGameObject->PlayerName,
saveGameObject->LevelName,
FileManager.GetTimeStamp(*(SavedGamesFolder + FileName + ".sav")).ToString()});
}
}
return Files;
}

View File

@ -6,6 +6,8 @@
#include "Engine/GameInstance.h" #include "Engine/GameInstance.h"
#include "NakatomiAIAttackTokenManager.h" #include "NakatomiAIAttackTokenManager.h"
#include "NakatomiLevelManager.h" #include "NakatomiLevelManager.h"
#include "NakatomiSaveFileInfo.h"
#include "NakatomiSaveGame.h"
#include "NakatomiGameInstance.generated.h" #include "NakatomiGameInstance.generated.h"
/** /**
@ -25,6 +27,9 @@ private:
UPROPERTY() UPROPERTY()
UNakatomiAIAttackTokenManager* AIAttackTokenManager; UNakatomiAIAttackTokenManager* AIAttackTokenManager;
UPROPERTY()
UNakatomiSaveGame* SaveGameObject = nullptr;
public: public:
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
@ -35,4 +40,19 @@ public:
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
void SetCurrentLevelManager(UNakatomiLevelManager* NewLevelManager); void SetCurrentLevelManager(UNakatomiLevelManager* NewLevelManager);
UFUNCTION(BlueprintCallable)
UNakatomiSaveGame* LoadGameFromSlot(FString SaveSlotName);
UFUNCTION(BlueprintCallable)
bool SaveGame(bool ResetDefaults);
UFUNCTION(BlueprintCallable)
UNakatomiSaveGame* GetSaveGameObject();
UFUNCTION(BlueprintCallable)
UNakatomiSaveGame* CreateNewSaveGame(FString PlayerName);
UFUNCTION(BlueprintCallable)
TArray<FNakatomiSaveFileInfo> GetAllSaveFilesFromDisk();
}; };