Making a NABU multiplayer game
Any NABU program can go online. Your program talks only to its own NABU Internet Adapter; the adapter keeps a connection to nabu.ca, buffers everything, and answers almost every call instantly from memory. You never deal with TCP, timeouts or reconnects.
1. Get a game ID
- Sign in and open My multiplayer games. Choose Register a game.
- Save the form. Your game ID (a number from 1 to 65534) is shown at the top. Put it in your program:
#define BIN_TYPE BIN_HOMEBREW
#include "NABULIB/NABU-LIB.h"
#include "NABULIB/RetroNET-Multiplayer.h"
#define MP_GAME_ID 42
Every hosted session shows up on the public lobby and is announced in the Discord #multiplayer-games channel. To keep a session to yourself, create it with MP_FLAG_PRIVATE (friends join by session ID); MP_FLAG_NO_DISCORD lists it on the lobby without the Discord post. You can also switch Discord announcements off for the whole game on its edit page.
While debugging, tick dev mode on the game's edit page. It switches on the packet inspector and lifts the hosting limits. New games start with it off. A published game can't be in dev mode: publishing switches it off, and you need to unpublish to turn it back on.
The multiplayer entry is just a link. Once your program is uploaded as a NABU channel or Cloud CP/M program (or both), pick it on the entry's edit page. The game page takes its description, icon, video and screenshots from the linked program, and the entry can only be made public after a published program is linked.
Every program needs its own registered game ID, even while you are experimenting. There is no shared test ID: if everyone used the same one, unrelated programs would end up in each other's sessions.
2. The session flow
MpStatus st;
uint16_t sessionId;
uint8_t myId;
if (!mp_isSupported()) {
vdp_print("Please update your NABU Internet Adapter");
return;
}
if (mp_init(MP_GAME_ID) != MP_OK)
return;
// Host...
mp_create(4, MP_FLAG_NONE, NULL, "DJ", "DJ's game", "Come play!", &sessionId, &myId);
// ...or browse and join
uint8_t count;
MpSessionInfo info;
mp_listSessions(MP_LIST_JOINABLE, &count);
for (uint8_t i = 0; i < count; i++)
mp_getSessionInfo(i, &info); // show info.name, info.playerCount / info.maxPlayers
mp_join(info.sessionId, NULL, "LEO", &myId);
// Lobby: poll until the host starts
do
mp_status(&st);
while (st.state == MP_STATE_LOBBY);
Always call mp_isSupported() first. An older adapter does not understand the multiplayer command, and the NABU would wait forever for an answer.
The host calls mp_start(). Everyone polling mp_status() sees MP_STATE_IN_GAME. mp_getStartInfo() then gives every machine the same random seed, so you can build the same level or shuffle the same deck everywhere.
3. Sending and receiving
// send my position to everyone else on channel 1 (latest-only)
mp_send(MP_TARGET_OTHERS, 1, sizeof(myShip), (uint8_t *)&myShip);
// read everything waiting
uint8_t from, channel;
uint16_t len;
while ((len = mp_recv(buf, sizeof(buf), &from, &channel)) != 0)
handlePacket(from, channel, buf, len);
mp_recv returns 0 when nothing is waiting, otherwise the packet length. A packet longer than your buffer is cut to fit, so you can never overrun memory.
| Target | Who gets it |
|---|---|
0x00–0x0F |
one player, by player ID |
MP_TARGET_OTHERS (0xFF) |
everyone except me |
MP_TARGET_ALL (0xFE) |
everyone including me, in one order the server picks |
MP_TARGET_HOST (0xFD) |
the host |
| Channel | Behaviour |
|---|---|
0 |
Reliable and in order. Use it for events that must not be lost: a hit, a move, a score. |
1–255 |
Latest-only. A new packet replaces an unread one from the same player on the same channel. Use it for positions: a slow NABU always reads the newest one instead of a backlog. |
4. Pick a netcode pattern
Everyone owns their own thing (Pong, tanks, racing). Each NABU simulates only its own sprite and sends its state on a latest-only channel. Others draw what they receive. Hits and scores go on channel 0.
Host decides (co-op, shared enemies). Players send inputs to MP_TARGET_HOST. The host runs the game and broadcasts snapshots on a latest-only channel.
Ordered moves (chess, cards, strategy, turn-based RPGs). Send every move to MP_TARGET_ALL on channel 0, and apply moves only when they come back from the server, starting from the shared seed. Every machine applies the same moves in the same order, so they stay identical without ever sending the whole state. The tic-tac-toe example does exactly this.
5. Mind the serial budget
The NABU talks to its adapter at about 11,000 bytes per second, roughly 186 bytes per 60 Hz frame, in both directions combined. That is the real limit for action games, more than the internet.
| Call | Approximate cost |
|---|---|
mp_status |
1.3 ms |
mp_recv, nothing waiting |
0.5 ms |
mp_send of 16 bytes |
2.1 ms |
mp_recv of 16 bytes |
2.1 ms |
mp_recv of 1024 bytes |
92 ms |
Four players each sending a 16-byte update ten times a second costs about 8% of the machine. Keep action packets under 64 bytes, and use mp_exchange() to send and receive in one call.
6. Everything else you get
- Lobby helpers:
mp_setReady, 8 bytes of per-player data (mp_setPlayerData: colour, team, character) and 16 bytes of session data the host sets and browsers see before joining (mp_setSessionData: map, mode, difficulty). - Session flags for
mp_create:MP_FLAG_PRIVATE(unlisted; friends join by session ID),MP_FLAG_LATE_JOIN,MP_FLAG_HOST_MIGRATION,MP_FLAG_AUTO_START,MP_FLAG_NO_DISCORD,MP_FLAG_WEB_CHAT(people on this site can chat in),MP_FLAG_SHARED_DATA. - Host controls:
mp_kick,mp_setLocked,mp_setHost, andmp_end(winnerMask, MP_END_FLAG_TO_LOBBY)for a rematch with the same players. - Quick join:
mp_quickJoinputs the player in an open session, or opens one. - Chat:
mp_chatSend/mp_chatRecv, separate from game packets. Chat shows on the live session page here. - Time:
mp_time()is milliseconds of session time, the same on every machine.mp_ping()is your round trip to the server. - Website extras:
mp_setScoreshows live scores on the session page;mp_setStatusText(host) shows a line such as "Round 3 of 5". - Leaderboards:
mp_scoreSubmit, thenmp_scoreCount+mp_scoreGetfor the top 100. Define board titles on your game's edit page. - Cloud saves:
mp_storeWrite/mp_storeRead, 4 slots of 1 KB per player per game. - Robustness: if a player's internet drops for under 20 seconds they keep their seat and miss no packets (
MP_STATUS_RECONNECTINGis set meanwhile). A NABU that stops polling for 30 seconds, or is reset, leaves automatically.
7. Testing
- One NABU? Open your session on this site (from your game's edit page) and press Add echo bot. The bot joins as a player and sends every packet you send it straight back.
- Packet inspector: on the same page, every packet in a dev-mode session is shown as hex as it passes through the server.
- Emulators: run two copies of MAME, each with its own Internet Adapter (different TCP ports), and play against yourself.
- Local server: developers running their own copy of the site can set the adapter's Multiplayer > Server URL to
ws://localhost:PORT/mp/ws.
8. Limits
Up to 16 players per session, packets of 1 to 1024 bytes, 60 packets and 8 KB per second per player, one chat line per second. A lobby with no activity closes after 15 minutes, a game with no packets after an hour.
Reference
RetroNET-Multiplayer.hin NABU-LIB: every function, struct and constant, with comments.retronetMultiplayerManual.mdin the Internet Adapter repository: the byte-level protocol, for writing your own bindings (assembly, BASIC, Pascal...).- Examples:
C Programs/Multiplayer/chat(the smallest possible multiplayer program) andC Programs/Multiplayer/tictactoe.