A custom variable in the GameInstance is null after map change

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.


(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 :\ )

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

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

But I have an error with PCHighscore being None.

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

1 Like

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:

1 Like

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 !

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:

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.

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:

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

Good luck!

1 Like

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 ?

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
1 Like

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

1 Like

@Topbleyzer

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

1 Like