trouble with UGameplayStatics::SaveGameToSlot not storing data

I’m sure I’m just making some kind of mistake but I cant access any save data with my approach.
If

class USaveGameOne : public USaveGame
{
public:
	int currentLevel;
}

void AHud::BeginPlay()
{
	USaveGameOne* newSave = Cast<USaveGameOne>(UGameplayStatics::CreateSaveGameObject(USaveGameOne::StaticClass()));
	newSave->SetCurrentLevel(25);
	UGameplayStatics::SaveGameToSlot(newSave, "saveGameOne", 0);
}

void SMainMenu::Construct(const FArguments& InArgs)
{
	USaveGameOne* currentSave = (USaveGameOne*)UGameplayStatics::LoadGameFromSlot("saveGameOne", 0);
}

currentSave->currentLevel = 0.

it seems like the issue might have something do with the casts but I dont know how I could go without them.

Hi @lazyFrond,

When loading the save game, it’s more reliable to use the Cast<> function provided by Unreal Engine. Also remember to make use of the TEXT macro.

USaveGameOne* CurrentSave = Cast<USaveGameOne>UGameplayStatics::LoadGameFromSlot(TEXT("SaveGameOne"), 0));
    if (CurrentSave)
    {
               
        CurrentSave->CurrentLevel = 0;
    }

The cast in BeginPlay looks fine but its always safer to make sure it is valid before setting it.

void AHud::BeginPlay()
{
    USaveGameOne* NewSave = Cast<USaveGameOne>(UGameplayStatics::CreateSaveGameObject(USaveGameOne::StaticClass()));
    if (NewSave)
    {
        NewSave->CurrentLevel = 25; 
        UGameplayStatics::SaveGameToSlot(newSave, TEXT("SaveGameOne"), 0);
    }
}

Hope this helps, let me know if the above works for you.

1 Like

still nothing, but worth a shot, thanks. Have you ever used SaveGameToSlot? it seems like it should be straight forward

Ive got a fun Idea. maybe Intsead of creating an instance of USaveGame I should just rewrite the original class

I used USaveGame in another one of my games with a similar approach and it worked fine.

Do you have UCLASS and GENERATED_BODY() macros?

1 Like

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:
load

2 Likes

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

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.