Saving level state between levels?

Evening all.

I’m working on a game where the player needs to collect keys to progress to the next level. After each level, you return back to a sort of hub level, where you can enter portals and replay previous levels.

Currently, I have it set up so that collecting a key increases that key count by one. The character stores the number of keys you have collected for each level.

Level one requires one level one key, level two, requires two level two keys, and so on.

I know I can store the number of each collected key so that it will persist between levels. But upon returning to a level where I have already collected a particular key, how can I make it so that that key is no longer present. So it remembers it has already been collected?

I might have answered my own question in my head. But could I store it as a boolean in the game state/player state or something? If key collected, don’t destroy it as soon as possible when loading a level?

Any help is much appreciated.

Cheers all! And Happy New Year!

Well let me see if I understand:

You have a certain number of keys depending of the level number… right?
And you want to “hide” the key in the level if it´s already collected?.. I’m right?

You could store an array of “Collected key levels”; in that array you cou save the levels’ name that you succesfully “complete” (collected all keys)…
For example:
The player successfully collected the level one, two, three keys so then the game saves the level name into the array

Then after you save the levels’ name into the array you could check by using begin play/contruction script casting to you game state/player and get the array then check if the array contains the name of the actual level; if its true then don’t spawn/hide them keys if false spawn/show them keys…

Do you undertood?:smiley:
Ask me if not… :wink: Sorry for my grammar and/or spelling XD

You would work with SaveGames here. If you don’t know how they work you might want to do a quick google search or youtube search as explaining them here again is kinda time wasting.
The SaveGame could have an Array of Integers where each entry represents a Key the Player has collected.

The Keys in your level would have unique numbers. So you give your Key Blueprint an Integer variable (make it Editable!), place your Keys in the level and set the number in the details panel.
So your three Keys have the numberes 1, 2 and 3. Now if you collect a Key, you check if a SaveGame already exists and if yes, you load it, add the Key’s number to the SaveGame’s array and save it.
If no, you create a new SaveGame Object and add the Key’s number to this ones array and save it. The SlotName could be the LevelName. You can use “GetCurrentLevelName” (or something like that) to retrieve the name of the current level.

In the BeginPlay of your Keys, you would check if the SaveGame exists. If yes you load it and check if the array contains the Number of the Key and if this is true you simply destroy the key.

To sum this up:

  • Everything happens in the Key Blueprint
  • If Player collects Key, Key checks if the SaveGame exists and will add its Number to SaveGame’s Array
  • BeginPlay of the Key, you will check if SaveGame exist, load SaveGame and check if Key’s Number exists in it’s Array
  • If yes, you destroy the Key Actor
  • If no, you do nothing

Thanks for the reply s both of you. Really struggling to get my head around the saving n what not.

I managed to get the health of the player to persist between levels by using a GameInstance with a health variable. Then cast to that.
Like so

But I honestly don’t know how to get the SaveGame blueprint to save more than one piece of information at a time… I’m probably just being slow.

I managed to make this
So I could create a save when I hit a checkpoint. It doesn’t spawn the player at that location on next play however.
And I can only figure out how to get it to save one piece of information.
I need it to save, the players location, current health, unique keys collected, and what level portals have been activated.
I can’t find any information online that explains how I can go about doing that.

So, I think its best I show how I have implemented the keys so far.

I have one base class for all the keys. Then for level one, two, three, four, five and so on, I created a child blueprint. All the child blueprint does is swap out the mesh/material if needed, and assign a different number to the “Key Number” variable.
Assigning a different number to the keys allows the player to collect however many needed keys for a particular level portal. For level three for example, you need three, level three keys to unlock it.

So. Base Key class

You see the branch checks what number is assigned to the “Key Number” int var, then increments that in the players variables. As seen here.
This works to open the portals. But I doubt its the best way of doing this as I just made it up from the top of my head.
Its probably way too simplistic.

The problem I have is that I can’t figure out a way for the unique keys collected to be saved between levels.

I think its worth noting that the order of the key collection is in not important. For example, you might find a level 4 key on level 1, 2 or 3. So it needs to save which physical key was collected, not how many. So that the player isn’t able to just re-enter the level and collect the same key over and over.

If anyone has any ideas how best to go about doing this, any help would be much appreciated.