Skip to content Skip to sidebar Skip to footer

Unreal Engine C++ Reading a Variable in Blueprints

Welcome Back! Do you desire to keep where you left off?

In this Quick Start Guide, you volition larn how to implement some bones in-game HUD elements and a front end menu using Unreal Motility Graphics UI Designer (UMG) by learning how to implement Wellness and Energy Bars likewise as a way to track and display the Ammo of a histrion.

Over the form of this guide, links to additional documentation will be provided which volition go deeper into each section'south subject area should yous have additional questions. While this guide volition encompass the actions needed to go on through each step, if yous have non worked with the Unreal Editor before, information technology is recommended that you first look over the Level Designer Quick Start guide to proceeds a better agreement of general editor usage, terms, and processes.

1 - Required Project Setup

For this tutorial we are using the Design Get-go Person template with Starter Content enabled.

  1. Click the Add together New button within the Content Browser, so under User Interface select Widget Pattern and proper noun it HUD.

    RequiredSetup_1.1.png

    All User Interface elements (HUD, Menus, etc.) will be created and housed inside a Widget Blueprint. The Widget Blueprint allows you to visually layout your UI elements as well as provides scripted functionality for those elements. We are creating this at present so that we can tell it to exist displayed when our role player character spawns in the globe, all the same we will gear up it up later.

    For more information on Widget Blueprints see the Widget Blueprint documentation.

  2. Create 2 more Widget Blueprints, one called MainMenu and some other called PauseMenu.

    AllWidgets.png

  3. Right-click in the Content Browser and create a new Level called Primary.

    RequiredSetup_1.3.png

    We will use this later on in the guide for our Main Menu setup.

  4. In the Content Browser, open the FirstPersonCharacter Blueprint located nether the Content/FirstPersonBP/Blueprints folder.

    UMGQS2.png

    This is the playable character'south Blueprint in which we volition create some information to pass to our HUD Widget Blueprint for display.

  5. In the My Blueprint window, click the Add Variable Push.

    RequiredSetup_1.5.png

  6. In the Details panel for the new variable, name information technology Health, modify it to a Bladder variable type and set the Default Value to 1.0.

    RequiredSetup_1.6.png

    This variable will represent the player character's Wellness that we will display on the HUD.

  7. Create another Float variable called Energy with a Default Value of 1.0.

    RequiredSetup_1.7.png

    This will represent the histrion character's Energy that nosotros volition display on the HUD.

    Unable to enter a Default Value? Click the Compile push from the Tool Bar to compile the Blueprint then attempt once more.

  8. Create another variable of the Integer type chosen Ammo with a Default Value of 25.

    RequiredSetup_1.8.png

  9. Create ane more Integer variable chosen MaxAmmo too with a Default Value of 25.

  10. Inside the Graph window, find the Consequence Brainstorm Play node. Right click on the execution pivot and select Break Link to Branch ().

    RequiredSetup_1.10.png

    This volition disconnect the Event Begin Play and so we can add our new functionality.

    Yous can also apply Alt + Click on the execute pin to disconnect it.

  11. Select the Event Begin Play node and drag information technology to the left to make room for the new nodes we will be creating in the next steps.

  12. Drag off the Effect Begin play then add together a Create Widget node with Class fix to your HUD Widget Blueprint.

    RequiredSetup_1.12.png

  13. Off the Return Value of the Create HUD_C Widget, select Promote to Variable and name HUD Reference.

    RequiredSetup_1.13.png

    This will create our HUD Widget Blueprint when the game is started and store information technology as a variable that we can admission later. This is useful for calling functions or setting backdrop of the HUD after on, for example if yous wanted to hide the HUD while the game is paused y'all tin can access the HUD through this variable.

  14. Elevate off the out pin of the Set node and add the Add to Viewport node.

    UMGQS9.png

    As the name suggests, this will add the Widget Blueprint specified equally the target to the player'south viewport, cartoon it onscreen.

Adjusting Character Variables

