Game Instance not loading variables

My Game Instance is proving to be my own personal hell on earth.

I am making a game similar to Crash Bandicoot. Instead of a warp room, the levels of the game are presented sequentially, so it appears as one giant world. Checkpoints are scattered throughout the game, and a special type of checkpoint marks a new level. The only real difference between a new level and a checkpoint is that levels can be selected from the load screen in case the player wants to play a level they’ve already beaten.

As the player progresses through the game, the Game State tracks what objects and enemies they’ve destroyed and collected, as well as the levels and checkpoints they’ve reached. When the player reaches a level or checkpoint, the data tracked on the Game State is set on variables in the Game Instance to preserve them in case the player dies.

However, if the player dies, the game will only load player stats from the Game Instance like health, lives, items collected, and the last checkpoint’s transform. It will not load the state of the level, which means that all of the objects and enemies the player has previously destroyed will not be removed from play. While the player will spawn at their last checkpoint, the game doesn’t remember what checkpoints they’ve reached.

The game will load the state of the level only if the variables are loaded from a Save Game Object. This is a problem because the game needs to be able to load these at checkpoints, which do not allow the player to save their game.

BP_GameInstance.uasset (560.6 KB)

1 Like

Probably the best way to track the enemies destroyed and items collected would be to have a map of arrays of actors no longer present in the scene (perhaps saving their GUID would be enough).

Then on load of the level iterate through the array and remove these actors.
The array would have to be saved in the save game.

@3dRaven A TMap is right… but the approach can be made better…

My approach uses Sha hashs and a TMap.
I recommend Blueprint Encryption Plugin. But the free MD5 one is good, too.

A hash is a unique string, that you construct of the current level name and the actor display name.
In my case, this looks like this:

You need to use this hash string as KEY of your TMap.
As value, you can use anything… if it’s just “Enemy killed”, use a bool.

Since a hash is unique, no other enemy will have the same hash…

Save your TMap into the Savegame and load it when needed.
With TMap::Find, you can get the value of the hash key.

Well if you will be going through the whole map on level load then just get the tmaps keys and iterate over them getting their values & do the appropriate actions.

Find is only needed if you want a specific entry.

This is what the system does when a game is loaded from a save game file. But I am trying to get the game to load this data from the Instance when the player dies and respawns.

Well on kill / collect you should be adding this information (best in the game instance) and use it on death respawn. Same procedure.

to the instance or to a save game file?

The system is storing this information as it happens on the instance. Then it can be saved to a save game file at the end of a level. What’s messing me up is that I can’t retrieve it from the instance on respawn. Do I need to have like a cache save game file for this?

Keep the array on the gi and load it and save it when needed. On death just use the array to reload the state in a level.

Haha yes, that’s what I’m doing. But I don’t know why it refuses to cooperate.

Step through the code on death up untill gi is supposed to delete actors and see where it fails

I tried stepping through, and the issue seems to be that the maps are not being set on the instance. Can maps not be stored on the instance?

More precisely, it’s setting the maps, but it seems to be setting the objects as blank.