Skip to content

Lists and strings

Compiles

A list holds any number of values in order. Write one with square brackets:

let empty: list = []
let names: list = ["plaza", "garage", "roof"]

Positions count from 0: in names, "plaza" is at 0 and "roof" at 2.

Write Does
names.len() How many values it holds.
names.get(i) The value at position i.
names.set(i, value) Replace the value at position i.
names.push(value) Add a value to the end.
names.destroy() Free the list when you’re completely done with it.
let mut visitors: list = []
fn remember(name: string) {
visitors.push(name)
let newest: string = visitors.get(visitors.len() - 1)
$debug.print_line("{} visitors, newest {}", [visitors.len(), newest], false)
}
let pads: list = $world.get_all_components("teleport_pad.vscript", "", null, "", 0)
for i in 0..pads.len() {
let pad: Component = pads.get(i)
$debug.print_line("pad on {}", [$entity.get_name($component.get_entity(pad))], false)
}

The $container natives cover the rest: $container.find, $container.insert, $container.remove, $container.clear, $container.shuffle and friends. See the container reference.

Join strings with +, and compare them with ==:

let place: string = "garage"
let title: string = "Welcome to the " + place
let home: bool = place == "garage"

To put numbers or other values into text, use {} placeholders. The print natives fill them directly:

let score: int = 7
$debug.print_line("score is {} after {} rounds", [score, 3], false)

To build a string for use elsewhere, $string_utils.format_string_temp fills placeholders the same way and hands back the text:

let lap: int = 2
let label: string = $string_utils.format_string_temp("Lap {}", [lap], 0)

The $string_utils namespace also has length, contains, compare, substr_temp and more; see the string_utils reference.

For programmers

List methods are sugar for $support.list_* natives with the receiver as first argument; push/set pass false for the trailing copy/insert flag. Call the native directly to control it. Lists are engine-owned values; a list literal passed straight to a native’s list parameter fills the call’s argument list rather than allocating.