Working with Arrays

Choose your OS:

If you are not familiar with programming terms, the best way to think of an Array is that it is a collection of variables of the same type, contained within a single entity. For example, if you were to create a Float Array, that Array would only be able to hold Float variables in it. Once you have an Array created, you can access individual variables within the Array and modify them or modify all the items in the Array at once (which greatly improves repetitive tasks by doing it all at once versus individually).

On this page, you will learn how to create Array variables inside a Blueprint as well as using the Make Array node to build an Array. Once you understand how to create an Array, you can check out the Example Array Usage section for some sample use cases for working with Arrays.

For more detailed information and usage, see the Blueprint Arrays and Array Nodes documentation.

Array Variables

Creating an Array variable can be done with the following steps.

  1. Inside your Blueprint click the Add Variable button, give the variable a name and set the variable type.

    HT1.png

    Here we have created a Text variable called TextArray.

  2. In the Details panel for the variable, next to Variable Type, click the Array grid button.

    HT2.png

    This will convert the variable into an Array.

    HT3.png

  3. Click the Compile button, then under Default Value, click the + sign next to the trash can icon to add items to the Array.

    HT4.png

  4. You can add as many elements you want to the array and assign its value in the index.

    HT5.png

    For our example, we have created a Text Array with our first entry set to Hello!, our second entry set to GoodBye! and our third entry set to .... This example could be used for an NPC that responds to a player with different text based on whatever conditions you choose to set up.

As seen in the last image, Array's use zero-based index values, meaning that the first index is zero and not 1. Keep this in mind when populating your Arrays, as you access the items in your Array by their Index value and if you wanted to access the first item, you would need to enter 0 as the Index value instead of one. For the second item in the Array, you would enter 1 as the Index and so on.

Making Arrays

When creating an Array variable, you define the items that are to appear in the Array through the variable's Default Value setting. There may be instances where your Array is to be populated at runtime, for example, populating your Array with Actors that exist in your level. In this case, you can use the Make Array node to create an Array out of a series of items instead of creating an Array variable and assigning Default Values.

In this example, say we wanted to have an Array of Camera Actors in our level that we can cycle between.

The steps below will show you how to use the Make Array node.

  1. After placing several Actors in your level (CameraActors in our case), select them all then open the Level Blueprint.

  2. Right-click in the graph and choose the Add Reference to Selected Actors option.

  3. Once you have reference to the Actors you want to include in the Array, Right-click in the graph and add the Make Array node.

    HT6.png

  4. On the Make Array node, click the + sign to add the amount of pins needed for each of the Actors to appear in the Array.

    HT7.png

  5. Drag off one of your Actors and connect it to the 0 (or any other pin) on the Make Array node.

    HT8.png

    This will convert the Array into an Array Type for the selected Actor (the Error message will disappear when you compile).

  6. Connect the rest of your Actors to the remaining pins on your Make Array node.

    HT9.png

  7. Drag off the Array pin, search for and add the Get node.

    HT10.png

    The value in the box on the Get node is the Index (Array Element) to access (Get) from the Make Array node.

    This could also be an Integer variable as depicted below as the variable called Get Camera.

    HT11.png

    Through some form of script, you could Set the Get Camera to an Integer value, effectively changing which Camera Actor is selected in the Get node. Below, this setup could be used to drive the cycling between Camera Actors that are placed in the level.

    HT12.png

Example Array Usage

In this example, we will use Arrays to spawn a random Blueprint effect in our level at a random location.

For this example, we are using the Blueprint Third Person Template project with Starter Content enabled.

  1. Inside your project, from the Modes menu on the Basic tab, drag 5 Target Points into your level.

    HT13.png

    These will serve as the locations in which to spawn our effect.

  2. In the World Outliner, search for Target then hold Shift and select all of your Target Points.

    HT14.png

  3. From the Main Toolbar, click the Blueprints button then Open Level Blueprint.

    HT15.png

  4. Right-click in the graph and select the Create References to 5 selected Actors option.

    HT16.png

    This will create a reference to each of the Target Points in the level so that we may access them.

  5. Right-click in the graph again then search for and add the Make Array node.

    HT17.png

  6. Click the + sign on the Make Array 4 times to add pins to the node then connect the Target Points.

    HT18.png

  7. Right-click in the graph and add the F Key Event and off the Pressed pin, add a Spawn Actor from Class node.

    HT19.png

  8. In the MyBlueprint window, create a new variable of the Actor Class type.

    HT20.png

  9. Name the variable ActorClassArray and click the Array grid icon to make the variable an Array.

  10. Click Compile then in the Default Value section, add 3 Actors (we are using Fire, Explosion, and Spark effects).

    HT21.png

  11. Hold Control and drag in the ActorClassArray variable, then drag off it and add a Get node.

  12. Off the Index pin, search for and add the Random Integer in Range node.

    HT22.png

  13. For the Max setting, set it to 2 then connect the Get pin to the Class pin on the Spawn Actor node.

    HT23.png

    For the Max setting, we are setting it to 2 since the Index values start at 0 and will allow us to get each of our Actors.

  14. Drag off the out pin of the Make Array node and add a Get node.

  15. Off the Index pin, add the Random Integer in Range node again with Max set to 4.

  16. Off the Get pin, search for and add the Get Actor Transform node.

    HT24.png

  17. Connect the Return Value from the Get Actor Transform to the Spawn Transform pin on the Spawn Actor node.

  18. Click the Compile and Save buttons, your graph should look similar to below.

    HT25.png

  19. Close the Blueprint, then click the Play button to play in the Editor.

    When you play in the Editor, each time that you press the F key a random Blueprint (from the ones you added to the Array) will be selected and spawned at a random location from the Array of Target Points. Instead of the F Key Event to spawn the items, and instead of effects that are spawned, you could use this same logic to set up a random spawn location for Health or Ammo pickups or even loot the player can obtain.