Implemented the save system base

This commit is contained in:
TjgL 2025-02-16 15:28:10 +01:00
parent 52d83193c4
commit 46247b0da4
7 changed files with 152 additions and 0 deletions

View File

@ -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

BIN
Content/Legumix/BP_GameInstance.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Legumix/BP_SaveGame.uasset (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -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<ULMSaveGame>(UGameplayStatics::LoadGameFromSlot(SaveGameSlot, UserIndex)))
{
SaveGame = UnrealSaveGame;
UE_LOG(LogTemp, Display, TEXT("Loaded game settings !"));
}
else
{
if (!SaveGame)
{
SaveGame = CastChecked<ULMSaveGame>(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."));
}
}
}

View File

@ -0,0 +1,4 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "LMSaveGame.h"

View File

@ -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<ULMSaveGame> SaveGame;
/**
* The save game slot to use for the save.
*/
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category=Legumix, meta=(AllowPrivateAccess=true))
FString SaveGameSlot;
int UserIndex = 0;
};

View File

@ -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()
};