Global variable ?


I want to make a variable that is accesible from my hud(ex: event actor begin overlap=>change variable to true, and then use the variable in MyCharacterBp,another example button pressed from hud=>variable true, do something in level blueprint ) . I tried the level instance option,didnt work same for level database. Im new to this so help pls.

Create a new Actor and place all variables that you want as global variables in it. Then every time you want to get to those variables, use a Get All Actors Of Class and choose the variable Actor you have made as the Actor Class. Then from the Array on right right, drag off a Get node, and leave the Index under that as 0. This will allow you to access the same Actor every time to get the variables. From there, you can drag off of the Actor the Get node has selected from the array and access all variables. They will be editable from all elements that you have placed this setup in. Cheers! :slight_smile:


It sounds like you are trying to allow your Actors, Character and HUD to communicate with each other. If that’s the case you can benefit from watching my tutorial video here Unreal Engine 4 Blueprints - Create a scrolling journal/log HUD widget. - YouTube which explains who to allow those 3 components to communicate to each other (on Actor Begin/End Overlap).

Check out my video and let me know if that helped you at all. If not I’m more than happy to find another solution that works for you, given some good practical examples of what you are trying to achieve.

Sounds like a perfect job for your GameState Actor because it’s already replicated to all machines including Authority server and each client. GameState should also be accessible to your UMG widgets.

Is the GameState not going to be a good solution for you?

Touche! I’m getting more and more into the design through UE4 and basically throw out anything that I know to work for myself. Since I’ve not really used GameState, this was pretty unknown to me. That will actually be handy for myself, as well. :slight_smile:

Good call, elite. If you are needing direct communication, doing it through casting is a great option. But if you’re needing an actual global variable that all actors have access to at all times, you’ll need a controlling actor to house the variables that need to be placed in your level or game.

Your GameState should store anything related to the state of the game. For example in an RTS it would store the different teams, each teams resource balance and even an array of different units that were built.

I think you should use the game state to solve your problem.

The problem with using GameState is the game state is not persistent through level-loading. Look into making a GameInstance if you want the variables to be persistent. This site: Game Instance has some great information on it. On the site, he’s using C++ to code the variables, then extends the class using a Blueprint. However, if you don’t know how to code, you can just extend directly from GameInstance and fore-go the C++.

Thanks ! :slight_smile:

Not a problem! I’m rather novice in a lot of this, so based off what vipulul said, I came up with a better solution, ane something more acceptable. So, create a custom GameInstance blueprint and use that as the containers for the variables. This will allow for seem less persistency to other levels, and it reduces casting cost. To implement, simply Cast To MyCustomInstance and off of Object, GetGameInstance, and have As MyCustomInstance set a local variable that references the actor blueprint for the custom instance. This will allow you to cast once at the beginning of play, but still be able to quickly access the reference to get or set variables enclosed in it. And Vipulul, if that’s what your video outlines, I apologize. I’ve been unable to watch it, but I’m assuming you outline that at some point.