Skip to content

Search

Tài liệu

Balaur Script

Learn Balaur Script, the engine's typed language, one concept a page: types, functions, structs, classes, enums, modules, async and nodes.

Trên trang này

Balaur Script is the language of game scripts and of the editor itself. A .bs file is statically typed, reads like Rust, and compiles to WebAssembly when it is saved. A running game picks up the new code and keeps its state.

enum Shape { Circle(float), Rect { w: float, h: float }, Empty }

fn area(s: Shape) -> float {
    match s {
        Shape::Circle(r) => r * r * 3.0,
        Shape::Rect { w, h } => w * h,
        Shape::Empty => 0.0,
    }
}

pub fn total() -> float {
    let shapes = [Shape::Circle(2.0), Shape::Rect { w: 2.0, h: 3.5 }, Shape::Empty];
    let sum = 0.0;
    for s in shapes {
        sum += area(s);
    }
    sum
}

// total() == 19.0

How this chapter is laid out

Each page teaches one concept and builds on the pages before it. The pages read in order, and each stands alone when you come back to look something up.

Part What it teaches
Getting started a first script on a node, and how to try code
The basics files, variables, operators, control flow, functions
Types numbers, booleans, strings, lists, maps, optionals, any, vectors, conversions
Structs, classes and enums the types a script declares, and match
Functions in depth function values and &mut parameters
Modules splitting a game across files
Async and await code that waits across frames
Memory what is copied, what is shared, what is freed and when
Errors and traps what the compiler refuses, and what stops a call
Scripts in the engine hooks, exported properties, components, events
Language reference every construct on one page

The script modules reference lists every engine function. The Scripting manual page covers the editor, the debugger and hot reload.

How to read the examples

A comment under an example says what a call answers:

pub fn double(x: int) -> int { x * 2 }

// double(21) == 42

Some calls end in a trap, which stops the call at run time. Others do not compile, and the comment gives the compiler's own message with its line and column:

pub fn half(n: int) -> int { n / 0 }
pub fn broken() -> int { 1 + true }

// error: 2:28: cannot combine int and bool

Trying the examples shows how to run any of them on your own machine.

Getting started Write a first script, attach it to a node, and try the language's examples from the command line.
The basics Files, variables, operators, control flow and functions: what every Balaur Script file is made of.
Types Every type in Balaur Script: numbers, booleans, strings, lists, maps, optionals, any, vectors, and how one becomes another.
Structs, classes and enums The types a script declares itself: structs copied as values, classes shared as objects, enums taken apart with match.
Functions in depth Functions as values, and parameters that write back to the caller's own variable.
Modules Splitting a game across files in Balaur Script: use, pub and internal, how a file is found, constants, types and engine modules.
Async and await Code that waits across frames in Balaur Script: async fn, await, task::seconds, task::wait, where an await may go, and what ends a task.
Memory Memory in Balaur Script: what is copied and what is shared, reference counting with no collector, cycles that leak, and when an object is freed.
Errors and traps What goes wrong in Balaur Script and how each kind shows: compile errors, traps that end a call, and calls that answer nil or false.
Scripts in the engine How the language meets the engine: a script on a node, its hooks, exported properties, components, events and other scripts.
Language reference Every construct of Balaur Script on one page: types, operators, strings, lists, maps, optionals, any, structs, classes, enums, functions, modules and async.