Arrays

An array is a type of container that stores one or more values of a specific data type. Thus, a single variable can be used to store multiple values instead of using separate variables for each value.

Arrays provide indexed access to their elements, so the label used to retrieve an element is the element's sequential index in the container.

To create an array, first create a variable and define its type. Then, click the icon next to the Variable Type drop-down menu and select the icon of an array, as shown in this screenshot:

Compile the Blueprint to be able to add the default values of the array. In the Default Value panel of the variable, click the + icon to add elements to the array. The next screenshot shows an example of an Integer array named Ammo Slot with four elements used to store the amount of ammunition the player has. Each element of the array stores the amount of ammunition of one type of weapon:

Arrays always start from index 0. So, in the previous example with 4 elements, the index of the first element is 0, and the index of the last element is 3.

To get a value from an array, use the Get (a copy) node. This node has two input parameters, which are a reference to an array and the index of the element, as shown in this screenshot:

To modify an element of an array, use the Set Array Elem node. The example in the next screenshot sets the Item value in the element to 75, with an Index value of 3:

Two nodes can be used to add elements to an array. The ADD node adds an element to the end of the array. The INSERT node adds an element at the index passed as an input parameter:

You can get the number of elements in an array by using the LENGTH node. The LAST INDEX node returns the index of the last element of the array. The following screenshot shows these two nodes:

Let's create an example to examine the use of arrays that store instances of Actors. In this example, we'll create a Blueprint called BP_RandomSpawner, which will have an array of Target Points. The elements of the Target Points array can be set in the Level Editor. When the level starts, the BP_RandomSpawner Blueprint will randomly select one element of Target Points and spawn an instance of a Blueprint in this location.

These are the steps to create this example:

  1. Create or use an existing project that has the starter content.
  2. Click the Add New button in Content Browser and choose the Blueprint Class option.
  3. On the next screen, choose Actor as the parent class.
  4. Name the Blueprint BP_RandomSpawner and double-click it to open the Blueprint Editor.
  5. In the My Blueprint panel, create a new variable named TargetPoints. In the Details panel, click the Variable Type drop-down menu and search for target point. Hover over Target Point to display a submenu and then choose Object Reference. Click the icon to the right of Variable Type and select the Array icon. Check the Instance Editable attribute, as shown in the next screenshot:

  1. Right-click on Event Graph and add Event BeginPlay. Drag the TargetPoints variable from the My Blueprint panel and drop it into Event Graph. Choose the GET Target Points option to create a node. Add the Actions shown in the next screenshot. The Random Integer in Range function returns a random integer between the value of 0 and the last index of the Target Points array. This value is used as the index in the GET node. The transform of the selected Target Points is used to define the location where an instance of a Blueprint will be spawned. In this example, the Blueprint spawned is SpawnActor Blueprint Effect Smoke, which is from the starter content. To use another Blueprint, change the Class input parameter in the SpawnActor Action:

  1. In the Level Editor, look at the Modes panel on the left. Search for target, as shown in the next screenshot. Drag Target Point and drop some instances in different locations of the level:

  1. Drag BP_RandomSpawner from Content Browser and drop it on the level. The TargetPoints variable appears in the Details panel because we checked the Instance Editable attribute. Click the + icon to add elements to the array. Click the drop-down menu of each element and select one of the TargetPoint instances that are in the level. The next screenshot shows the array with 5 elements:

  1. Click the Play button of the Level Editor. BP_RandomSpawner will spawn an instance of Blueprint Effect Smoke at one of the TargetPoint instances.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset