While Loop

From Augmotic Library
< Unreal‎ | API
Inputs
Condition Loop's test condition to decide when to exit.
Outputs
Loop Body Execution Pulse for each iteration of the loop.
Completed Execution Pulse after the test condition is false.

Overview

While Loop will perform an unlimited series of runs until its condition is false.

These are useful when iterating through an unknown amount of times since the only time the loop exits is by the condition usually set within the loop body.

As long as the condition can be evaluated as a boolean, it is valid. The condition while be checked each time before running the body of the loop. If false at the start, the loop body never runs.

Initializing any values needed by the loop to a known state beforehand is good practice to ensure it behaves correctly.

Warnings

The entire loop will run in a single linear flow when executing. In most cases, the loop has to finish before any other logic runs.

If the iteration count of the loop is very high or the logic inside it is heavy, it could cause performance problems.

Causing an infinite loop is very easy to do if not careful. If the loop begins and there is no way for the condition driving it to be set to false, or it doesn't happen for a lengthy amount of time, a crash can occur.

There is an internal watchdog that attempts to detect and reset itself as a last-ditch effort, do not rely on it. It is best to avoid it entirely or have a backup exit condition after a maximum number of attempts.

Examples

Random Number Rolling

Here we initialize the variables used in the loop, which will repeat, generating a number between 0 and 20 until it rolls a 10. After that, the number of attempts is printed on the screen.