Compare commits

..

3 Commits

Author SHA1 Message Date
baz c941024ecf Add Save functions to NakatomiGameInstance 2024-05-21 22:07:26 +01:00
baz a3120e27f3 Create NakatomiSaveGame class 2024-05-21 22:06:57 +01:00
baz e475a6a365 Create NakatomiSaveFileInfo struct 2024-05-21 22:06:47 +01:00
4 changed files with 147 additions and 4 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();
}; };

View File

@ -3,13 +3,25 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "NakatomiSaveFileInfo.generated.h"
/** /**
* *
*/ */
class NAKATOMI_API NakatomiSaveFileInfo USTRUCT(BlueprintType)
struct FNakatomiSaveFileInfo
{ {
public: GENERATED_BODY()
NakatomiSaveFileInfo();
~NakatomiSaveFileInfo(); UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Info")
FString SaveSlotName;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Info")
FString PlayerName;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Info")
FString CurrentLevel;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Info")
FString DateTimeSaved;
}; };

View File

@ -3,6 +3,7 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "Weapon.h"
#include "GameFramework/SaveGame.h" #include "GameFramework/SaveGame.h"
#include "NakatomiSaveGame.generated.h" #include "NakatomiSaveGame.generated.h"
@ -14,4 +15,25 @@ class NAKATOMI_API UNakatomiSaveGame : public USaveGame
{ {
GENERATED_BODY() GENERATED_BODY()
public:
UPROPERTY(VisibleAnywhere, Category = Basic)
FString PlayerName = "Player Name";
UPROPERTY(VisibleAnywhere, Category = Level)
FString LevelName = "Level1";
UPROPERTY(VisibleAnywhere, Category = Player)
float PlayerHealth = 100.0f;
UPROPERTY(VisibleAnywhere, Category = Player)
TArray<AWeapon*> WeaponInventory;
UPROPERTY(VisibleAnywhere, Category = Player)
int CurrentInventorySlot = 0;
UFUNCTION(BlueprintCallable)
void ResetPlayerValuesToDefault()
{
PlayerHealth = 100.0f;
}
}; };