From 46247b0da48114028cbd71bcbafdc75ccda9b06d Mon Sep 17 00:00:00 2001 From: TjgL Date: Sun, 16 Feb 2025 15:28:10 +0100 Subject: [PATCH] Implemented the save system base --- Config/DefaultEngine.ini | 1 + Content/Legumix/BP_GameInstance.uasset | 3 + Content/Legumix/BP_SaveGame.uasset | 3 + Source/LegumeMix/Private/LMGameInstance.cpp | 66 +++++++++++++++++++++ Source/LegumeMix/Private/LMSaveGame.cpp | 4 ++ Source/LegumeMix/Public/LMGameInstance.h | 59 ++++++++++++++++++ Source/LegumeMix/Public/LMSaveGame.h | 16 +++++ 7 files changed, 152 insertions(+) create mode 100644 Content/Legumix/BP_GameInstance.uasset create mode 100644 Content/Legumix/BP_SaveGame.uasset create mode 100644 Source/LegumeMix/Private/LMGameInstance.cpp create mode 100644 Source/LegumeMix/Private/LMSaveGame.cpp create mode 100644 Source/LegumeMix/Public/LMGameInstance.h create mode 100644 Source/LegumeMix/Public/LMSaveGame.h diff --git a/Config/DefaultEngine.ini b/Config/DefaultEngine.ini index 231627c..0d431b9 100644 --- a/Config/DefaultEngine.ini +++ b/Config/DefaultEngine.ini @@ -4,6 +4,7 @@ GameDefaultMap=/Game/Legumix/MainMenu/LVL_MainMenu.LVL_MainMenu GlobalDefaultGameMode=/Game/Legumix/BP_GameMode.BP_GameMode_C EditorStartupMap=/Game/Legumix/Levels/LVL_GYM_00.LVL_GYM_00 +GameInstanceClass=/Game/Legumix/BP_GameInstance.BP_GameInstance_C [/Script/Engine.RendererSettings] r.AllowStaticLighting=False diff --git a/Content/Legumix/BP_GameInstance.uasset b/Content/Legumix/BP_GameInstance.uasset new file mode 100644 index 0000000..f24d392 --- /dev/null +++ b/Content/Legumix/BP_GameInstance.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:048fe757890ee89f44871a2be3d4c17936fb084d3e305ba359ef99b02d5afe1e +size 19143 diff --git a/Content/Legumix/BP_SaveGame.uasset b/Content/Legumix/BP_SaveGame.uasset new file mode 100644 index 0000000..f1dfe19 --- /dev/null +++ b/Content/Legumix/BP_SaveGame.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:54cb207cbb19eea658a3aeecbe66e0b57307eb494271f9ff45bcf7a8e69f6f48 +size 5687 diff --git a/Source/LegumeMix/Private/LMGameInstance.cpp b/Source/LegumeMix/Private/LMGameInstance.cpp new file mode 100644 index 0000000..180c641 --- /dev/null +++ b/Source/LegumeMix/Private/LMGameInstance.cpp @@ -0,0 +1,66 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "LMGameInstance.h" + +#include "LMSaveGame.h" +#include "Kismet/GameplayStatics.h" + +void ULMGameInstance::SaveSettings() +{ + const bool bSuccess = UGameplayStatics::SaveGameToSlot(SaveGame, SaveGameSlot, UserIndex); + if (!bSuccess) + { + UE_LOG(LogTemp, Error, TEXT("Failed to save game settings.")); + } + else + { + UE_LOG(LogTemp, Display, TEXT("Saved game settings")); + } + SettingsSaved(SaveGameSlot, UserIndex, bSuccess); +} + +void ULMGameInstance::AsyncSaveSettings() +{ + FAsyncSaveGameToSlotDelegate SavedDelegate; + SavedDelegate.BindUObject(this, &ULMGameInstance::SettingsSaved); + + UGameplayStatics::AsyncSaveGameToSlot(SaveGame, SaveGameSlot, UserIndex, SavedDelegate); +} + +void ULMGameInstance::SettingsSaved_Implementation(const FString& SlotName, const int32 UserIndex, bool bSuccess) +{ + if (!bSuccess) + { + UE_LOG(LogTemp, Error, TEXT("Failed to save game settings %s with user %i."), *SlotName, UserIndex); + } + else + { + UE_LOG(LogTemp, Display, TEXT("Successfully saved game settings %s for user %i"), *SlotName, UserIndex); + } +} + +void ULMGameInstance::LoadSettings() +{ + if (ULMSaveGame* UnrealSaveGame = Cast(UGameplayStatics::LoadGameFromSlot(SaveGameSlot, UserIndex))) + { + SaveGame = UnrealSaveGame; + UE_LOG(LogTemp, Display, TEXT("Loaded game settings !")); + } + else + { + if (!SaveGame) + { + SaveGame = CastChecked(UGameplayStatics::CreateSaveGameObject(ULMSaveGame::StaticClass())); + } + + if (!UGameplayStatics::SaveGameToSlot(SaveGame, SaveGameSlot, UserIndex)) + { + UE_LOG(LogTemp, Error, TEXT("Failed to create settings save.")) + } + else + { + UE_LOG(LogTemp, Display, TEXT("Created game settings.")); + } + } +} diff --git a/Source/LegumeMix/Private/LMSaveGame.cpp b/Source/LegumeMix/Private/LMSaveGame.cpp new file mode 100644 index 0000000..277c541 --- /dev/null +++ b/Source/LegumeMix/Private/LMSaveGame.cpp @@ -0,0 +1,4 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "LMSaveGame.h" diff --git a/Source/LegumeMix/Public/LMGameInstance.h b/Source/LegumeMix/Public/LMGameInstance.h new file mode 100644 index 0000000..d4b2bf6 --- /dev/null +++ b/Source/LegumeMix/Public/LMGameInstance.h @@ -0,0 +1,59 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Engine/GameInstance.h" +#include "LMGameInstance.generated.h" + +class ULMSaveGame; +/** + * + */ +UCLASS() +class LEGUMEMIX_API ULMGameInstance : public UGameInstance +{ + GENERATED_BODY() + +public: + /** + * Saves the current settings. + */ + UFUNCTION(BlueprintCallable, Category=Legumix) + void SaveSettings(); + /** + * Saves the current settings asynchronously. + */ + UFUNCTION(BlueprintCallable, Category=Legumix) + void AsyncSaveSettings(); + + /** + * Callback called when a save is finalized. + * @param SlotName The slot that was saved. + * @param UserIndex The user index assigned to the slot. + * @param bSuccess Whether the save succeeded or not. + */ + UFUNCTION(BlueprintNativeEvent, Category=Legumix) + void SettingsSaved(const FString& SlotName, int32 UserIndex, bool bSuccess); + + /** + * Load the save settings. + */ + UFUNCTION(BlueprintCallable, Category=Legumix) + void LoadSettings(); + +private: + /** + * The save game to use. + */ + UPROPERTY(BlueprintReadWrite, EditAnywhere, Category=Legumix, meta=(AllowPrivateAccess=true)) + TObjectPtr SaveGame; + + /** + * The save game slot to use for the save. + */ + UPROPERTY(BlueprintReadWrite, EditAnywhere, Category=Legumix, meta=(AllowPrivateAccess=true)) + FString SaveGameSlot; + + int UserIndex = 0; +}; diff --git a/Source/LegumeMix/Public/LMSaveGame.h b/Source/LegumeMix/Public/LMSaveGame.h new file mode 100644 index 0000000..e128579 --- /dev/null +++ b/Source/LegumeMix/Public/LMSaveGame.h @@ -0,0 +1,16 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/SaveGame.h" +#include "LMSaveGame.generated.h" + +/** + * + */ +UCLASS() +class LEGUMEMIX_API ULMSaveGame : public USaveGame +{ + GENERATED_BODY() +};