The final thing nosotros will do earlier we outset generating our bodily HUD is provide a way for our grapheme variables to modify.

  1. Inside the FirstPersonCharacter Pattern, while holding Alt drag in the Energy variable and place information technology next to the Leap script.

    Step2_1.png

  2. Concord Ctrl and drag in a copy of the Energy variable and connect it to a Bladder - Float node set to 0.25 and connect as shown.

    Step2_2.png

    Each fourth dimension the graphic symbol jumps, 0.25 will be subtracted from the grapheme'southward current Energy value.

  3. Ready up the same script for the Health variable simply employ an F Cardinal Event (or whatever other primal printing event) for testing.

    Step2_3.png

    This will allow us to test that our Health variable is properly displayed through our HUD whenever a key is pressed.

  4. Find the Spawn Projectile script and following the InputAction Fire issue, Correct-click and add a Branch node.

  5. For the Co-operative status, Ctrl drag in the Ammo variable connected to a Integer > Integer variable set to 0 and connect as shown.

    Step2_4.png

    This volition but permit the player to fire a projectile if their ammo is greater than zero.

  6. At the terminate of the Spawn Projectile script following the Play Audio at Location node, set Ammo to equal Ammo - 1.

    required-setup-1-2-6.png

    This will subtract 1 ammo each time the histrion fires a projectile, giving us something to display on our HUD.

    To add the ability to reload, add an R key event, alt drag in the ammo variable, ctrl drag in the max ammo variable and connect it as shown in the image below:
    RequiredSetup_ReloadAmmoNote.png

  7. Compile and Save and so shut the Blueprint.

In the next step we will set up the displaying of our Health, Energy and Ammo variables on our HUD and get them working in-game.

2 - Displaying Health, Energy & Ammo

Visual: Wellness, Energy and Ammo

Here nosotros will brainstorm setting up the visual aspects of our HUD.

  1. Open your HUD Widget Blueprint to admission the Widget Design Editor.

    Step2_6.png

    Within the Widget Blueprint Editor nosotros tin provide the visual layout and scripted functionality of our HUD.

    For more than information on each of the unlike facets of the Widget Blueprint Editor, see Widget Blueprints.

  2. Inside the Palette window nether Console, drag a Horizontal Box onto the CanvasPanel in the Bureaucracy window.

    DisplayingHealth_2.2.png

    Panel Widgets are sort of similar containers for other widgets and provide boosted functionality for the widgets that are inside them.

  3. Also under Panel, drag two Vertical Boxes onto the Horizontal Box.

    Step2_8.png

  4. Under Common, drag 2 Text widgets onto the start Vertical Box and two Progress Bars onto the second Vertical Box.

    Step2_9.png

  5. Select the Horizontal Box, then in the graph resize the box and position it in the upper left corner of the window.

    Step2_10.png

    The progress bars will appear very small but don't worry, we volition fix that next.

  6. Select both Progress Bars, then in the Details panel ready both to Fill for the Size.

    DisplayingHealth_2.6.png

  7. Select the Vertical Box containing the Progress Confined and set it to Fill as well.

    DisplayingHealth_2.7.png

  8. Select the Horizontal Box again and resize information technology and then the bars line upwards with the text.

    Step2_13.png

  9. Select the top-most Text widget in the Hierarchy window then in the Details console nether Content enter Health :.

    DisplayingHealth_2.9.png

    Do the same for the other Text widget but label it as Free energy then your graph looks like below.

    Step2_15.png

    Past default, UMG text widgets use a font that ships with Unreal Engine. This gives you lot something to work with out of the box so you tin can get started quickly. However, this born font has some limitations; for case, information technology simply supports a small-scale prepare of languages. For most projects, particularly those that crave UIs with non-English text, you lot'll desire to import your own custom fonts into Assets and ready your text widgets to use your fonts. For details, come across the documentation on Fonts.

  10. Select the Progress Bar adjacent to health and in the Details panel set up the Fill Color and Opacity to a light-green color.

    UMGColorPicker1.jpg

    You may notice that Progress Bar does not change colour when assigning a color. This is because the Percent value to fill the bar is set to 0.0 (you can change this to exam out different colors, later nosotros volition link this to the health value of our character).

  11. Also set a Fill up Color for the Free energy bar (nosotros selected an Orange color).

  12. For the display of Ammo, using the methods from higher up, add widgets to the Hierarchy so your setup is laid out like below.

    Step2_16.png

  13. With the Horizontal Box containing the ammo information selected, resize it and position it in upper right corner.

    DisplayingHealth_2.13.png

  14. With the Horizontal Box still selected, in the Details panel click Anchors and select the upper-correct Anchor.

    DisplayingHealth_2.14.png

    This will move the Anchor Medallion to the upper-right corner of your screen.

    Step2_19.png

    By Anchoring a Widget, you are indicating to the Widget that it is to announced in its placed position relative to the position of the Ballast Medallion regardless of screen size. In other words, when the screen size changes, move the widget the same distance away from the Ballast Medallion's position (in this example the same distance away from the upper-right corner of the screen).

    Yous can test different screen sizes by clicking and changing the Preview Size selection within the graph.

