Learn

Chapter 03 Conditions, Time & Loops

#030 For Each Loop with Break

Five enemies. You only need the first one within range. So stop there.

  • 1:49 video
  • Written lesson
  • Beginner
Video lesson: For Each Loop with Break, 1:49

For Each Loop plus Break

It’s For Each Loop plus one execution input: Break. The outputs don’t change: Loop Body, Array Element, Array Index and Completed.

Call Break from inside Loop Body, and the remaining items are skipped. Then Completed fires, just like after the last item. Without Break, a plain For Each Loop keeps checking every enemy, even after the match.

Example: a target lock

Loop Body asks a Branch: is this enemy in range? Enemy 0, no. Enemy 1, no. Enemy 2, yes. True sets Target to that Array Element, then calls Break. Enemies 3 and 4 are never checked, and Completed fires once.

But if nobody is in range, Completed fires too, and Target is still None. Check it with Is Valid first.

The same pattern finds the first free slot, the first open door, or the first item a player can afford.

Chapter 3 recap: time and loops

  • Set Timer by Event runs an event later, or on repeat. Clear and Invalidate stops it for good; Pause keeps the time left.
  • A Delay can’t be cancelled. When you need to cancel, use a timer.
  • For Loop runs every index, both ends included, in one frame. With Break, it stops early.
  • While Loop repeats while its condition is true.
  • Arrays hold many values from index zero. Get reads one, and For Each Loop visits them all.

Three final tips

  • Loops never wait. For work over time, use a timer.
  • Check an index with Is Valid Index before calling Get.
  • Never remove items mid-loop, unless you iterate backwards.

Next, chapter 4: variables and data types, from Booleans to structs.

In short

Found a match? Break. Completed still fires.

Covered in this lesson

  • For Each Loop with Break
  • Break
  • Loop Body
  • Array Element
  • Array Index
  • Completed
  • Branch
  • Is Valid

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.