Skip to content

Split parts

On a host, server and client callbacks share one script instance, so a ShapeClass member set on one side and read on the other works in singleplayer but breaks for remote clients. A split class rules that out.

You write a ServerShapeClass and a ClientShapeClass, usually in separate files. Each half is its own C++ object and only has its own side's callbacks. Their names have no server_ or client_ prefix, because the half already says the side: ClientShapeClass::onInteract is client_onInteract.

The halves talk through typed messages: plain structs in a shared header.

// Lamp.hpp, shared by both halves
struct Toggle {}; // client to server
struct State { bool on = false; atl::String by; }; // server to clients
Lamp.server.cpp
class LampServer : public ServerShapeClass {
public:
void onCreate() override {
if (auto saved = loadState<State>()) state_ = *saved;
network().sync(state_);
}
void onToggle(const Toggle &, Player sender) {
state_ = {!state_.on, sender.name()};
interactable().setActive(state_.on);
network().sync(state_);
saveState(state_);
}
private:
State state_;
};
ATL_SERVER_CLASS(Lamp, LampServer).on<&LampServer::onToggle>();
Lamp.client.cpp
class LampClient : public ClientShapeClass {
public:
bool canInteract(Character) override {
gui::interaction("Use", state_.on ? "Turn off" : "Turn on");
return true;
}
void onInteract(Character, bool pressed) override {
if (pressed) network().send(Toggle{});
}
void onState(const State &state) { state_ = state; }
private:
State state_;
};
ATL_CLIENT_CLASS(Lamp, LampClient).on<&LampClient::onState>();

Both halves register into one game-side class, so the Lua file is still scriptClass("Lamp"). Each half is created on its side's first callback, so a dedicated server never creates a client half. A class has at most one half of each kind.

A tidy layout is a folder per part, each with a .server.cpp, a .client.cpp and the protocol header.

Half Sends Receives
client network().send(msg) to the server .on<&C::handler>() with void handler(const Msg &)
server network().send(msg) to every client, sendTo(player, msg), sync(state, channel) .on<&S::handler>() with void handler(const Msg &) or (const Msg &, Player sender)

sync is the typed form of setClientData. The game keeps the latest value, one per channel, and sends it to players who join later.

saveState(state) and loadState<T>() store a struct in the part's storage the same way.

The raw calls (sendToServer("name", value), .method<>(), onClientDataUpdate, storage()) still work next to them.

bool, integers, float, double, enums, atl::String, atl::Vector<T>, atl::Optional<T>, Vec3, Quat, Color, Uuid, nested plain structs of up to 15 fields, game objects such as Shape and Player, and atl::Value for anything else.

Fields are encoded by position, so no field list or macro is needed. That works because both halves are built from the same header. To carry another type, specialize atl::wire::Codec<T>.

The compiler rejects:

  • a client callback on the server half, or the other way round
  • a prefixed name like client_onInteract, which would never run
  • a half passed to ATL_CLASS
  • a message that isn't a plain struct, or has a field the encoder can't carry
  • a client handler that asks for a sender

Nothing a remote player sends can trap your mod. A message that arrives on the wrong side, has no handler, is malformed, or comes from a build with a different definition of the struct is logged and dropped. The log names the type:

the client half has no handler for lamp::State
lamp::State arrived with a different definition than this build's

A saved state that doesn't match the current struct is ignored with a warning, and the part starts fresh.

Networking covers the wire format and the size limits.