Script: Health, Energy and Ammo
Now that we have our visual layout, we can add the hooks to provide the functionality behind our HUD elements.
Click the Graph button in the upper-right corner of the Widget Blueprint Editor window.
In the Graph under the Event Construct node, Right-click and add a Get Player Character node.
Drag off the Return Value pin and select Cast to FirstPersonCharacter
Here we are checking if the Character Blueprint being used by the Player Character is indeed the First Person Character Blueprint.
Drag off the As First Person Character pin and select Promote to Variable (name it My Character) then connect as shown.
This will allow us to access the variables contained inside our FirstPersonCharacter Blueprint.
Click Compile in the toolbar to compile the script.
Return to the Designer tab and select the Progress Bar next to Health.
In the Details panel under Progress, click the Bind option next to Percent and set it to use Health from MyCharacter.
This binds the value of the Progress Bar to our Health variable inside our First Person Character Blueprint. Now whenever our Health value changes in our Character Blueprint it will automatically be updated in our HUD as well. You may also notice that the Progress Bar does not change after binding it. That is because we've stated this to occur upon Event Construct (or when the HUD is constructed, which occurs when the game is started).
Select the Progress Bar next to Energy and repeat the process above, binding Percent to Energy from MyCharacter.
Select the 25 following the Ammo text, then in the Details panel for Text, click Bind and Create Binding.
Now, like the way we previously used Sub-Object Properties to bind properties of the same type, we can also create our own custom bindings. Here we are going to bind our Text property to an Integer property from our First Person Character Blueprint giving us a display of our current Ammo.
Inside the function that is created and automatically opened, hold Ctrl and drag the MyCharacter variable into the graph.
Drag off the pin MyCharacter and select get Ammo.
Connect the Ammo pin to the Return Value of the Return Node.
A conversion node To Text will automatically be created for you when connecting.
Repeat the process above for the other 25 text and creating a binding for the "Max Ammo" text.
You probably don't need to do this if your Max Ammo is going to remain the same, however this will allow you to change it.
Click Compile and Save then click the Play button to play in the editor.
Our Health, Energy and Ammo values are now displayed on our HUD and reflect our current values from our Character Blueprint. Pressing Space will have the character jump and deplete Energy, pressing the Left Mouse Button will fire the weapon reducing our Ammo, and pressing F will cause us to lose Health (which you could hook up to a damage system at a later date).
In the next section we will create a Main Menu which we can use to load into our game that we've setup.