Download

Get player character to bind HUD progress bars with character variables

Hey everyone. I’ve been lurking the forums for a while but haven’t become active yet. I’ve just started getting into UE4 and have really enjoyed it so far, but am having a minor issue. Currently i’m just trying to block out some of the logic for my game in BP’s until I’m more comfortable w/ the way the engine works & will move on to C++ for heavier math functions when I’m more familiar with UE4.

I’ve found a workaround for my problem, but am curious as to what is causing it. When I attempt to cast to my character to get a reference & bind it’s variables to progress bars the cast keeps failing. I’ve seen it done this way in multiple tutorials including the UE4 documentation and can’t figure out why it won’t work in my project. To pass a reference of my character to the HUD blueprint I’ve been having to do this:
e5134457891cb3776f6a1ded6ac73177c93646e1.jpeg

This method will also work without the index check if there’s only one instance of the character in the game, but with multiple characters of this type inside the game the index check is required.

What isn’t working is this:
casttochar2.jpg

I’m not sure why, but this will not get a proper reference to my character & appears to be failing to cast. This method is shown in the documentation here: https://docs.unrealengine.com/latest/INT/Engine/UMG/QuickStart/2/ and in several tutorials I’ve watched, so there has to be a reason it’s not working inside my project. If anyone knows what would cause the “get player character” function to fail I’d appreciate the help.

Regards,
CIMDGN

The first image doesn’t require a cast, since the get all actors is already casting it.

The second image could be failing because of where you’re creating the widget. The widget could be created before the character is, and therefor there is no Character.

My typical setup is to have the creation logic on the Player Controller, and have the function called after the Player Controller possesses the Character. Also, if you set the Owning Player variable to the Player Controller on the Create Widget node, you can use the ‘Get Owning Player’ and ‘Get Owning Player Pawn’ nodes within the Widget Blueprint.

You can’t get character on construct. Use Begin Play instead.
First create game HUD class and set to GameMode.
Create and store your widgets in HUD class.
Then, when you need to update values on HUD from player character, call to GetPlayerController->GetHUD and cast it to your HUD class.
From there you can access widget variables.