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

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:

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

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.