Skip to content

Reuse: join and import

There are two ways to share code between scripts. A class is a script saved in the class editor. You can add it to an object with join, or make separate objects from it with import.

join("Class") adds the class to the current object, and obj.join("Class") adds it to another object (an NPC, a database NPC, a weapon, a player, or a TStaticVar).

  • The functions of the class become functions of the object. The object’s own functions come first, then the joined classes in the order they were joined: when two classes have a function of the same name, the class joined first wins.
  • The statements outside functions in the class run in the context of the object, and then the class’s onCreated runs with this set to the object. When the object joins the class among its own statements outside functions, the class waits until all of those statements have run, then the classes run in join order, then the object’s onCreated.
  • Joining a class twice does nothing.
  • A class file may start with join("other"); to pull in more classes every time it is joined.
// classes/health_functions.bsc
public function getHealthText() {
this.health = 3;
return "Health: " @ this.health @ " hearts";
}
// npcs/slots.bsc
function onCreated() {
join("health_functions");
echo(this.getHealthText());
}

The compiler cannot see the functions that a class adds, because the join happens at run time. A bare call to getHealthText() in the second file makes the warning W-UNKNOWN-FUNCTION (an error in strict mode). Calling this.getHealthText() avoids it.

obj.leave("Class") removes the functions of the class. The variables the class set stay. obj.isinclass("Class") tells whether it is joined.

A join on the server joins both halves: the server half on the server object, and the client half on the client copy of the same object. For a player, that is the client of that player only. For an NPC, it is the clients of the players in its level.

A join on the client joins only the client half, on that client.

A join on a player does not survive a disconnect. Join player classes in onPlayerLogin every time. Any server script can call the public functions of a class joined to a player through the player object.

Saving a class does not change the objects that already joined it. They keep the old version until they join again. An object can handle the event onJoinedClassUpdated(className) and call this.join(className) to take the new version at once.

import class_name; makes a type named like the class. new TypeName(args) then makes a separate object whose script is that class, and calls the class’s public function with the same name as the type with the arguments. Inside it, this is the new object. From outside, only the public functions can be called (OQ-LANG-28).

import vector2;
function onCreated() {
temp.v = new vector2(3, 4);
echo(temp.v.length());
}

Use join when the class should behave like a part of the object. Use import when you want an object of its own.

Engine version 0.1.0. Built 2026-11-18.