Skip to main content

Local Variables

A variable is a named location in memory that stores a value. A local variable is a variable that is declared within a function or block and is only accessible within that function or block.

Mutable variables

Variables declared with let are mutable, which means they can be assigned to after they are declared. Mutable variables must begin with a lowercase letter.

pub fn Example() {
let x = 123
x = 456
Transmission { "x=" + x } // Outputs: x=456
}

Constant variables

Variables declared with const cannot be assigned to after they are declared. Constant variables may begin with an uppercase or lowercase letter.

pub fn Example() {
const NumBots = 7
NumBots = 8 // Compilation error: Cannot assign to a constant variable
}

Context variables

Context variables get passed into functions automatically and are declared with a use prefix. Context variables must begin with a lowercase letter.

use color=#ff8800, density=15 // Context variables

ExampleFunction1("Bob", radius=1.5)
// This is identical to: PolygonSprite("Bob", radius=1.5, color=#ff8800, density=15)

// This is a function that takes `name` directly and `radius`, `color` and `density` from context
fn ExampleFunction1(name, [radius, color, density]) {
// ...
}

See the Context section for more information.