How to add a widget to a HUD class? (Variables across blueprints?)

Hi, after reading quite a bit about variables across blueprints i still have some trouble with them.

I am trying to set a new variable after a button is clicked (in widget event graph) and use that in another BP. So i tried casting that widget and putting Get HUD, but how would i set my widget as HUD class in GM for that to work?

Thank you.

Hi there,
As you say it might be better to store your “NewPaddleSpeed” somewhere where every Blueprint has access to.
Yep, GameMode would be the easiest choice. So your HUD can set and display it and more important: Your Pawn, PlayerController or whoever needs it, can access it.

Be aware that the GameMode only exists on the server. No issue at all if you do not have more than one player or different GameModes per Level. Else you need to store your “NewPaddleSpeed” in the PlayerController or PlayerState. Can be accessed from the HUD and the Pawn as well.

hope that helps :slight_smile: Marooney

Btw, if you want to access your Widget it is easier to create a Variable (with the Type of your Widget) in your PlayerController. This should hold the Widget for you. When you create the Widget you will get the required reference to it. Then you need to set the Variable to this reference. Now you can access your Widget by GetPlayerController->cast to MyPlayerController->Get MyWidgetVariable → Access every Widget-Variable you wish.

Oooooh…

I tried to do what you said, but it didn’t work. Then I noticed that my widget is in a different level from the one I want to get my variables to. That must change things quite a bit. So I found this https://www.youtube.com/watch?v=K_4CWDxwufg. Putting the variable in a game instance and getting it from there worked perfectly! :slight_smile:

One thing i still don’t understand is how would i put my widget into my HUD class in Gamemode (That widget isn’t a class)?

Thanks Marooney!

you are welcome :slight_smile:

There is really no reason to use the GameInstance other than storing data you need throughout all Levels. It is better to keep everything within the level and let UE do its proper garbage collection on level unload.
If you can access GameInstance than you can access GameMode too.

You will never need a Widget in the GameMode or elsewhere. Try to keep them in your PlayerController. Makes life easier.
I just show you what i mean:

Here is how you can access the GameMode variable from the Widget.