Standard Library
Reference guide for Walrus built-in modules: Math, IO, Sys, and Core.
Walrus provides a small, high-performance set of built-in modules that are always available in your scripts. Most of these modules are backed by efficient C++ implementations to ensure they don't impact your game's frame rate.
Math Module
The Math module provides standard mathematical operations and random number generation.
| Function | Description |
|---|---|
Math.sqrt(x: float) -> float | Returns the square root of x. |
Math.sin(x: float) -> float | Returns the sine of x (in radians). |
Math.cos(x: float) -> float | Returns the cosine of x (in radians). |
Math.tan(x: float) -> float | Returns the tangent of x (in radians). |
Math.abs(x: float) -> float | Returns the absolute value of x. |
Math.floor(x: float) -> float | Returns the largest integer less than or equal to x. |
Math.ceil(x: float) -> float | Returns the smallest integer greater than or equal to x. |
Math.log(x: float) -> float | Returns the natural logarithm of x. |
Math.exp(x: float) -> float | Returns e raised to the power of x. |
Math.rand() -> float | Returns a random float between 0.0 and 1.0. |
Math.seed(s: int) | Seeds the random number generator for deterministic results. |
let radius = 5.0
let area = 3.14159 * Math.sqrt(radius)
Math.seed(1234)
let chance = Math.rand()IO Module
The IO module handles console interaction and file system access.
Console Functions
| Function | Description |
|---|---|
print(value: any) | Built-in global that prints any value to the console. |
IO.input(prompt: string) -> string | Prints a prompt and waits for user input from the console. |
File Operations: IO.File
You can open files using IO.open(path, mode). The mode string follows standard C conventions ("r", "w", "a", and + or b modifiers).
| Method | Description |
|---|---|
read(count: int) -> string | Reads up to count bytes from the file. |
write(data: string) | Writes the provided string to the file. |
close() | Closes the file handle. |
let file = IO.open("save_data.txt", "w")
file.write("level=5\nhealth=100")
file.close()
let reader = IO.open("save_data.txt", "r")
let content = reader.read(1024)
reader.close()Sys Module
The Sys module provides low-level system utilities and environment access.
| Function | Description |
|---|---|
Sys.sleep(ms: int) | Pauses execution for the specified number of milliseconds. |
Sys.time() -> int | Returns the current system time in milliseconds since the epoch. |
Sys.rand_range(min: int, max: int) -> int | Returns a random integer between min and max (inclusive). |
Sys.getEnv(name: string) -> string | Returns the value of an environment variable, or an empty string if not found. |
Sys.setEnv(name: string, value: string) | Sets the value of an environment variable for the current process. |
let startTime = Sys.time()
Sys.sleep(100)
let damage = Sys.rand_range(10, 20)
let user = Sys.getEnv("USER")Core Module (Interop)
The Core module handles low-level registration and invocation of C++ functions. This is primarily used by host engines to bridge script logic with engine systems.
| Function | Description |
|---|---|
Core.register_function(alias: string, id: int) | Maps a script-visible name to a C++ function ID. |
Core.call_cxx(id: int, ...args) -> any | Directly invokes a registered C++ function by its numeric ID. |
Global Functions
Walrus also provides several built-in global functions that are available without an import.
print(...): Prints any number of arguments to standard output.delete(object, key): Removes a property from a dynamic object.len(container): Returns the length of an array or a string.
Core container types such as Map<K, V> and Tuple<TFirst, TSecond> are part of the language runtime rather than the standard library. They are available without import and are documented in Collections.