unreal-engine4unreal-blueprintunreal-umg

How to setup HUD widgets correctly in UE4


I am following a tutorial on Udemy on Unreal Engine based game development. I have posted the sam question on Udemy as well. I will try my best to explain the issue I am facing.

What works

I have set up a

I have set the HUD in FPS_GameMode to FPS_HUD. I have assigned the FPS_GameMode as GameMode in project settings.

Now I have created 2 blueprints MainHUD and UI_DynamicCrosshair. UI_DynamicCrosshair consists of functionality related to resizing the crosshair if the character is moving. (It spreads out if the character is moving). I have used this crosshair in the MainHUD (used UI_DynamicCrosshair in the MainHUD's viewport). In my FPS_Character blueprint, I am creating the MainHUD widget and adding it to the viewport.

The crosshair widget shows up when I play the game but it does not update when my character moves.

What doesn't work

I need to call the functionality defined in my UI_DynamicCrosshair, in FPS_Character so that I can trigger it when the character moves. For that, I tried using the MainHUD reference as it is accessible in the FPS_Character blueprint assuming that I would be able to access the UI_DynamicCrosshair via the MainHUD reference. But this doesn't work. The UI_DynamicCrosshair is not accessible via the MainHUD reference.

Can you share a checklist/list-of-steps so that I can crosscheck everything I have done and figure out what I have missed?

Please let me know if you need more details.


Solution

  • I am assuming it's all in Blueprints, because you're not mentioning any C++ code.

    Seems to me that your UI_DynamicCrosshair instance is just set as private, as you would normally be able to access it via a MainHUD reference if it were set as public. Make sure that this checkbox is unchecked in your MainHUD class, when your UI_DynamicCrosshair variable is selected.

    Although, the better (or nicer) way to achieve this would be to create a public function, let's say SetCharacterMovement(Boolean bIsMoving), in your MainHUD, and to implement the logic that does the resizing in it.

    You can then just call this function on your MainHUD reference from your FPS_Character when you need to update the crosshair. That way your PlayerCharacter does not have to be aware of the inner logic of your HUD, which takes care of itself (and its 'children').

    It's a principle called Separation of concerns, and that will help you design things the clean way.