Skip to content

Functions

David Cruz edited this page Sep 27, 2023 · 2 revisions

Functions are essentially compile-time macros.

They do not use subroutines, as those don't support parameters or return types.

Here's a basic function which sets everyones health to 0:

function test() {
    let players = TEAM_1.allPlayers()
    players.setHealth(0)
}

If you call it, the compiler will treat it as if you wrote the internal code of the function where you called it.

Arguments

Essentially, this code

function foo(n: number, s: string, b: boolean) {
    // do stuff
}

// Imagine this is inside an event :p
foo(1, "string", true)

Desugars to

// Imagine this is inside an event :p
let n = 1
let s = "string"
let b = true

// do stuff

Returns

Return values are implemented halfway. There's no support for early returns, nor enforcement that a function actually returns.

But it works, this code

function bar() -> number {
    return 55;
}

// Imagine this is inside an event :p
let x = bar();

Desugars to

// Imagine this is inside an event :p
let __returnvalue__ = 55;
let x = __returnvalue__;

Note

Because functions are essentially compile time macros, they are lazily evaluated.

This means your function definition won't cause any errors even if there's something wrong with your code, until you call the function itself.

Clone this wiki locally