Multiplayer and networking
Script mods can connect games: co-op hunts, arenas with friends, PvP, shared events, chat. The game brings the connection (UDP, reliable and unreliable messages, LAN discovery) and an automatic sync that makes every player see the same creatures and each other. A mod decides what the game mode is.
The net table is the whole API; this page explains how to use it.
How a session works
- One session per game. A game either hosts (
net.host) or joins one (net.join). The host is peer 0; joining players get ids 1, 2, ... (up to 8 players by default). - Star. Players talk to the host; a message to another player or to everyone is passed on by the host, so every player can send to every other. The receiver sees who sent it.
- The host's world counts. The host's game runs every creature (AI, fights, deaths). With the automatic sync the other games show the host's creatures; their shots on them hurt the host's creature; creatures hunt every player.
- Each game starts its own hunt on the same map; the sync then replaces the joining players' creatures with the host's. Start the hunt on the same map on every game (a mod can send the map with a message first).
- Connections go to an IP address and a UDP port (default 27015): the same computer, the local network, or over the internet with the port forwarded on the host's router. There is no matchmaking server.
Permission
Network use needs the player's permission, per mod. Until the player allows it, every function that would send or
connect returns nil, "network use is not allowed ...".
if not net.allowed() then
net.request("to play co-op hunts with your friends") -- the game asks the player once
end
on("net_consent", function(e)
if e.mod == mod.id and e.allowed then game.message("Multiplayer ready") end
end)
The answer is remembered; players change it in the mod menu (F1 → Network), where they also set their player name.
Hosting and joining
-- host on the default port, with the player's name from the Network tab
local ok, err = net.host()
if not ok then print("cannot host: " .. err) end
-- join (asynchronous: wait for net_joined of peer 0, or net_left of peer 0 with the reason)
net.join("192.168.1.20") -- port 27015
net.join("friend.example.net", 27016, "Alan")
on("net_joined", function(e) game.message(e.playerName .. " joined") end)
on("net_left", function(e) game.message("player " .. e.peer .. " left: " .. e.reason) end)
net.peers() lists everyone ({id, name, ping, host, self}), net.isHost(), net.self() (your id),
net.leave() ends it (the host's leaving ends the session for all). On a LAN, net.discover() asks for games and
net.lanGames() lists the answers a moment later.
Messages
net.send(to, topic, data, reliable) sends data to one player (to = an id) or everyone (to = nil). The topic
is a name you choose; prefix it with your mod's id. data is a string, a number, a boolean or a table (tables are sent
as JSON). Messages are reliable by default: they arrive once, in order. reliable = false is for state you send
again soon anyway (positions); those may be lost and must be small (about 1 KB).
-- everyone receives "chat:say" messages
net.on("chat:say", function(e)
game.message(e.data.name .. ": " .. e.data.text, 6)
end)
keys.bind("Y", function()
net.send(nil, "chat:say", {name = net.name(), text = "Rex at the lake!"})
end)
A handler of net.on(topic, fn) (or of on("net_message", fn) for every topic) gets e.from (the sender's id),
e.topic, e.data and e.reliable. Your own messages to everyone do not come back to you.
The automatic sync
net.sync(true) -- on every game of the session
With the sync on, the host sends its creatures (the ones near any player many times a second, the whole map every two seconds) and every game sends its hunter. Then:
- the other games show the host's creatures, in the same places, moving and animating the same;
- every player sees the others as a figure (the game's hunter model) where they stand, and hears their shots;
- a joining player's shots hurt the host's creatures;
- creatures notice, chase and attack every player, not only the host. A creature that reaches a joining player
kills that player in their own game (so their god mode, extra lives and
player_deathhandlers still apply).
net.syncSettings{rate = 20, nearRadius = 22000, sweepSeconds = 2, hits = true, hunters = true} tunes it;
net.huntRemotes(false) makes creatures ignore the other players.
Creatures across games
A creature has a different id and index in each game. To talk about a creature in a message, send its host
index: net.hostIndex(c) on any game; net.creature(hostIndex) gives each game its own handle for it.
-- the host marks a boss for everyone
if net.isHost() then
local rex = world.spawnNear("trex", 4000)
net.send(nil, "boss:marked", net.hostIndex(rex))
end
net.on("boss:marked", function(e)
boss = net.creature(e.data)
end)
Other players' figures: net.standIn(peer) is the creature handle of that player's figure in your game,
net.standInPeer(c) the player a figure stands for, and net.remoteHunter(peer) their latest state (x y z yaw pitch
weapon health inHunt hidden). On the host, net.lastHitBy(c) tells which player's shot last hurt a creature.
PvP
A shot that hits another player's figure fires the hit event with that figure as e.creature. The game does not hurt
players by itself; a PvP mod sends the damage:
on("hit", function(e)
local peer = net.standInPeer(e.creature)
if peer then net.send(peer, "pvp:hit", e.damage) end
end)
net.on("pvp:hit", function(e)
local health = player.health() - e.data * 10
if health > 0 then player.setHealth(health) else game.message("Shot by player " .. e.from) end
end)
Your own sync
With net.sync(false) a mod can move the state itself: net.packCreatures{huntableOnly =, aliveOnly =, radius =}
returns chunks (strings) of the creatures' state to send; net.applyCreatures(chunk) makes a game follow them;
net.packHunter() / net.applyHunter(peer, data) show a player's figure; net.packHit(c, damage, kill) /
net.applyHit(peer, data) carry a hit to the host. Useful to send only some creatures, or less often.
Events
| Event | Fields |
|---|---|
net_joined |
peer, playerName (a joining player sees the host first, then everyone already there) |
net_left |
peer, reason (a joining player whose session ended gets it for everyone, the host last) |
net_message |
from, topic, data, reliable (every topic) |
net:<topic> |
the same, for one topic (net.on(topic, fn)) |
net_consent |
mod, allowed (the player answered a net.request, or changed it in the Network tab) |
Ports and firewalls
The host needs its UDP port (27015 unless you pass another) reachable: on the same network nothing is needed besides the system firewall allowing the game; over the internet, forward the UDP port on the host's router to the host's computer. Joining players need nothing.
Limits
- IPv4 only; no NAT traversal (the host needs a reachable or forwarded port); no encryption, and a player can choose any name: do not send secrets.
- Network events are handled once per frame, so a round trip takes at least one frame (about 17 ms at 60 fps). A game that stops responding for 15 seconds is dropped; loading a hunt takes 1-3 seconds.
- Each game starts its own hunt; the map is checked, the time of day and weather are not.
- Creatures shown from the host cannot be driven or fight in the joining games (do that on the host). A joining player's kill gives no trophy and the drop ship does not fetch it.
- The other players' figures stand upright and slide (no walking animation, no weapon in hand, no name tag: a mod can
draw names with
c:screen()anddraw.text). - A creature reacts to the nearest player only.
The protocol (for the curious)
Datagrams of at most 1200 bytes starting with CH and a type (hello, welcome, reject, data, ping, pong, disconnect,
discover, host info). Data datagrams carry acknowledgements and messages: unreliable, reliable (sequence numbers,
resent until acknowledged) or fragments of large reliable messages (up to 8 MB). Players ping every half second; 15
seconds of silence ends a connection. A mod message is [kind][topic length][topic][payload], kind 0 = a string,
1 = JSON.