Learn

Chapter 03 Conditions, Time & Loops

#027 Arrays

Ten weapons. Ten variables? No, one array.

  • 1:14 video
  • Written lesson
  • Beginner
Video lesson: Arrays, 1:14

One variable, many values

Weapon One, Weapon Two, Weapon Three: every new item needs a new variable, and you can’t loop over them.

An array is one variable that holds many values, all of one type. To make one, select the variable, then switch its type icon from Single to Array.

Each item sits at an index, and indexes start at zero. Length counts the items, so the last index is Length minus one.

Example: an inventory

The array holds Pistol, Rifle, Shotgun.

  • Add puts a new item at the end. Its Return Value is the new index: 3. Length is now 4.
  • Remove Index 1 deletes the Rifle, and every item after it moves down. The Shotgun was index 2; now it’s index 1, and Length is 3 again.
  • Add allows duplicates. Add Unique skips any item the array already holds.

Don’t store positions

Beginners miss that shift: a saved index might now point to another item. So look items up with Find Item right when you need them. It returns the item’s index, or minus one if it isn’t there.

Need only a yes or no? Contains Item returns true or false.

In short

One variable, many values, counted from zero.

Covered in this lesson

  • Array
  • Length
  • Add
  • Add Unique
  • Remove Index
  • Find Item
  • Contains Item

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.