Unreal Engine Blueprint – Cycle Through an Array

This tutorial will show you how to cycle through an array. It will cycle through a selection of different colors to do this, but it can be used with other functions as well and not just changing colors.

Components:

  1. Right click in the required folder and create an actor blueprint.
  2. In components panel add:
    1. A Static mesh and in its details panel add the required mesh e.g. Lamp shade or light fitting.
    2. Add a collision box and resize to the required size (make sure the players character can fit into the box).
    3. Add a point light and position where required.

Event Graph:

  1. To replace the Event Interact Interface (red node) in the image below see NOTES at bottom of page.
  2. Add a Branch.
  3. Drag of execution pin and search for Set Light Color
    1. This will be added with the point light (if not then drag point light from components and attached to set light color).
  4. In variables panel:
    1. Add a new variable – then change type from Boolean to Color Actor
    2. Right click on Color Actor to make it an array.
      1. In its detail panel (on the right hand side)
      2. Add 3 Array Elements.
      3. Change the color of each.
      4. You now have man array of 3 colors.
    3. Add anĀ Integer and name it Current Index.
    4. Drag these both into the graph.
  5. Drag off the Color array node and search for Get (a copy).
    1. Plug in the Current index into the lower socket of Get (a copy).
  6. Drag off Get (a copy) and plug into New Light Color of the Set Light Color node.

Finishing off the Graph:

  1. Drag the Current Index node into the graph again.
    1. Select the Set option.
    2. Drag off Current Index socket.
    3. Add a + node.
    4. Add 1 into the bottom box.
    5. Drag of the top left node and search for and add the Current Index node again.
  2. Now you will be able to cycle through the Array index by adding 1 to its element each time you press the interact key (E.g. E key).
    1. This will need to reset to 0 though.
    2. Follow the image below to add the rest of the nodes.
  3. To add the LENGTH node
    1. Drag off from the Color array node (once you’ve added it)
    2. Search for LENGTH.
    3. This simply gets the number of elements in the array (3 in this example).

Completed Graph: Including view of variables and the array color elements.

Array node overview

Whilst this may look a little complicated its fairly straight forward:

  • This blueprint creates a color array of 3 colors
  • Sets the light to these colors.
  • Then adds 1 to the current index (of the array node) so the colors are cycled through.
  • Once the Index gets to 2 (Arrays start from 0 so 2 is the 3rd element) it will start again from 0.

Summary:

When your character walks up to this light and the player presses the E key the light will now change color and pressing the E key again will move to the next color in the Array.

For further blueprint tutorials please see the blueprints page by clicking here.

NOTE:

  1. I have used an interface to connect the button E to all the characters interactions.
  2. You don’t have to do this – but it is a highly recommend and more advanced approach.
  3. You can simply add the nodes below to add a key directly into the blueprint.
  4. Once the nodes below are added – then replace the Interface node key with the E key (or a key of your choosing – see more detailed instructions by clicking here).