BSL Standard Library

Instead of writing complex low-level code from scratch, BSL comes pre-packaged with native utilities to handle common tasks like file management, data formatting, and execution timing.

BSL File I/O

File Input/Output (I/O) is essential for any application that needs to save data locally, create logs, or read configurations. BSL supports both procedural and object-oriented approaches to file management.

Procedural File Handling
You can interact directly with files using standalone built-in functions.

//Try it Yourself:

// Create and write to a file
createFile("log.txt");
writeFile("log.txt", "INIT: System online.\n");

// Measure the file size and read its contents
print("Log size: " + fileSize("log.txt") + " bytes");
print("Contents:\n" + readFile("log.txt"));

Object-Oriented File Handling
For cleaner code when managing multiple files, you can open a file as an object.

// Try it Yourself:

var myFile = openFile("log.txt");
myFile.append("LOG: Task complete.\n");

print("Updated Contents:\n" + myFile.getData());

BSL JSON Handling

JSON (JavaScript Object Notation) is the standard format for exchanging data across the web and between systems. BSL makes it easy to convert between BSL objects and JSON strings.

stringifyJSON: Converts a BSL object or array into a JSON-formatted string.
parseJSON: Converts a JSON string back into a usable BSL object.
loadJSON: Reads a file directly from your system and parses its JSON content in one step.

// 1. Stringifying Data
/* Inside player.json
{
    "name": "Hero",
    "health": 100
}
*/
var playerConfig = loadJSON("player.json");
var jsonString = stringifyJSON(playerConfig);
print("Stringified: " + jsonString);

// 2. Parsing Data
var rawString = "{\"score\": 500, \"active\": true}";
var parsedData = parseJSON(rawString);
print("Parsed Score: " + parsedData.score);

Output

Stringified: {"health":100,"name":"Hero"}
Parsed Score: 500

BSL Timing & Execution

Sometimes you need to control the flow of time in your script. Whether you are creating a delay between operations or measuring how long a piece of code takes to run.

delay(ms): Pauses the execution of the script for a specified number of milliseconds.
millis(): Returns the number of milliseconds that have passed since the script started running.

// Try it Yourself:

print("Starting calculation...");
var startTime = millis();

// Pause the script for half a second (500 milliseconds)
delay(500); 

var endTime = millis();
var timeTaken = endTime - startTime;

print("Calculation finished in " + timeTaken + " ms.");

BSL Memory / Metadata

BSL provides built-in utilities to inspect your data structures at runtime. The most commonly used tool is sizeof(). While you may have seen it used to get the length of an Array, it is a versatile function that can measure the size of various data structures and objects in memory.

// Try it Yourself:

var activeUsers = ["Alice", "Bob", "Charlie"];
var emptySlots = new Array(10);

print("Active Users count: " + sizeof(activeUsers));
print("Empty Slots count: " + sizeof(emptySlots));

Output

Active Users count: 3
Empty Slots count: 10