Custom games
A custom game (a mod of type "Custom Game") can be written entirely in C++: the game script,
its worlds and their terrain generators. A custom game built with
atl-build looks like this:
HelloGame/ description.json "type": "Custom Game" Scripts/game.wasm compiled from src/game.cpp; atl-build packs it into Scripts/game.json Scripts/game/Game.lua the game script Scripts/game/World.lua a world class Scripts/game/Terrain.lua that world's terrain scriptThe three Lua files are written by hand, because the game loads them by path. Each one loads
the module from the .json atl-build writes.
The game script
Section titled "The game script"The game script uses gameClass instead of scriptClass:
dofile("$CONTENT_b6e3437f-3460-4ec3-ade6-5e12e20765c5/Scripts/AllTheLanguages/Init.lua")Game = sm.allthelanguages.register(sm.json.open("$CONTENT_DATA/Scripts/game.json")):gameClass("HelloGame", { enableLimitedInventory = false,})class HelloGame : public GameClass {public: void server_onCreate() override { world_ = worlds::create("$CONTENT_DATA/Scripts/game/World.lua", "World"); } void server_onPlayerJoined(Player player, bool isNew) override { if (isNew) world_.loadCell(0, 0, player, [this, player](int, int) { player.setCharacter(characters::create(player, world_, {0, 0, 12})); }); }private: World world_;};ATL_CLASS(HelloGame);gameClass is scriptClass with one addition: instances of the class also drive the
mod-wide code of every registered WASM mod (ATL_MOD's server_onCreate,
server_onFixedUpdate and so on). Normally the loader's runtime tool does that, but a custom
game's script can run before any tool exists, or without one.
Mods are created before the game's own server_onCreate runs. If runtime tools exist as well,
each tick still runs once: the first live script that receives a callback drives it, and
another takes over when that one goes away. Any Lua class can do the same with
sm.allthelanguages.drive(cls).
Worlds
Section titled "Worlds"Worlds are ordinary script classes. The engine's world settings go in the fields table:
World = sm.allthelanguages.register(sm.json.open("$CONTENT_DATA/Scripts/game.json")):scriptClass("HelloWorld", { terrainScript = "$CONTENT_DATA/Scripts/game/Terrain.lua", cellMinX = -4, cellMaxX = 3, cellMinY = -4, cellMaxY = 3, enableSurface = true,})Terrain
Section titled "Terrain"Terrain scripts aren't classes. The engine calls global functions (Init, GetHeightAt and
so on) once per generator thread, and each thread has its own Lua state. Derive from
sm::TerrainScript, override what your terrain provides, and assign the functions that
terrainScript returns:
class RollingHills : public TerrainScript {public: bool Load() override { return true; } double GetHeightAt(double x, double y, int) override { return 4 * sin(x * 0.02); } TerrainColor GetColorAt(double, double, int) override { return {0.25, 0.5, 0.2}; }};ATL_CLASS(RollingHills);local t = sm.allthelanguages.register(sm.json.open("$CONTENT_DATA/Scripts/game.json")):terrainScript("RollingHills")Init, Load, GetHeightAt, GetColorAt = t.Init, t.Load, t.GetHeightAt, t.GetColorAt-- one assignment per engine function the class implementsA function the class doesn't override stays nil, which the engine treats as not defined.
GetHeightAt and GetClutterIdxAt run for every vertex, so once Init has run they take a
faster numeric path that skips the handle table. GetColorAt and GetMaterialAt return
several values and still use the handle path.