Learn

Chapter 03 Conditions, Time & Loops

#026 While Loop

Reload until the clip is full. How many rounds is that? You don't know, so use a While Loop.

  • 1:13 video
  • Written lesson
  • Beginner
Video lesson: While Loop, 1:13

A condition instead of a count

A For Loop needs a Last Index up front. A While Loop only needs a condition.

It has one exec input and a boolean Condition. Before every iteration, it checks Condition: true runs Loop Body, then it checks again. When Condition is false, Completed fires once. If it starts false, Loop Body never runs.

Condition is read again every time, so wire it to the value your Loop Body changes.

Example: reloading

Clip is 2, and the magazine holds 6. Condition: Clip is less than 6. Loop Body calls Load Round, which adds one.

Three. Four. Five. Six. Now the check is false, Completed fires, and the weapon is ready. In game, all four rounds load in one frame.

The same shape fits any unknown count, like adding enemies until the wave is full.

The classic mistake

Loop Body never changes the condition. Condition stays true, the loop never ends, and Unreal stops it with an Infinite loop detected error.

While Loop also can’t wait for something, like a door opening. For that, use a timer or an event.

In short

Change the condition inside the loop, or it never stops.

Covered in this lesson

  • While Loop
  • Condition
  • Loop Body
  • Completed

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.