# A custom variable in the GameInstance is null after map change

**URL:** <https://forums.unrealengine.com/t/a-custom-variable-in-the-gameinstance-is-null-after-map-change/1987654>\
**Category:** Blueprint\
**Tags:** unreal-engine, question\
**Created:** [August 22, 2024, 3:21pm UTC](https://forums.unrealengine.com/t/a-custom-variable-in-the-gameinstance-is-null-after-map-change/1987654 "2024-08-22T15:21:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Topbleyzer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Topbleyzer](https://forums.unrealengine.com/u/Topbleyzer)\
**Post date:** [August 22, 2024, 3:21pm UTC](https://forums.unrealengine.com/t/a-custom-variable-in-the-gameinstance-is-null-after-map-change/1987654/1 "2024-08-22T15:21:32Z")

</div>

Hello, I’m currently scripting a score system. I have a BP\_ScoreElement class that has 2 variables “Name” a String and “Score” a int.

In my game instance I have two variables “PCHighscore” and “PCLastScore” both type are BP\_ScoreElement.

When the game is over in my game map “Level\_A” I send the score to the GameInstance that stores it in the PCLastScore variable and check if the score is better than PCHighscore or not. If PCHighscore is null or if PCLastScore score is greater than PCHighscore score, PCLastScore became the new PCHighscore.

 ![Game Manager > Send Score to Game Instance (function)](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/1/3/e13fcf7930950711d4b1f0603258283296914781.png)  
_(I first try to use a local variable BP\_ScoreElement to save the score and pass it to the Game Instance, but I change it to a SpawnActor when trying to fix this issue. Didn’t fix anything :\ )_

 ![Game Instance > Save Local Score (function)](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/2/3/1237f52ce3325bc50162e8fa6d222944c5067539.png)

When I print the score of PCLastScore and PCHighscore before I change map, I have the right score when the GameOver happens  
 ![Score print before map change](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/2/0/e2001c3fa3c96d440eda31f07ccfca1b0845bf48.png)

Then I change the map to “Level\_B”, display the GameOver widget and try to set the text element with the Game instance \> PCHighscore:Score

 ![WB_GameOver > Event Construct > Update Visual New Highscore (function)](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/3/1/331a702aaf133bf0f88b95564668cd821570860c.png)

But I have an error with PCHighscore being None.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/6/c/e6c2cb1e42d0008caf2bf00bcb8f595600955f78.png)

And if I try to print the value of PCLastScore or PCHighscore score they are also None.

If you have any idea, please let me know

Thank you

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 22, 2024, 3:59pm UTC](https://forums.unrealengine.com/t/a-custom-variable-in-the-gameinstance-is-null-after-map-change/1987654/2 "2024-08-22T15:59:18Z")

</div>

The variable is not an actor, it’s just a reference, a pointer. The actor is in the memory, and the variable tells you where. When you map change, the actor gets `Destroyed`, and the variable points to the part of the memory that is now empty - hence `Accessed None` - your actor is no longer there.

Store the `Score` int in the `Game Instance`; when you map change, re-spawn the `PCHighscore` actor and set its `Score` value using the int you stored in the `Game Instance`.

* * *

On the other hand, perhaps it’s time to look into `Save Games` - you may need it sooner or later anyway:

> **[Saving and Loading Your Game in Unreal Engine | Unreal Engine 5.4...](https://dev.epicgames.com/documentation/en-us/unreal-engine/saving-and-loading-your-game-in-unreal-engine?application_version=5.4)**
>
> Overview of how to save and load your game

---

<div class="post-metadata">

**Author:** ![Topbleyzer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Topbleyzer](https://forums.unrealengine.com/u/Topbleyzer)\
**Post date:** [August 22, 2024, 4:08pm UTC](https://forums.unrealengine.com/t/a-custom-variable-in-the-gameinstance-is-null-after-map-change/1987654/3 "2024-08-22T16:08:12Z")

</div>

I see, it means that I should save some of my objects to make me able to access it anywhere when needed by loading them from files or any of saving type.

Thank you !

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 22, 2024, 4:12pm UTC](https://forums.unrealengine.com/t/a-custom-variable-in-the-gameinstance-is-null-after-map-change/1987654/4 "2024-08-22T16:12:41Z")

</div>

> [@Topbleyzer](#):
>
> I see, it means that I should save some of my objects to make me able to access it anywhere when needed by loading them from files or any of saving type.

In Blueprints, saving _whole_ dynamically spawned actors / widgets is not really a thing. You instead save their data only - class, variables, components they had, and so on. After loading the game, respawn actors using the saved data.

* * *

Alternatively:

> [@\[PLUGIN\] Savior](https://forums.unrealengine.com/t/plugin-savior/110080):
>
> UPDATE: [PDF Documentation can be found here](https://www.dropbox.com/scl/fi/fy5x4tbsqv0ngtto6e72i/Savior_PluginDocs.pdf?rlkey=5ktwd8w12j05wefxphoiudo8y&st=jg11qnpl&raw=1)! WHAT is Savior: This is a major update for the “Auto-Save Game” system I’ve been maintaining up on Unreal Marketplace for last couple years. Focused on performance and data reach, I’ve totally scrapped original code and wrote this version from scratch, implementing a fully custom serialization engine that made possible for me to achieve the goal of moving away from the built-in ‘FArchive’ system and it’s ’ \<\< ’ operator constraints. The reason…

---

<div class="post-metadata">

**Author:** ![Topbleyzer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Topbleyzer](https://forums.unrealengine.com/u/Topbleyzer)\
**Post date:** [August 22, 2024, 4:17pm UTC](https://forums.unrealengine.com/t/a-custom-variable-in-the-gameinstance-is-null-after-map-change/1987654/5 "2024-08-22T16:17:12Z")

</div>

In case I want a leaderboard, currently I have an array of BP\_ScoreElement, how can I save that ? Can’t I just save the whole array in the GameSave object ? _(I’m reading the GameSave article you linked, there’s may be the answer)_

In other languages, for only two variables Name and Score, I would have use a dict to link a Name to a Score. But in Unreal blueprint I don’t know how to proceed.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 22, 2024, 4:20pm UTC](https://forums.unrealengine.com/t/a-custom-variable-in-the-gameinstance-is-null-after-map-change/1987654/6 "2024-08-22T16:20:34Z")

</div>

> [@Topbleyzer](#):
>
> Can’t I just save the whole array in the GameSave object

Yes you can. Array of ints, vectors, strings, names, sure. However, do look into `structs` - allows you to combine various data types that describe an object, you then make arrays of those structs. And then can pack arrays of structs into arrays of structs into arrays of…

But if you try to save actor references, you’ll save pointers to memory locations where those actors used to be (if the actor were destroyed). So that’s not gonna work.

> I would have use a dict to link a Name to a Score. But in Unreal blueprint I don’t know how to proceed.

Dictionaries are called maps:

> **[Map Containers in Unreal Engine | Unreal Engine 5.4 Documentation | Epic...](https://dev.epicgames.com/documentation/en-us/unreal-engine/map-containers-in-unreal-engine)**
>
> TMaps are defined by two types, a key type and a value type, which are stored as associated pairs in the map in Unreal Engine.

And yes, that’d be perfect for saving a `Name`|`Score` list!

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/9/7/a97637ef161e2fc7ef8778e86b7af6b76b022d6a.png)

Good luck!

---

<div class="post-metadata">

**Author:** ![Topbleyzer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Topbleyzer](https://forums.unrealengine.com/u/Topbleyzer)\
**Post date:** [August 22, 2024, 4:27pm UTC](https://forums.unrealengine.com/t/a-custom-variable-in-the-gameinstance-is-null-after-map-change/1987654/7 "2024-08-22T16:27:29Z")

</div>

I’m not sure to understand the logic for this.

If I have a leaderboard array of 100 BP\_ScoreElement and want to save it. It is by definition, an array of actor (and so only the reference to these actors are put in the array).

1. How should I save the full array ?

- Maybe save the name in a String array and the score in an int array ?

1. How could I recover my leaderboard later when I have save two arrays of Strings and ints ? Should I spawn 100 actors to the scene to store the data ? (it seems really wrong when I’m writting this)

What is to you the best solution for this ?

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 22, 2024, 4:28pm UTC](https://forums.unrealengine.com/t/a-custom-variable-in-the-gameinstance-is-null-after-map-change/1987654/8 "2024-08-22T16:28:24Z")

</div>

> [@Topbleyzer](#):
>
> What is to you the best solution for this ?

A map, as above.

- create a Map variable in the Save Game object
- populate it with the data pulled from the game
- save the Save Game object to disk

---

<div class="post-metadata">

**Author:** ![Topbleyzer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Topbleyzer](https://forums.unrealengine.com/u/Topbleyzer)\
**Post date:** [August 22, 2024, 4:28pm UTC](https://forums.unrealengine.com/t/a-custom-variable-in-the-gameinstance-is-null-after-map-change/1987654/9 "2024-08-22T16:28:53Z")

</div>

Sorry didn’t see your previous message while I was wrinting -\_-

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 22, 2024, 4:34pm UTC](https://forums.unrealengine.com/t/a-custom-variable-in-the-gameinstance-is-null-after-map-change/1987654/10 "2024-08-22T16:34:35Z")

</div>

@Topbleyzer

Here’s the Blueprints doc, I sent the C++ one by mistake:

> **[Unreal Engine Programming and Scripting | Unreal Engine 5.4 Documentation |...](https://dev.epicgames.com/documentation/en-us/unreal-engine/working-with-maps?application_version=4.27)**
>
> How to use the programming and scripting languages and tools for controlling Unreal engine programmatically at runtime.

---

<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:** [September 21, 2024, 4:34pm UTC](https://forums.unrealengine.com/t/a-custom-variable-in-the-gameinstance-is-null-after-map-change/1987654/11 "2024-09-21T16:34:47Z")

</div>

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