Skip to content

Scripting Language

alantr7 edited this page May 29, 2024 · 2 revisions

Main function

Main function is called as soon as the Bot starts.

function main() {
    bot.chat("Hello world!")
}

Variables

Variables can be declared using the var keyword. Variables are dynamically typed. They can also be declared without a value. Variables can be declared outside a function too, which makes them accessible from all functions.

var name = "Alan"

function main() {
    var greeting = "Hello "
    var sentence = greeting + name
}

If statements

Expressions must be surrounded by parentheses. You can use and, or and not operators. If the expression is false, the else/else if block will be executed if present.

function main() {
    var foo = "bar"
    if (foo == "bar") {
        bot.chat("foo is bar")
    } else {
        bot.chat("foo is not bar")
    }
}

Arrays

Arrays can be created using array() function. Default capacity for arrays is 8. You can also specify the capacity as an argument, it is limited to 20 by default.

By default, all elements in the array have a value of null.

var numbers = array()
var numbers2 = array(10)

To access, or set an element in an array, use the square brackets.

numbers[0] = 5
bot.chat(numbers[0])

To get the length of an array, use the length(array) function.

bot.chat(length(numbers))

Dictionaries

Dictionaries are a datatype that can store key-value pairs. They are defined using dict() function.

var locations = dict()

To add a key-value pair to a dictionary, use the square brackets. Key can only be of string type (subject to change). You can also use dict_set(dict, key, value)

locations["home"] = new Location(12, 20, -12)
locations["farm"] = new Location(123, 20, 456)

dict_set(locations, "farm", new Location(123, 20, 456))

To remove a key-value pair from a dictionary, use the dict_unset(dict, key) function.

dict_unset(locations, "farm")

To get the length of a dict, use the length(dict) function.

print(length(locations))

Records

Records are a datatype that can store multiple values. They are defined using record keyword. Properties of the record are defined in the parentheses. Properties are read-only.

Records must be defined outside a function, so in the root of a module.

record Location(x, y, z)

To create an instance of a record, use the new keyword.

var location = new Location(123, 20, 456)

To access a property of a record, use the dot operator.

var x = location.x
var z = location.z

Importing modules

You can import modules using the import keyword. To see all the modules available by default, please read this: Standard Modules

If you'd like to use a different name for an imported module, you can use the as keyword, like so: import bot as b;

import bot

function main() {
    bot.chat("Moving forward.")
    bot.move("forward")
}

Clone this wiki locally