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 the level in the next menu section, the variable of the character (which character I selected) is set to its default value. Why?
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. The problem with Blueprint Communication is that it's hard (especially for non-advanced users) to use/get the right object or reference or class variable, or not miss a single detail of the setup, which are so many. Even when you do set it up correctly, some variables just won't go through, depending on the blueprint context. Then you must also define in every class, widget, or blueprint, an instance of the other object that contains your variables. So just DON'T use the 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.
From any Event Graph or Function, whether from your Level Blueprint, an actor or a widget, you can use the blueprint node "All Actors of Class", and drag out of it an array of all actors from the specified class that exists in your level. Here follows a step by step setup of your local database:
Test your database:
Get 0 vs ForEachLoop:
The 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 the index 0. For that reason, we are saving a very small amount of CPU by refering directly to the item we want, rather than calling a ForEachLoop structure.
Use the 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 the 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 the 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 the 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 the verge of exiting a level, call the function to save all your variables in your transition file. - On entering your new level, call the 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 the 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 the 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
answered Jan 12 '16 at 10:06 PM
You can save variables between levels using SaveGame Blueprints. Create a new Blueprint and search for the custom class SaveGame, name it something appropriate for the variables you want to store there. Inside the 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 the 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.
answered Aug 23 '14 at 08:32 PM
You could try the way mentationed in https://answers.unrealengine.com/questions/65281/what-is-the-best-way-to-keep-data-between-levels.html 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 the game!
EDIT: I found that in UE 4.4 is UGameInstance class that is persistent between levels. You can try using it.
there is different solution !
Just understand that in your case you can simply on character selection send in your "Player State" the 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 the appropriate Character Blueprint.
You have also the Game instance, who is the 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.
answered Feb 12 '16 at 01:07 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here