How create global variable?
Hi. How can I create variable whom I can edit from differents blueprints, and then using her in another blueprints?
asked Jun 26 '14 at 01:25 PM in Blueprint Scripting
Global variables database, accessible throughout your entire level!
For every question on this matter, everyone directs you to direct blueprint communication, and it almost always doesn't work. The problem with blueprint communication is that it's hard (especially for non-advanced users) to get the right object or reference or class, or not miss a single detail of the setup, and there are so many. And even when you do set it correctly, some variables just won't go through, depending on 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 it's purpose, which is to allow an actor to affect another actor, such as turning on a light. It should not be used to handle your database.
From any Event Graph or Function, you can use the blueprint node "All Actors of Class", and drag out of it an array of all actors from the specifiec class. So step by step:
Make a test:
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 live from one level to another: - Put a single instance of your LocalDatabaseActor in every level. - Learn to save and load files using the SaveGame blueprint (other tutorials). - Create a SaveGame blueprint called "TransistionFileSG", contining the same variables as your LocalDatabaseActor. - On the verge of exiting a level, save all your variables in a file called "transitionFile". - On entering your new level, load your "transitionFile", and re-associate all values from your TransitionFileSG to your LocalDatabaseActor.
You passed all your variables from one level to another. More cookies and coffee (or whatever you use to celebrate, that won't prevent you from continuing to code your game).
Hint: Create 2 functions in your LocalDatabaseActor. One to save everything in a transitionFile, and one to load everything from your transitionFile. Call your save function when you exit a level. Connect your load function to the BeginPlay event in your LocalDatabaseActor, so you are certain that you are always loading your last saved transitionFile.
Hint: In your LocalDatabaseActor and TransitionFileSG, add a few variables that will help you handle your project variable, regardless if you think you'll ever access them. Always plan your debuging needs a head. Add strings like DataTimeOfFile, GameName, CharacterPlayerName, CharacterSGFileName. You should add a boolean LoadMe that is verified before you load all your stats. When changing a level, set LoadMe to true. On game quit, save your transitionFile with LoadMe set to false. You will know in your load fuction if your transitionFile is outdated.
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 varialbes like that, just reading step by step made sense to you.
I'll go get myself more coffee and cookies.
Maha, Maha Merrymaking
Hey, there is good solution via singelton class https://wiki.unrealengine.com/Global_Data_Access,_Data_Storage_Class_Accessible_From_Any_CPP_or_BP_Class_During_Runtime
answered Aug 01 '15 at 06:45 AM
It sounds like you could use a blueprint interface. You can find more information on blueprint interfaces here: https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/Types/Interface/index.html
answered Jun 26 '14 at 08:12 PM
To do this, I create my own custom game instance blueprint class) and then cast to it to get or set the variables I need.
The image shows you how to do that. (I renamed the custom blueprint class "brukelgameinstance".)
The proposed solution with the custom actor didn't work for me as easily as I use multiple streamed levels.
answered Oct 19 '16 at 04:43 AM
You can also use an object variable to communicate between blueprints.
How To Use Blueprint Communications https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/BlueprintCommsUsage/BPComHowTo/index.html
answered Mar 10 '15 at 04:56 PM
For me is best solution GameInstance. You can set up Game Instance in Project Settings -> Maps & Modes. Game Instance is persist thru level loads, so it's truly hold your variables all the time.
answered Oct 02 '18 at 03:09 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here