BSL Object-Oriented Programming

While the Bonezegei Scripting Language (BSL) is lightweight, it is also fully Turing-complete and supports standard Object-Oriented Programming (OOP) constructs. OOP allows you to bundle data (variables) and behaviors (functions) into highly organized, reusable structures. This is incredibly helpful when your scripts grow in complexity.

BSL Classes & Objects

Class: Defines the structure, variables, and functions that a certain type of data should have.
Object: An active, unique instance of a class. You create an object by "instantiating" a class using the new keyword.

BSL Constructors

When you create a new object, you usually want to set up its initial state (like giving a user a name, or setting a device's default status). This is done using a Constructor.

A constructor is a special built-in method inside your class that automatically runs the exact moment an object is instantiated with the new keyword. It is the perfect place to accept setup parameters.

BSL Properties & Methods

The internal variables and functions that belong to an object have specific names in OOP:
Properties: Variables that live inside an object to store its state (e.g., this.speed , this.name).
Methods: Functions that are bound to the class and dictate what the object can do (e.g., calculateSpeed() ).

In BSL, you access properties and invoke methods on an object using dot notation (e.g., myObject.doSomething() ). You use the this keyword inside the class to refer to the specific instance of the object currently being acted upon.

// Try it Yourself:

var true = 1;
var false = 0; 
// 1. Declaring a Class
class SensorDevice {

    // 2. The Constructor: Initializes state upon creation
    constructor(deviceName, initialValue) {
        // Defining Properties using 'this'
        this.name = deviceName;
        this.value = initialValue;
        this.isActive = true;
    }

    // 3. Methods: Functions bound to the class object
    function updateValue(newValue) {
        if (this.isActive) {
            this.value = newValue;
            print(this.name + " updated to: " + this.value);
        } else {
            print(this.name + " is offline. Cannot update.");
        }
    }

    function toggleStatus() {
        // Flips the boolean state
        this.isActive = !this.isActive; 
        print(this.name + " active status is now: " + this.isActive);
    }

    function getReport() {
        return "Device: " + this.name + " | Value: " + this.value;
    }
}

// --- Using the Class ---

// Instantiating Objects using the 'new' keyword
var tempSensor = new SensorDevice("Temperature", 22.5);
var humiditySensor = new SensorDevice("Humidity", 60);

// Accessing properties and invoking methods
print(tempSensor.getReport()); 

// Updating object state
tempSensor.updateValue(24.0);
humiditySensor.toggleStatus();
humiditySensor.updateValue(65);

Output

Device: Temperature | Value: 22.50
Temperature updated to: 24
Humidity active status is now: 0
Humidity is offline. Cannot update.