How To Get A Variable Value Between Levels That Have Different Game Modes?

So I have different levels in my game and I want to show player’s success rate when they hover over the level icon, in the main menu. If I was using the same Game Mode for main menu and the level, I could just get the variable of success rate, but they are using different game modes.

How can I use a variable globally?

You can use a save game, or the game instance, or a persistent level ( if you’re level streaming ).

1 Like

So, I created a score variable in widget class of the level. Then I added an event dispatcher at the end of the level, before it goes back to main menu. Then I created a widget reference in game mode of the level. After that I got a reference of the game mode in game instance. In game instance, I called the reference of game mode, and from that reference of the widget. I created a custom event that would update the score, if it is higher than previous level score, and binded it to event dispatcher in widget blueprint.

Then I created a timer by event to give me the level score every ten seconds. And it doesn’t work. It is stuck at 0. My in-game score variable in widget blueprint updates correctly, but I can not assign it’s value to the best score variable in game instance.

Sorry if I couldn’t explain myself more clearly. I am a beginner and I am not quite sure on what I am doing.

Don’t worry, I think you’re suffering from ‘youtube-itis’, you will often come back from there with all sorts of weird ways of doing things :slight_smile:

If you just want to get a variable from level A to level B, game instance is by far the easiest way. No dispatchers or anything fancy.

Game instance

Before changing levels

and after

image

It doesn’t matter where you make the widget, you can bind it to the game instance ( or read the variable out, and make the widget in the player ).

1 Like

Ops, I came a bit late. I managed to solve the problem with exactly what you said. I tried to create the variable in level, and move that variable between levels but simply creating the variable in game instance solved the problem.

Still not sure why event dispatcher didn’t work though :frowning: I made everything the right way

It doesn’t matter if you use a dispatcher or a rocket, you can’t get variable from one level to another without something like the game instance :slight_smile:

1 Like