Language Guide
Import System
Required version: Sponge >=1.0.4
Basic Module Import
Import a Custom File
use math;This imports the math.sp file from the same directory:
let result = math.add(5, 3);Import with Alias
Use as to give a module a shorter or custom name:
use mathematics as math;
let result = math.add(5, 3);Module Structure
Create a module by making a .sp file with the functions and variables you want to export:
math.sp
def add(a, b) {
return a + b;
}
def subtract(a, b) {
return a - b;
}
def multiply(a, b) {
return a * b;
}
def divide(a, b) {
if b == 0 {
return null;
}
return a / b;
}main.sp
use math;
print(math.add(10, 5)); # 15
print(math.subtract(10, 5)); # 5
print(math.multiply(4, 3)); # 12
print(math.divide(20, 4)); # 5Importing Specific Items
Import only the functions or variables you need from a module:
use math { add, subtract, multiply };
let result = add(5, 3); # No module prefix needed
let diff = subtract(10, 5);
let product = multiply(4, 2);Built-in Modules
SDL2 Module
The sdl2 module provides bindings to SDL2 (Simple DirectMedia Layer), a cross-platform development library that provides low-level access to audio, keyboard, mouse, and graphics hardware.
use sdl2;
sdl2.init();
sdl2.create_window("Window Title", 1024, 768);Or with an alias:
use sdl2 as sdl;
sdl.init();
sdl.create_window("Window Title", 1024, 768);