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:
- Right click in the required folder and create an actor blueprint.
- In components panel add:
- A Static mesh and in its details panel add the required mesh e.g. Lamp shade or light fitting.
- Add a collision box and resize to the required size (make sure the players character can fit into the box).
- Add a point light and position where required.
Event Graph:
- To replace the Event Interact Interface (red node) in the image below see NOTES at bottom of page.
- Add a Branch.
- Drag of execution pin and search for Set Light Color
- This will be added with the point light (if not then drag point light from components and attached to set light color).
- In variables panel:
- Add a new variable – then change type from Boolean to Color Actor
- Right click on Color Actor to make it an array.
- In its detail panel (on the right hand side)
- Add 3 Array Elements.
- Change the color of each.
- You now have man array of 3 colors.
- Add anĀ Integer and name it Current Index.
- Drag these both into the graph.
- Drag off the Color array node and search for Get (a copy).
- Plug in the Current index into the lower socket of Get (a copy).
- Drag off Get (a copy) and plug into New Light Color of the Set Light Color node.
Finishing off the Graph:
- Drag the Current Index node into the graph again.
- Select the Set option.
- Drag off Current Index socket.
- Add a + node.
- Add 1 into the bottom box.
- Drag of the top left node and search for and add the Current Index node again.
- 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).
- This will need to reset to 0 though.
- Follow the image below to add the rest of the nodes.
- To add the LENGTH node
- Drag off from the Color array node (once you’ve added it)
- Search for LENGTH.
- 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.
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:
- I have used an interface to connect the button E to all the characters interactions.
- You don’t have to do this – but it is a highly recommend and more advanced approach.
- You can simply add the nodes below to add a key directly into the blueprint.
- 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).
Leave a Reply