Skip to content

Variables

ItsDeltin edited this page Mar 31, 2020 · 5 revisions

Variables

There are a maximum of 128 'whole variables' that can be assigned to the global and player variable sets. OSTW supports internally storing variables as arrays, increasing the variable limit. See extended collection for more information.


Variables are scoped, while in the workshop every variable is global. This makes OSTW more consistent with other programming languages.

globalvar define a;

rule: "Do something"
{
    // You can access 'a' here, but not 'b' or 'c'.
    SmallMessage(AllPlayers(), a);

    define b;
    // After defining 'b', you can access 'a' or 'b' here.

    if (condition)
    {
        define c;
        // 'c' can only be accessed inside the 'if' block. When the block ends, it can no longer be accessed.
    }
}

Rule-level variables

Rule-level variables can be accessed from anywhere in the project. These variables require either the globalvar or playervar attribute, but not both. The root keyword is required to access these variables from inside a class.

// Global variable
globalvar DeathZone[] deathZones;

// Player variable
playervar define lives = 5;

If no target is specified when getting or setting a player variable, the event player is used by default. EventPlayer().variable = 3 is equivalent to variable = 3. Arrays of players work as well, the value will be set for every player in the array.

AllPlayers().isBoss = false;
define newBoss = RandomValueInArray(AllPlayers());
newBoss.isBoss = true;

Variables defined at the rule level and inside blocks can have an ID that will switch their order in the workshop. The following example will give the variable myVar the id 5.

define myVar 5 = EventPlayer();

Variables cannot have an ID and be in the extended collection.

Variables with types

Replace define with the name of your class or struct.

globalvar MyClass classVar = new MyClass();

rule: "DoThing"
{
    classVar.objectMethod();
}

Extended Collection

The extended collection extends the number of variables that can be used in the Overwatch Workshop by 1000, 128 to 1128. Variables defined at the rule level, inside blocks, and parameters can be put in the extended collection. To add a variable to the extended collection, add the ! symbol after defining the variable.

globalvar define myVar! = EventPlayer();

void MyFunction(define parameter!)
{
    define anotherVar!;
}

Variables in the extended collection are internally stored as an array. Setting a variable in the extended collection at an index will need to modify it as a multidimensional array. This can result in a high amount of elements in the workshop output, so it is a good idea to not use the extended collection for variables that are intended to be arrays.

Reserving variables

Pasting OSTW code into an existing project may have conflicting variable names or variable ids.

To reserve variables, add the globalvar or playervar keyword to the top of the script file followed by a list of names and ids.

globalvar {
    // A string will reserve a variable name
    "Variable Name",
    "Another Variable",
    // A number will reserve an ID.
    0,
    1,
    2
}

playervar {
    "Player Variable",
    10,
    13,
    14
}

Setting variables

Variables with constant types (Color, EffectRev, lambdas, etc.) cannot be set to.

Operator Function
v = x Assigns the variable to the evaluated expression.
v += x Adds x to the variable.
v -= x Subtracts x from the variable.
v *= x Multiplies x to the variable.
v /= x Divides x from the variable.
v %= x Divides x from the variable and assigns the remainder.
v ^= x Raises v to the power of x.
v++ Adds 1 to v.
v-- Subtracts 1 from v.

The output of = will use Set Variable (At Index). The rest's output will use Modify Variable (At Index).

Variables can also be set to by using ModifyVariable() or ChaseVariable(). ModifyVariable(v, Operation.AppendToArray, newArrayElement) uses less server load than v = [v, newArrayElement].