Global Variables for every Blueprint scripts?

Hi everyone,

Please forgive me I’m pretty new in Blueprint. is there a way to declare a set of global variable like colors and strings that I can call in specific blueprints?
Basically I’m creating an interface and I would like to get the same set of colors and datas instead that change the same colors, names and datas for each blueprint script.

Many thanks

You could create a Blueprint function library that provides functions that just return the desired values.

Can you please share an example?

any example or solution?

You can set them in the game mode and pass them to the game state. Then Get Game State -> Variable

Every Variable you create inside a Game Instance can be get/set globally and is persistent.
​​​​​

Ok this is because I need to have same colors and strings for every control in the interface. So let me try to create variables in the game instance, but how can I get them in a blueprint?

First go to Project Settings and under Maps & Modes set your Game Instance to be the default one. Then you have two options, one is to “Get Game Instance > Cast To NameOfYourGameInstance” and get the variables that you need, the other option is to create a Blueprint Function Library and inside it create a new function that does the same thing and connect it to be the return value, after which you can simply call it from any blueprint.

I cannot get my variable to a blueprint code. Could you please share an image?

Ok so basically this is what I need to do and it is the common work in every development stage.

  • Declaring some global color variables in GameInstance
  • Change the UI based on those variables

Could you please share an example?

Like this:

  1. Make your game instance, right click in content browser and create a blueprint:

  1. Open your game instance and put variables in it. I made one called GlobalFloat and set it to 23.6

  1. It’s accessible anywhere, like this:

1 Like

Oh many thanks ClockworkOcean
At this point I think it’s coming convenient to use textures and images and design a bit less dynamic interface in terms of software. But it looks better.

There should be some method to get/set a GLOBAL value without casting…like actual global general variables.

How do you use a function library where you want to read some last function call value, do some process and then store the last result for the next function call without lose performance casting inside the function?

not possible?

Create an interface and implement it in any of the framework classes - bam, global variables, no casting. Also, you’d be using getters, like a proper programmer. :slight_smile:

2 Likes

googling interfaces and getters right now LOL

Thanks Everynone!

Do note that 9/10 people in the know will tell you that global variables are a bad idea. The idea of OOP is to encapsulate and keep things on the need-to-know only basis. Not the other way round.

It’s just bad practice imho. Once you structure your code properly and learn blueprint comms (direct, dispatchers and interfaces), you’ll never want or need global variables.

And if you ever need to fetch something, it’s always a cast-away in a framework class that is already loaded, making things somewhat global anyway.


But an interface to a framework class that holds a refence to a data base actor is not a bad idea.

Can you explain how to use an interface with a function library?

I want to start with a dumb test:

Every time I call a COUNT >> function library << it must increment 1 and print the results in the screen. No matter who calls the Function.

Just cant implement interfaces in the Class settings of a library function.

  • blueprint interface:

  • implemented in the Game Mode that does the counting:

  • function library

image

  • something counting stuff:

image


Seems overengineered.

I think I have a solid understanding of tickers dispatchers, timers, timelines, delegates, etc but somehow I always avoided interfaces. Usually I share common data in gamemode or just cast if really needed. But I in this case I want to do a dumb time measure functions library just to measure time of any block of code on start and end . For example:

A ‘global’ FDateTime variable called StartedTime.

a library function for start like this:

void StartMeasuring() { StartedTime = UKismetMathLibrary::Now(); }

and a results function like this:

int32 GiveResults() { return  UKismetMathLibrary::Subtract_DateTimeDateTime(UKismetMathLibrary::Now(),  StartedTime).GetFractionMilli(); }
1 Like

you are counting in Gamemode and not in the library function.

So if you want to use the function in Playercontroller you must count inside it too…I want the library function to make the task if not if useless :slight_smile:

What i want is this:

in gamemode you call in the ticker CountSomething and it prints in the screen 1…2…3…4…no math in gamemode. that is why I want to get the value inside the function, add and store the value again.

is ok if the value is stored in gamemode but is not ok if the math is in gamemode.