x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

AnswerHub Maintenance

Background maintenance is scheduled to occur between 9 - 11am EDT on Tuesday, May 21. Site operation may be slower than normal during this time and a brief interruption in operation may be observed

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?

Product Version: Not Selected
Tags:
more ▼

asked Aug 18 '14 at 07:32 PM in Blueprint Scripting

avatar image

Scienziatogm
369 44 40 51

(comments are locked)
10|2000 characters needed characters left

4 answers: sort voted first

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.

SOLUTION DATABASE:

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:

  • 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 the blueprint of your LevelDatabaseActor.

  • In the Event Graph, create all the 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 the node "All Actors of Class" and select your "LevelDatabaseActor".

  • From the array output drag a "Get" array item, and leave it to 0.

  • From the 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 the 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 the Print node.

  • Compile and run your level.

  • Enjoy some coffee and cookies. You have a new database in blueprint only.

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

more ▼

answered Jan 12 '16 at 10:06 PM

avatar image

Maha Vajra
226 3 71 34

(comments are locked)
10|2000 characters needed characters left

Hey Scienziatogm,

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.

more ▼

answered Aug 23 '14 at 08:32 PM

(comments are locked)
10|2000 characters needed characters left

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

https://wiki.unrealengine.com/Config_Files,_Read_%26_Write_to_Config_Files

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!

https://wiki.unrealengine.com/Save_System,_Read_%26_Write_Any_Data_to_Compressed_Binary_Files

:)

Rama"

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

more ▼

answered Aug 18 '14 at 07:39 PM

avatar image

Markar
3 2 5

avatar image Scienziatogm Aug 18 '14 at 09:08 PM

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

avatar image Markar Aug 19 '14 at 11:09 AM

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++.

avatar image Scienziatogm Aug 22 '14 at 10:48 PM

So I need someone that can do it in blueprints.

(comments are locked)
10|2000 characters needed characters left

Scienziatogm,

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.

more ▼

answered Feb 12 '16 at 01:07 PM

avatar image

GolgothX01
91 6 10 13

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question