# trouble with UGameplayStatics::SaveGameToSlot not storing data

**URL:** <https://forums.unrealengine.com/t/trouble-with-ugameplaystatics-savegametoslot-not-storing-data/1776418>\
**Category:** C++\
**Tags:** loading, unreal-engine, saving, question, CPP\
**Created:** [March 28, 2024, 6:16pm UTC](https://forums.unrealengine.com/t/trouble-with-ugameplaystatics-savegametoslot-not-storing-data/1776418 "2024-03-28T18:16:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lazyFrond](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@lazyFrond](https://forums.unrealengine.com/u/lazyFrond)\
**Post date:** [March 28, 2024, 6:16pm UTC](https://forums.unrealengine.com/t/trouble-with-ugameplaystatics-savegametoslot-not-storing-data/1776418/1 "2024-03-28T18:16:42Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![SylarAtomic](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sylaratomic/32/1194543_2.png) [@SylarAtomic](https://forums.unrealengine.com/u/SylarAtomic)\
**Post date:** [March 28, 2024, 6:56pm UTC](https://forums.unrealengine.com/t/trouble-with-ugameplaystatics-savegametoslot-not-storing-data/1776418/2 "2024-03-28T18:56:43Z")

</div>

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.

```auto
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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![lazyFrond](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@lazyFrond](https://forums.unrealengine.com/u/lazyFrond)\
**Post date:** [March 28, 2024, 7:11pm UTC](https://forums.unrealengine.com/t/trouble-with-ugameplaystatics-savegametoslot-not-storing-data/1776418/3 "2024-03-28T19:11:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lazyFrond](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@lazyFrond](https://forums.unrealengine.com/u/lazyFrond)\
**Post date:** [March 28, 2024, 7:15pm UTC](https://forums.unrealengine.com/t/trouble-with-ugameplaystatics-savegametoslot-not-storing-data/1776418/4 "2024-03-28T19:15:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SylarAtomic](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sylaratomic/32/1194543_2.png) [@SylarAtomic](https://forums.unrealengine.com/u/SylarAtomic)\
**Post date:** [March 28, 2024, 7:27pm UTC](https://forums.unrealengine.com/t/trouble-with-ugameplaystatics-savegametoslot-not-storing-data/1776418/5 "2024-03-28T19:27:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [March 28, 2024, 7:29pm UTC](https://forums.unrealengine.com/t/trouble-with-ugameplaystatics-savegametoslot-not-storing-data/1776418/6 "2024-03-28T19:29:33Z")

</div>

header

```auto
// 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

```auto
// 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

```auto

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

```auto
		UPROPERTY(SaveGame)

```

Upon loading:  
 ![load](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/2/4/7242ee72926711c86df38e896c4e156d9c959e10.png)

---

<div class="post-metadata">

**Author:** ![lazyFrond](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@lazyFrond](https://forums.unrealengine.com/u/lazyFrond)\
**Post date:** [March 28, 2024, 7:49pm UTC](https://forums.unrealengine.com/t/trouble-with-ugameplaystatics-savegametoslot-not-storing-data/1776418/7 "2024-03-28T19:49:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [April 27, 2024, 7:49pm UTC](https://forums.unrealengine.com/t/trouble-with-ugameplaystatics-savegametoslot-not-storing-data/1776418/8 "2024-04-27T19:49:47Z")

</div>

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