Your first script
Make a project, read the script it starts with, change it while the game runs, and learn what each line of a Balaur Script class does.
På den här sidan
A script is a .bs file attached to a node in a scene. Start with a new project:
balaur new hello
balaur run hello
The project holds one scene with one node, and one script.
# scenes/main.toml
[[nodes]]
name = "Hello"
script = { source = "scripts/hello.bs" }
// scripts/hello.bs
pub class Hello {
node: Node,
elapsed: float,
}
impl Hello {
fn init() {
log::info("hello from {self.node.name()}");
}
fn update(dt: float) {
self.elapsed += dt;
}
}
What each part does
pub class Hellodeclares the script. Its name follows the file:hello.bsholdsHello, anddoor_bell.bswould holdDoorBell.- Each node the script is attached to gets one object of this class.
node: Nodeis set to the node the object sits on.elapsed: floatis state the object keeps between frames.impl Helloholds the class's methods, andselfis the object.- The engine calls
initonce andupdateonce a frame.dtis the time since the last frame, in seconds. log::infowrites a line to the log."{self.node.name()}"puts a value into the text.
Change it while it runs
Keep the game running, and make the script report once a second:
// scripts/hello.bs
pub class Hello {
node: Node,
elapsed: float,
}
impl Hello {
fn init() {
log::info("hello from {self.node.name()}");
}
fn update(dt: float) {
self.elapsed += dt;
if self.elapsed > 1.0 {
self.elapsed -= 1.0;
log::info("another second");
}
}
}
Save the file. The game keeps running on the new code, and elapsed keeps its
value, because a reload carries each field across by name.
A save that does not compile changes nothing. The error goes to the log with its file, line and column, and the old code keeps running.
Next
Trying the examples shows how to run the code on the other pages. Scripts in the engine goes further with hooks and nodes.