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"

How create global variable?

Hi. How can I create variable whom I can edit from differents blueprints, and then using her in another blueprints?

Product Version: Not Selected
Tags:
more ▼

asked Jun 26 '14 at 01:25 PM in Blueprint Scripting

avatar image

DcDenton
51 3 8 8

avatar image Rave185 Dec 19 '18 at 07:17 AM

Man, if only any of these solutions worked in the Robo Recall Mod kit. I'm streaming levels in to create a random dungeon for the player to explore but I wanted to take the enemies in each streamed level and count them so the level ends once you defeat all the enemies. Nope! Not possible. ugh.

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

7 answers: sort voted first

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.

SOLUTION 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:

  • 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 LDA

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

Make a test:

  • 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 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

more ▼

answered Jan 12 '16 at 08:40 PM

avatar image

Maha Vajra
206 3 71 34

avatar image Ertosi Feb 28 '16 at 09:12 PM

Maha Vajra,

Your answer looks like exactly what I'm looking for to solve my current project but you've lost me at the last step: "•From the Get 0, you have access to set or get any variable in your LevelDatabaseActor, from anywhere."

How? I have all of my variables set from the database actor with their eyes open, I used the Get All Actors of Class of the correct database class in my other blueprint, I Got array 0 but when I pull the node off, I don't see any of the variables from the Database actor. Should they be over in the left-hand window with my other local variables? That's the only place I know of where I can Get and Set variables (sorry, ultra-noob here).

avatar image Ertosi Feb 28 '16 at 10:16 PM

Huzzah! I found the step which was missing... The last step listed above was "•From the Get 0, you have access to set or get any variable in your LevelDatabaseActor, from anywhere." No, no, we're not done yet... you pull that pin and you get nada, nothing, zilch. This may be assumed information for people used to UE but for us noobs every step needs to be spelled out.

There needs to be one last step after that: From the Get 0 node pull the right pin over and starting typing "Cast to X" where X is whatever your class is named from the earlier steps - in the example above its LocalDatabaseActor. Connect the Exec arrows from the last node to your new Cast node. Now you can access all of your variables from your LevelDatabaseActor by pulling the "As X" pin on the bottom right of your Cast node. Just pull the pin and start typing "Get Y" where X is your database class and Y is the name of your sought after variable.

Score 1 for the new guy!

And thank you Maja for the great post above as I've been looking for this since starting Unreal modding a few weeks ago (stupid Unreal not using global variables, dumbest thing I've ever seen) and your post got me going in the right direction!

avatar image Maha Vajra Feb 28 '16 at 11:54 PM

Sometimes you have to Cast To, sometimes you don't. If the variable is configured IN your LocalDatabaseActor, then there is no cast required. Unless you are calling it from somewhere else than I did, which does require the Casting. But hey! Thanks for sharing this addition to the method.

avatar image CosmicDreamStudios Dec 27 '16 at 10:47 PM

This is a great solution. You could also use game mode and just access that for all your global variable needs.

avatar image VictorCoo Jan 18 '17 at 03:37 PM

Really thank you, I always wonder which blueprint should I store those global variable. This detail answer really help us a lot

avatar image navas87 Mar 15 '17 at 05:07 AM

THIS IS GREAT!! thanks for the simplest to explain and implement solution, I think LDA´s should become a very common architecture, you solved a big problem thanks thanks thanks!

avatar image NeoCJay Aug 26 '17 at 01:24 AM

I'm using GameState for a similar solution. Never thought in accessing within other BPs nice solution for dealing with global vars. Congrats.

avatar image ilias48rus Mar 14 '18 at 01:52 PM

There is enough actors which always is, differently replicate and persist. GetGameInstance , getGameMode , getGameState , getPlayerController .., the commands giving you references to them, and program do not need to load additional actor and select item from array for no reason

cast is way of 'transforming' instance reference to sub|parent class instance reference (on pins' tooltips you have "|1| object reference", with casting to target class you can edit the |1|), you can get variables with "get" node from instance of class(blueprint) ,in which the variable was defined

avatar image JordanService Aug 16 '18 at 06:58 PM

I LOVE YOU!

avatar image JJRuizGames Dec 11 '18 at 11:38 PM

It works like a charm! Thanks buddy

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

answered Aug 01 '15 at 06:45 AM

avatar image

vctr
26 1 5

avatar image NeoCJay Aug 26 '17 at 01:56 AM

This was nice one. I tried and gave me some crashes, maybe its a feature that needs to be invested a little bit more. (version: 4.16.3)

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

Hi DcDenton,

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

more ▼

answered Jun 26 '14 at 08:12 PM

avatar image DcDenton Jun 26 '14 at 09:16 PM

Me need create dynamic variable for use in another blueprints?

avatar image Adam Davis STAFF Jun 26 '14 at 09:26 PM

Hi DcDenton,

That should be done using interfaces. This should allow you to access variables across multiple blueprints. We do not have a global variable option, however.

avatar image Odb718 Sep 21 '18 at 10:21 AM

It's 9/21/2018. Do you guys have a global variable yet?

avatar image Maslobojik May 06 '15 at 02:04 PM

I created interface, but I could not do anythink with him. Disabled. Why?

avatar image Alexander Paschall ♦♦ EPIC May 06 '15 at 04:04 PM

Interface functions are defined per blueprint that implements them. You'll have to go into the blueprint that is implementing the interface, then in the function tab select the function you want to define. It will open up like any normal function, but have the inputs and outputs you have predetermined in the BPI. If you can give me some more information about what you are experiencing, I'll be happy to further assist.

avatar image DanzigLucifuge Mar 18 '17 at 01:57 AM

I wish people would stop suggesting BP interfaces for this problem. BP interfaces can communicate values between bps but you still can't define global vars through it.

avatar image ilias48rus Mar 22 '18 at 02:11 PM

Well, you can make sending message to storage actor to initialize sending the value from it :)

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

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".)

alt text

The proposed solution with the custom actor didn't work for me as easily as I use multiple streamed levels.

capture.png (69.8 kB)
more ▼

answered Oct 19 '16 at 04:43 AM

avatar image

bobdeschutter
43 1 4 5

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

by definition it should be the role of "Game State" (or a local, non network version of it) I just found GameInstances that are also persistant through levels https://www.youtube.com/watch?v=5w594D3qtLs

more ▼

answered Dec 13 '17 at 08:10 PM

avatar image

phil123456789
26 2 4

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

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

more ▼

answered Mar 10 '15 at 04:56 PM

avatar image

unit23
453 25 44 77

avatar image DanzigLucifuge Mar 18 '17 at 02:16 AM

Ok awesome except now, I get an error that says the self reference for those variables is invalid. Anyone know how to get around that?

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

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.

more info at: https://forums.unrealengine.com/development-discussion/blueprint-visual-scripting/111663-global-variable

more ▼

answered Oct 02 '18 at 03:09 PM

avatar image

MakeBeliever
0 1 1 3

(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