header
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/SaveGame.h"
#include "OneSaveGame.generated.h"
/**
*
*/
UCLASS()
class YOUR_API UOneSaveGame : public USaveGame
{
GENERATED_BODY()
public:
UFUNCTION()
void SetCurrentLevel(int level);
UFUNCTION()
int GetCurrentLevel();
private:
UPROPERTY(SaveGame)
int _level;
};
cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "OneSaveGame.h"
void UOneSaveGame::SetCurrentLevel(int level)
{
_level = level;
}
int UOneSaveGame::GetCurrentLevel()
{
return _level;
}
implementation
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
UOneSaveGame* sg = Cast<UOneSaveGame>(UGameplayStatics::CreateSaveGameObject(UOneSaveGame::StaticClass()));
sg->SetCurrentLevel(25);
UGameplayStatics::SaveGameToSlot(sg, "saveGameOne", 0);
sg = nullptr;
UOneSaveGame* currentSave = (UOneSaveGame*)UGameplayStatics::LoadGameFromSlot("saveGameOne", 0);
int lvl = currentSave->GetCurrentLevel();
}
I’m guessing you didn’t mark the property to be saved
UPROPERTY(SaveGame)
Upon loading:
