Skip to content

Script classes

A script class is the C++ side of a part, tool or other scripted object. Derive from a base class, override the game callbacks you need, and register the class with ATL_CLASS.

Callbacks keep the game's names: server_onCreate, client_onInteract and so on. They must be public.

Only the callbacks a class overrides exist on the game-side class. The game treats a defined callback as a feature. Defining client_onInteract, for example, makes a part interactable.

Methods the game calls by name, such as network targets, are registered with .method<&T::name>("name"). Their parameters are converted from Lua by type.

ATL_CLASS(Lamp).method<&Lamp::sv_toggle>("sv_toggle");
Base class Used for
ShapeClass parts and blocks
ToolClass tools
CharacterClass characters and creatures
HarvestableClass harvestables
UnitClass AI units
PlayerClass the player script
WorldClass worlds
ScriptableObjectClass, ClientScriptableObjectClass scriptable objects
GameClass a custom game's game script, see Custom games
TerrainScript a world's terrain generator
ServerShapeClass, ClientShapeClass the halves of a split part

Damage and collision callbacks take one struct instead of a long argument list. In server_onProjectile(const ProjectileHit &hit), the damage, shooter and mass are hit.damage, hit.shooter and hit.mass.

GameClass binds chat commands to lambdas:

bindChatCommand("/heal", {{"int", "amount", true}}, "Heals you", [](atl::Value a) { });

Custom games usually extend one of the game's own scripts, like Game = class( CreativeTerrainWorld ). Pass that class as the third argument to scriptClass:

Game = mod:scriptClass("Game", fields, CreativeTerrainWorld)

Create and destroy callbacks you don't override fall through to the base class, and other methods are inherited. To run the base class's version of a callback from C++:

callBase("server_onPlayerJoined", player, isNew);

Where the Lua API takes a method name, the C++ API takes a lambda:

world.loadCell(x, y, player, [](int x, int y) { });
character.onAnimation("attack", 0.3, [] { });

For any other function that wants a method name, sm::bindCallback([](atl::Value args) { }) returns one.

Each game-side instance gets its own C++ object. The loader frees it once both the server and client sides of that instance are gone.

On a host, server and client callbacks share one instance. A member set on one side and read on the other works in singleplayer but not for remote clients. Split parts make that mistake impossible.