How to load game from another level

hello, i want to know how to load my game from another level.

every time i change my level, My character attributes are reset.

for example, in this map, i reduced my health value.

then i move to another level, the value of my health is reset.

i really want to know how i can maintain my health value when i change the level

thanks a lot

If you game is single player, then you can simply put those values in the GameMode or in GameInstance.

i tried to use GameInstance to do that, but i failed.
i can use GameInstance to load game from the level in which i save my game.
but i cann’t load game from another level.

Loading a new level, doesn’t change the value of any of your variables defined in the gamemode or gameinstance, unless you do it. After you load a new level, you just pass the values of health, score or whethever to the hud.
If the values are been resetting, the the problem is with your logic.
How are you loading a new level?
Are you spawning a new player?
Where are the values of score, health and such, stored?
Who controls the HUD?

Here is a simple example using gameinstance:

Score in Gameinstance:

295630-a1.png

Hud update :

Player or someone else updating the score stored in Gameinstance:

When opening a new level (openlevel), the values in gameinstance are not changed.

Also, don’t forget to set the gameinstance in the project settings:

thanks all of you ,and i tested GameInstance from a new project, as you say, the value of my variables defined in my GameInstance doesn’t change.

(i don’t either forget to set the gameinstance in the project settings:),anyway, thanks for your recommendations )

so it is the problem with my HUD.

you can see the photo, when i try to cast to mycharacter, and it fails in my UI_MainMenu.(inside my UI_MainMenu, it has a button called “ContinueGame”)

so i think it is the real reason why i cann’t store the value of my variables.

Do you have any ideas to resolve that, or you can show your “save load game system” in here, thank you very much !

sorry, it is the problem with my UI_MainMenu :slight_smile:

With the variables in the gameinstance, there’s no more need for the player character reference, even if it’s the player who created the hud.

now i know that the values in my GameInstance will not change. and the values in my character will reset when i change the level.

so i want to find a way to put the values in my GameInstance to the values in my character when i change the level. i have tried my best to do it, but i didn’t resolve it…

Why not keeping all the values you need to keep in the GameInstance?

But if you want to transfer them from gameinstance to player, then all you need is to create a function or custom event in the gameinstance to copy those values:

Then in the character, at the begin play, just call that function:

You can also create a function to copy from player to gameinstance, and call it before the level change.

Note: there’s too much duplication. If you don’t want to use gameinstance as the tracker, then I suggest you use slots to save and load the values between levels. This way, you can discard gameinstance.

now i finally find out why i can’t cast to my character. because in my start level,the mode of game is different from that in my play level. so that’s why i cann’t cast!

now my problem is resolved, anyway ,thanks for your help, thanks for your teaching!