Learn

Chapter 03 Conditions, Time & Loops

#028 Get Array Element

Three guns in an array. Ask for slot one, and you get the second gun.

  • 1:13 video
  • Written lesson
  • Beginner
Video lesson: Get Array Element, 1:13

Reading one item

To use an item from an array, you read it out by its index. That’s the Get node: drag off an array pin, search for Get, and pick Get (a copy).

It takes the array and an index, and returns the item stored there. Index 1 is the second item, because indexes start at zero. And a copy means exactly that: changing the result doesn’t change the array.

Example: a weapon switch

Select Weapon passes a Slot number, and Weapons holds Pistol, Rifle and Shotgun. Slot 1: Get returns the Rifle. Slot 2: the Shotgun.

Now Slot 3. There is no fourth item. Get doesn’t crash: it returns an empty default, here None, and the log reports Attempted to access index 3.

Guard it with Is Valid Index. It’s true only from zero to Length minus one.

More details

  • To change an item, use Set Array Elem. It writes a new value at an index.
  • Want the last item? Pass Last Index into Get. Passing Length is one too high.
  • Reading every item, one by one? Don’t count indexes yourself: that’s For Each Loop, the next episode.

In short

Read by index, check the range, and count from zero.

Covered in this lesson

  • Get (a copy)
  • Is Valid Index
  • Set Array Elem
  • Last Index
  • Length

Newsletter

Development updates are coming to your inbox.

The newsletter opens soon. Until then, new lessons, tools and Beach Hunters progress appear first on our channels, linked below.