Language:
Page Info
Tags:
Skill Level:
Engine Version:

3 - Adding Keys and Values to a Map

Choose your OS:

By now, you have a TextRender Actor displaying the associated values of a Blueprint Map that you defined in the previous step. In this step, you'll learn how to Add a key-value pair to a Map, subsequently displaying the Map's keys and associated values.

  1. From your project's Content Browser, double-click the Display Map TextRender Actor to open its Blueprint Event Graph.

    Click for full image.

  2. You should see the Display Map TextRender Actor Blueprint Event Graph, where you'll be able to add a new Blueprint Map, which you'll be able to Add a new key-value pair to, ultimately displaying the Map's keys and associated values to the Viewport.

    Click for full image.

  3. At this point in the tutorial, you'll want to create a new Blueprint Map container for the upcoming Add operation. Go ahead and right-click on the Instruction Map variable and select Duplicate from the drop-down list to create a new Blueprint Map variable, naming it Weapon Inventory Map.

    Step3_3.png

  4. Please make sure that the new Map container matches the following properties, where the key is a String and the value is an Integer:

    Step3_4.png

    For illustrative purposes, you're creating a weapon inventory Blueprint Map container, where the key is the weapon name and the value is how much ammunication is available for the weapon.

  5. If you select your new Blueprint Map, the Default Value message, saying Please compile the blueprint, should be visible.

    Click for full image.

  6. Go ahead and click the Compile button.

    Click for full image.

  7. After compiling the Blueprint, you should see the Default Value disappear, making way for a default key-value pair.

    Click for full image.

  8. At this point, the editor should also emit the following warning:

    Click for full image.

    UE4 emits this warning because you're duplicating Instruction Map, which is a Blueprint map having an Integer-String key-value property, to create a Weapon Inventory Map, which is a Blueprint Map having a String-Integer key-value property. During the duplication process, the engine parses key-value pairs belonging to Instruction Map, attempting to copy them into the key-value pairs for Weapon Inventory Map, which is undesired behavior.

    Click for full image.

  9. Go ahead and fill your new Weapon Inventory Map container with the following key-value pairs:

    Step3_9.png

  10. Now, click the Compile button.

    Click for full image.

  11. After filling the Weapon Inventory Map container with the appropriately typed key-value pairs, satisfying the new Blueprint Map's key-value property requirements, you should see the warning disappear.

    Click for full image.

  12. Before you can add new logic to the existing Blueprint Event Graph, go ahead and drag the Weapon Inventory Map variable into the Blueprint.

    Click for full image.

Scripting the Logic

  1. To get started with building your new logic, drag off the Weapon Inventory Map node and add the Keys node from the Map Utilities interface.

    Click for full image.

    The Keys node will output an Array of all keys that are present in your Weapon Inventory Map.

  2. Drag off the Array output pin belonging to the Keys node, search for, and add the ForEachLoop node.

    Click for full image.

  3. Now, drag off the Array Element pin belonging to the ForEachLoop node, search for, and add the Print String node.

    Click for full image.

  4. Right-click in the Blueprint Graph to open the Context Menu, where you'll search for and add Event ActorBeginOverlap.

    Click for full image.

  5. Drag off the Other Actor pin of the Event ActorBeginOverlap node, search for, and add the Cast To FirstPersonProjectile node.

    Click for full image.

  6. Now, connect the Cast To FirstPersonProjectile node to the Keys node.

    Click for full image.

Displaying your Map's Keys

At this point, if you enter Play in Editor (PIE) mode, you'll be able to fire a projectile at the collision box for your Display Map Text Render Actor, where you'll be able to display an Array of keys that are contained in your Weapon Inventory Map.

Click for full image.

Finishing the Script

  1. Now's the time to add a new key-value pair to your container. To do this, go ahead and drag the Weapon Inventory Map variable into the Blueprint.

    Click for full image.

  2. Drag off the Weapon Inventory Map variable, adding the Add node from the Map Utilities interface.

    Click for full image.

  3. Now, define the key-value pair that you'd like to add, defining Weapon 6 as the key and 60 as the value.

    Click for full image.

  4. Drag off the Add node's Boolean Return Value pin, search for, and add the Branch node.

    Click for full image.

  5. Before you can set up the additional display logic, showing your added key, you'll need to drag the Weapon Inventory Map variable into the Blueprint.

    Click for full image.

  6. Now, drag off the Weapon Inventory Map variable, adding the Keys node from the Map Utilities interface.

    Click for full image.

  7. Drag off the Array output pin belonging to the Keys node, search for, and add the ForEachLoop node.

    Click for full image.

  8. Now, drag off the Array Element pin belonging to the ForEachLoop node, search for, and add the Print String node.

    Click for full image.

  9. Click on the Text Color box to open the Color Picker menu.

    Click for full image.

  10. Go ahead and set the color to the following values:

    Step3_28.png

  11. Again, before you can set up more display logic, showing your added value, you'll need to drag the Weapon Inventory Map variable into the Blueprint.

    Click for full image.

  12. Now, drag off the Weapon Inventory Map variable, adding the Values node from the Map Utilities interface.

    Click for full image.

  13. Drag off the Array output pin belonging to the Values node, search for, and add the ForEachLoop node.

    Click for full image.

  14. Now, drag off the Execute Output pin belonging to the ForEachLoop node, search for, and add the Print String node.

    Click for full image.

  15. Drag off the Array Element pin belonging to the ForEachLoop node, connecting it to the In String pin belonging to the Print String node.

    Click for full image.

  16. Click on the Text Color box to open the Color Picker menu.

    Click for full image.

  17. Go ahead and set the color to the following values:

    Step3_35.png

  18. Connect the Branch node's True pin to the Keys node.

    Click for full image.

  19. Now, connect the Branch node's False pin to the Values node.

    Click for full image.

  20. To complete the script's logic, go ahead and connect the Cast Failed pin of the Cast To FirstPersonProjectile node to the Add node.

    Click for full image.

  21. After inspecting your completed script, go ahead and click the Compile button.

    Click for full image.

  22. Finally, click the Save button before going back to the Editor's Viewport interface.

    Click for full image.

  23. From the Editor's Viewport interface, click the Play button to see the updated script at work.

    Click for full image.

End Result

As you can see from the video, when you initially fire a First Person Projectile the Display Map TextRender Actor's collision box, you'll see the original five keys being printed to the Viewport. Then, when you walk through the collision box, you'll see the new key being added, subsequently being printed. After backing out of the collision box, go ahead and fire another First Person Projectile to confirm the newly added key. Finally, when you walk through the collision box for a second time, you'll see all of the values being printed.

Tags