Skip to content
AEOWUN
Docs Menu

Functions

Write code once. Call it when you need it.

Declare a function

Declare a function with fn. Give it a name, inputs, and a body.

fn add(a: number, b: number): number {
    return a + b
}

Here, a and b are the inputs. The : number after the parentheses describes the return value.

Call a function

Use the function name followed by parentheses.

const total = add(10, 20)
debug.log(total)

This prints 30. Writing add without parentheses refers to the function. It does not call it.

Parameters and return values

Parameters are local to the function. Pass one value for each required parameter.

return sends a value to the caller and ends the function. A function that ends without returning a value returns nil.

Types can be left out:

fn double(value) {
    return value * 2
}
debug.log(double(5))

Functions as values

You can store a function in a variable and call it later.

const double = fn(value) {
    return value * 2
}
debug.log(double(5))

Closures

A function can keep access to variables from where it was created. This is called a closure.

fn make_counter() {
    count: number = 0
    return fn() {
        count += 1
        return count
    }
}
const next = make_counter()
debug.log(next())
debug.log(next())

This prints 1, then 2. Each call to make_counter() creates its own counter.

Callbacks

A callback is a function that another system calls for you. For a button example, see UI. For contact callbacks, see Events.

Functions in Modules

A Module lets another script call your functions. See Actors and Modules. A function may also pause with wait().

[BETA] marks features newer than the current public release. Current release: 0.8.0.