I am quite new to UE, and in my situation i really really need use of global variables for Blueprints and Widgets.
Some simple, discrete and easy way i mean(for such simple thing). On practice something like:
I have global variable of Color type, call it WeirdOrange, and i don’t want ANY extra nodes when i’m using it.
Such functionality i managed to achieve by creating Blueprint, and throw it in to scene, but the problems is:
It works only in that particular level.
I must create references for that Blueprint object.
Need to add extra node for every use of such global variables,
instead of WeirdOrange, there must be BlueprintRef->WeirdOrange every time.
Feels like too much extra work and inconvenient for such intuitively simple and needy thing as Global variables.
The slightly longer answer is, maybe you need a bit more practice with the engine? ( excuse me if that’s wrong ). A lot of people have problems moving data between blueprints and widgets.
The full answer ( in short ), is related to the fact that all programming languages since Basic have spent a lot of time getting rid of global variables for a good reason. Namely, you will end up with unmaintainable, undebuggable code.
@ClockworkOcean I don’t think that pointing newbies towards 30$ plugin is a good idea, especially when it teaches them bad practices.
@Engelard Generally in Blueprints you will always need to reference something in order to achieve this. You could wrap those nodes in something like Blueprint Macro Library, but it would really create just a bigger mess. When it comes to which Class should be used, take a look at Gameplay Framework and pick something that is persistent across levels. “UGameInstance” is probably the best as it lives throughout whole lifetime of the Game, but I’m not sure if you can use it from Blueprints.
Though, I agree, global variables are bad idea, especially when you mutate them in multiple places. That said you probably need to try them first to understand why
The best way to create “global variables” is by creating your own game instance blueprint. This is because you can cast to it and get the variable you want and set the local variable. That is the best method I can suggest without referring to plugins. I don’t suggest to use a plugin for using global variables because it’s a good idea for knowing how the engine works without using third-party plugins instead of relying on them.
For variables that changes during runtime, I think the easiest path is indeed the GameInstance if you’re just starting with the engine.
For variables that don’t change at runtime you should have a look at data assets https://www.youtube.com/watch?v=hcwo5m8E_1o
This is a type of asset inside which you can add any number of variables and them you can reference this asset inside other blueprint. To make things easier to access you can create a Blueprint Function Library that directly reference the data asset and make your globally accessible single nodes that get the right variables inside the assets.
You can make functions in function library, and store that “Global” variable in game mode or game instance.
First make bp game mode and your variable inside of it:
variable declaration in BP_Game_Mode (create this blueprint based on game mode class, add variable)
Second, make Blueprint Function Library. And make those 3 functions inside:
Get Game mode (this is mainly to make SINGLE point/code in case you want to store those global variables elsewhere, also single point to add logging or error checks) this is PURE function (tick that pure checkbox)
ps.
Quite Important: SET that variable only from SINGLE place, and everybody else reads it only.
If you start setting this variable from different blueprints you may get weird results, because you cannot be sure which place set it last. It is very easy to mess things up, unless you only care about most recent value.