Loops - ItsDeltin/Overwatch-Script-To-Workshop GitHub Wiki

Loops

If you get the Server closed due to excessive server load error, you may have an infinite loop with no waits.

for

The for's block or statement will execute while the expression evaluates to true. After every loop, the iterator will update.

for (define i = 0; i < CountOf(AllPlayers()); i++)
{
    define teamID = RandomInteger(0, 4);

    SmallMessage(AllPlayers()[i], <"team: <0>", teamID + 1>);

    AllPlayers()[i].team = teamID;
}

auto-for

This for variant uses the workshop's For() action rather than a while loop.

When the for runs, the variable will be set to the initial start expression. After each iteration, the step expression is added to the variable. Iteration will stop when the variable exceeds end.

It will have less output elements than a normal for. However, this for variant requires an entire variable. Variables in the extended collection will not work. Additional conditions also do not work, you can only input a number where the variable will stop counting.

// Creates a variable that can only be accessed inside the for.
for (define = start; end; step) { ... }

// Uses an already existing variable.
for (var = start; end; step) { ... }

foreach

foreach will iterate through each value in an array.

define radius = 8;

define locations = [Vector(56.64, 21.00, -67.14), Vector(50.46, 9.15, -92.95), Vector(30.00, 14.00, -77.91), Vector(82.59, 12.68, -88.21)];

foreach (define loc in locations)
{
    CreateEffect(AllPlayers(), Effect.Sphere, Color.Red, loc, radius, EffectRev.VisibleTo);
}

while

The while's block or statement will execute while the expression evaluates to true.

while (scanning)
{
    if (AngleOfVectors(cameraPos, cameraLookingAt, targetPlayer) < 45)
    {
        scanning = false;
        SmallMessage(targetPlayer, "Warning: Detected by camera!");
    }
}
⚠️ **GitHub.com Fallback** ⚠️