How can I store variables in all levels?

Hi, I’ve done a Menu and a Character Selection System, but when I select a character, and then I open level in next menu section, variable of character (which character I selected) is set to its default value. Why?

You could try way mentationed in What is the best way to keep data between levels? - World Creation - Epic Developer Community Forums i.e.

"Config Files
For very small amount of data you can simply save it out to config file and then load it back

Binary Save and Load
I have tutorial on how to save any data you want to compressed binary file and then load it back into game!

:slight_smile:

"

EDIT:
I found that in UE 4.4 is UGameInstance class that is persistent between levels. You can try using it.

Thanks, but how can I get game instance variables and set them? I tried to add Get Game Instance > Cast to GameInstanceBlueprint on my HUD Blueprint, but Cast fails.

I’m not sure, never used it, maybe you can make a Class that inherits from UGameInstance, then add some variables with static modifier. I don’t have any idea how to do it in blueprints, I use only C++.

So I need someone that can do it in blueprints.

Hey ,

You can save variables between levels using SaveGame Blueprints. Create a new Blueprint and search for custom class SaveGame, name it something appropriate for variables you want to store there. Inside SaveGame BP, create a variable of each type that you need saved (being sure to name them something unique so your other Blueprints don’t get confusing).

You can then save to or load from SaveGame BP when you need. There are some useful answers on AnswerHub that go into more detail:

Hope that helps! Let me know if you have any more specific questions setting this up.

**Global variables database, and pass it between levels! **

For most questions on this matter, we are suggested to use Direct Blueprint Communication, and it rarely works. problem with Blueprint Communication is that it’s hard (especially for non-advanced users) to use/get right object or reference or class variable, or not miss a single detail of setup, which are so many. Even when you do set it up correctly, some variables just won’t go through, depending on blueprint context. Then you must also define in every class, widget, or blueprint, an instance of other object that contains your variables.
So just DON’T use Direct Blueprint Communication. It should be used only for its purpose, which is to allow an actor to affect another actor, such as allowing your player to turn on a light. It should not be used to handle your variables and database needs.

SOLUTION DATABASE:

From any Event Graph or Function, whether from your Level Blueprint, an actor or a widget, you can use blueprint node “All Actors of Class”, and drag out of it an array of all actors from specified class that exists in your level. Here follows a step by step setup of your local database:

  • Create a new blueprint ACTOR, because you are going to put in in your level, and it has to be an actor. Call it “LevelDatabaseActor”
  • Place your new LevelDatabaseActor in your level (I put it at origin 0,0,0).
  • Add/edit blueprint of your LevelDatabaseActor.
  • In Event Graph, create all variables you wish, and make them visible.
  • Compile and save.
  • Open any other actor, widget, or blueprint, anywhere with an event graph.
  • on any event (BeginPlay?) connect node “All Actors of Class” and select your “LevelDatabaseActor”.
  • From array output drag a “Get” array item, and leave it to 0.
  • From Get 0, you have access to set or get any variable in your LevelDatabaseActor, from anywhere.

Test your database:

  • In your LevelDatabaseActor, create a String variable called “TestVariable”, make it visible, and set default value to “This will print if I did it correctly.”.
  • In your other actor/widget/blueprint/eventgraph, Get All Actors from Class, Get array element 0, get variable “TestVariable”, connected to Print node.
  • Compile and run your level.
  • Enjoy some coffee and cookies. You have a new database in blueprint only.

Get 0 vs ForEachLoop:

ForEachLoop code uses a bit more CPU ressources than a direct call to an item in your array. By habit, most blueprint coders use a ForEachLoop after an “All Actors of Class” because there usually are many instances of that actor calss in your level. Since you are absolutely confident that there is only a single instance of your LocalDatabaseActor class in your entire level, the “All Actors of Class” is certain to return an array containing only one item, in index 0. For that reason, we are saving a very small amount of CPU by refering directly to item we want, rather than calling a ForEachLoop structure.

Use ForEachLoop only when there is a possibility to have more than one instance of a class in your level. When you are certain to have only 1, use Get arrya item 0. Every micro-cycle of your CPU saved, here and there, is a tad more visual effects your user’s computer or mobile will be able to handle.

Passing your database through levels

To keep your database alive from one level to another, learn to save and load files using SaveGame blueprint (many tutorials exist). I will hereby take from granted that you know how to use a SaveGame blueprints class.

  • Put a single instance of your LocalDatabaseActor in every level.
  • Create a SaveGame blueprint called “TransistionFileSG”, containing same variables as your LocalDatabaseActor.
  • Create 2 functions in your LocalDatabaseActor:
    - One to save every variable of you LocalDatabaseActor in a transition file (I personally call this file “transitionFile”).
    - One to load every variable from your transition file back into your LocalDatabaseActor.
    - In both functions you have to get/set each variable separately.
  • On verge of exiting a level, call function to save all your variables in your transition file.
  • On entering your new level, call function to load back all variables.

You passed all your variables from one level to another. More cookies and coffee, or whatever you use to celebrate which won’t prevent you to pursue coding your game.

Hint: In your LocalDatabaseActor and TransitionFileSG, add a few variables that will help you handle your project. Try to plan your debuging needs a head. Add string variables like DataTimeOfFile, GameName, CharacterPlayerName, CharacterSGFileName. You could add a boolean LoadMe that is verified before you load all your stats. When changing a level, set LoadMe to true before calling your save function. On game quit, save your transitionFile with LoadMe set to false. You will know in your load fuction if your transitionFile is outdated from a previous quit game, or fresh from an actual level transition.
PS: If you are using World Composition functionality, you have to put your LocalDatabaseActor in your persistent level, and only save/load transition when you are getting out of your actual persistent level. If you don’t know what is World Composition, forget that PS.

I really hope that helps you guys. If my Unreal Engine was not loading a new mesh for last 6 hours, I would do screen captures of all this. But I figured if you are using variables like that, just reading step by step made sense to you.

I’ll go get myself more coffee and cookies.

Maha,
Maha Merrymaking

,

there is different solution !

Just understand that in your case you can simply on character selection send in your “Player State” reference of your character ID you have selected then you can simply on level start check in your player state which character id is selected and load appropriate Character Blueprint.

You have also Game instance, who is only way to carry information while your game session.
Game mode is often used for Respawn your character.

Hope it will help you.
You can back to me in private for any additional help.