Question about "Global Variables".

Hello,

Again I am new and use a visual scripting program called Adventure Creator. So, now that I am using Unreal it’s just super different and I am trying to get accustomed. In Adventure Creator if you want a global variable, all you have to do is mark it as global. Then it can be used anywhere in the game.

However, for Unreal it seems a lot more complicated. From what I understand there aren’t “Global Variables” I followed this tutorial, but I get stuck at some point. How create global variable?

I feel like what I did was wrong, or I am not understanding something.

I also tried following this tutorial but got lost. https://www.youtube.com/watch?v=HPVUMQ3Y7DY

Could someone explain further? Images and examples are very helpful. I watch Unreal tutorials but they’re sort of fast paced. If anyone knows a more slow paced tutorial for this, let me know. They just go from one thing to another without explaining what anything really means.

Hey there @ImagineTheEnding! So Unreal’s paradigms avoid things like global variables, as they have always been more work to maintain in the long run and worse when scaling games. I personally often recommend creating a new GameInstance and using that to hold your variables that you need to access globally.

Gameinstance are active and maintain data through the entire play session and you can access it from anywhere. This is often where I place saving/loading functionality and data for the session.

Nawrot’s setup over here is probably the closest simple way to keep a global that is maintainable, but it would still suffer from the scale issues if you end up needing more ways to manipulate that variable itself.

Here’s also a video showing how to make a new gameinstance and create variables on it that might make the transition a bit clearer:

Disclaimer: One or more of these links are unaffiliated with Epic Games. Epic Games is not liable for anything that may occur outside of this Unreal Engine domain. Please exercise your best judgment when following links outside of the forums.

1 Like

Depending on the use case you could create a Replicated Actor placed in the level that contains said variables. Having only the Game mode or Game State govern their values (Set).

Each class that needs access to these variable would have to get a reference to the actor and save a copy ref.

Usage of “Globals” imo aren’t recommended.

1 Like