Walrus/Language

Standard Library

Reference guide for Walrus built-in modules: Math, IO, Sys, and Core.

Updated April 17, 202610 min readWalrus / Stdlib / Math / IO / Sys

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.

FunctionDescription
Math.sqrt(x: float) -> floatReturns the square root of x.
Math.sin(x: float) -> floatReturns the sine of x (in radians).
Math.cos(x: float) -> floatReturns the cosine of x (in radians).
Math.tan(x: float) -> floatReturns the tangent of x (in radians).
Math.abs(x: float) -> floatReturns the absolute value of x.
Math.floor(x: float) -> floatReturns the largest integer less than or equal to x.
Math.ceil(x: float) -> floatReturns the smallest integer greater than or equal to x.
Math.log(x: float) -> floatReturns the natural logarithm of x.
Math.exp(x: float) -> floatReturns e raised to the power of x.
Math.rand() -> floatReturns a random float between 0.0 and 1.0.
Math.seed(s: int)Seeds the random number generator for deterministic results.
Walrus
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

FunctionDescription
print(value: any)Built-in global that prints any value to the console.
IO.input(prompt: string) -> stringPrints 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).

MethodDescription
read(count: int) -> stringReads up to count bytes from the file.
write(data: string)Writes the provided string to the file.
close()Closes the file handle.
Walrus
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.

FunctionDescription
Sys.sleep(ms: int)Pauses execution for the specified number of milliseconds.
Sys.time() -> intReturns the current system time in milliseconds since the epoch.
Sys.rand_range(min: int, max: int) -> intReturns a random integer between min and max (inclusive).
Sys.getEnv(name: string) -> stringReturns 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.
Walrus
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.

FunctionDescription
Core.register_function(alias: string, id: int)Maps a script-visible name to a C++ function ID.
Core.call_cxx(id: int, ...args) -> anyDirectly 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.