// 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
Thats it! its necessary to mark the properties within the save game class as uproperties. thanks 3dRaven this is the second time youve helped me. It is kinda strange however, I wouldnt expect the garbage collector to destroy an initialized value from within a save file, but alls well