Page tree
Skip to end of metadata
Go to start of metadata

Introduced in



Description:Repeatedly execute a code block for as long for as the specified condition is true.

Condition is checked before the command is executed, so if it isn't true when first invoked, the code will never be executed (unlike waitUntil, which will always execute the code at least once).

The cycle speed of while is very high, and unless it is a very brief loop, the code segments should always include a sleep statement, as it could otherwise lock up VBS:

while {alive tank1} do {
  hint "Destroy the tank";
  sleep 1;

An ongoing loop can be exited via the exitWith command:

while {alive tank1} do {
  hint "Destroy the tank";
  if (count units group player<2) exitWith {
    hint "Your group is too small to complete the task!";
  sleep 1;

In VBS3 it is no longer possible to cause VBS to lock up with an infinite loop. Instead there is a limit of 10,000 iterations after which:

  • In Non-scheduled Environment the loop is paused for the current frame. It will resume in the next frame at the earliest (other scheduled script may run first).
  • In Scheduled Environment the loop is terminated as if the condition was false and the execution of the script continues.


Syntax:while {condition} do {code}
Return Value:Nothing


while {a<b} do {a=a+1} 
A practical example: Heal all members of a group to such a level that they are able to stand up:
  while {(not canStand _x) and (alive _x)} do {
    _x setDammage (getDammage _x) - 0.01;
} forEach units (group player); 

Additional Information

See also: waitUntil, exitWith, Control Structures


Posted on May 14, 2008
The boolean code that's used to evaluate the while condition can be preceded by code that executes a regular command.
e.g. while {_a=_a+1; _a<10} do {...}
Since VBS2 NG; a while loop inside a spawn or execVM thread will be interrupted every 3.0ms to let other scripts run. It will resume on the next simulation step. Also in VBS2 and VBS2 NG a while loop will terminate without warning after 10,000 iterations if started from within a call statement.
Interrupted spawned while example: ok = [] spawn { _j = 0 ; while {_j <880} do {_j =_j +1}}; Prematurely terminated called while example: ok = 0 call { j = 0 ; while {j <800000} do {j =j +1}}; result is j = 10000