How do you make 'Global' variables?

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:

  1. It works only in that particular level.
  2. I must create references for that Blueprint object.
  3. 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.

Thanks

The short answer is, use a plugin

https://www.unrealengine.com/marketplace/en-US/product/global-variable-system

( there are some others ).

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.

3 Likes

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

In C++ the best ones for this are Subsystems

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

2 Likes

I think it would be worse to not mention different possibilities.

Even with a plugin, you need to have a node present, so it’s still not really like having global variables, in the totally scope-less sense…

1 Like

UGameInstance works great at the BP level :smiley: I recommend UGameInstance, too. Subsystem is more great, but maybe Subsystem need c++.

I took some screenshots about GameInstance.


Make new BP

Click “All Classes”, and type “GameInstance”, Select.


You can use like this from anywhere. But before that,


You should set the BP you made in here.

2 Likes

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)
    my global variable

Second, make Blueprint Function Library. And make those 3 functions inside:

Third, how to use it or THE RESULT. This works in ANY blueprint, just like variable that is global.

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.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.