Script: Wellness, Energy and Ammo

Now that we have our visual layout, we can add the hooks to provide the functionality backside our HUD elements.

  1. Click the Graph push button in the upper-right corner of the Widget Blueprint Editor window.

    Step2_20.png

  2. In the Graph under the Event Construct node, Right-click and add a Get Histrion Grapheme node.

  3. Elevate off the Return Value pin and select Bandage to FirstPersonCharacter

    ScriptHealthEnergyAmmo_2.3.png

    Here we are checking if the Character Blueprint being used by the Actor Character is indeed the Offset Person Character Pattern.

  4. Elevate off the As Starting time Person Character pin and select Promote to Variable (name it My Character) then connect as shown.

    Step2_22.png

    This will allow us to admission the variables contained within our FirstPersonCharacter Blueprint.

  5. Click Compile in the toolbar to compile the script.

  6. Return to the Designer tab and select the Progress Bar next to Wellness.

  7. In the Details console under Progress, click the Bind option adjacent to Percent and set it to use Wellness from MyCharacter.

    ScriptHealthEnergyAmmo_2.7.png

    This binds the value of the Progress Bar to our Wellness variable inside our First Person Character Design. Now whenever our Wellness value changes in our Grapheme Blueprint it will automatically be updated in our HUD as well. Yous may also notice that the Progress Bar does not alter after binding information technology. 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).

  8. Select the Progress Bar next to Free energy and repeat the process higher up, bounden Percentage to Energy from MyCharacter.

    ScriptHealthEnergyAmmo_2.8.png

  9. Select the 25 post-obit the Ammo text, then in the Details panel for Text, click Bind and Create Bounden.

    ScriptHealthEnergyAmmo_2.9.png

    Now, like the manner we previously used Sub-Object Properties to bind properties of the same blazon, we can likewise create our own custom bindings. Here we are going to bind our Text property to an Integer property from our Showtime Person Character Blueprint giving the states a display of our electric current Ammo.

  10. Within the function that is created and automatically opened, hold Ctrl and elevate the MyCharacter variable into the graph.

    Step2_27.png

  11. Drag off the pin MyCharacter and select get Ammo.

  12. Connect the Ammo pin to the Return Value of the Return Node.

    Step2_28.png

    A conversion node To Text will automatically be created for you when connecting.

  13. Repeat the process above for the other 25 text and creating a binding for the "Max Ammo" text.

    Step2_29.png

    You probably don't need to do this if your Max Ammo is going to remain the aforementioned, however this volition let you to change it.

  14. Click Compile and Save and so click the Play button to play in the editor.

Our Health, Free energy and Ammo values are at present 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 claw up to a damage system at a after date)

In the adjacent department we will create a Main Card which nosotros tin use to load into our game that we've setup.

The adjacent section will provide y'all with boosted information on working with Unreal Motion Graphics and Blueprints.

three - Farther Reading

To continue to build on the HUD you created in this Quick Start guide, see the Creating a Principal Carte du jour and Creating a Pause Menu.

Equally for coverage related to the topics in this guide:

  • For more than information on Unreal Motion Graphics, see: UMG UI Designer

  • For more data on Blueprints, encounter: Blueprint Visual Scripting

gosneyourning.blogspot.com

Source: https://docs.unrealengine.com/4.26/en-US/InteractiveExperiences/UMG/QuickStart/

Post a Comment for "Unreal Engine C++ Reading a Variable in Blueprints"