net

Multiplayer: host, join, messages, the automatic sync, other players.

Read Multiplayer and networking first. Functions that connect or send need the player's permission for your mod (net.request); without it they return nil, "network use is not allowed ...".

Fields

FieldTypeAccess
net.defaultPortintegerread27015

Functions

# net.allowed() → boolean

Whether the player allowed your mod to use the network.

Returns
boolean

# net.request(reason?) → boolean

Asks the player for permission (once; the answer is remembered and changeable in F1 → Network). net_consent tells the answer.

ParameterType
reason optionalstringshown to the player
Returns
booleantrue when already allowed
if not net.request("for co-op hunts") then print("waiting for the player") end

# net.host(port?, name?, maxPlayers?) → boolean|nil

Hosts a session.

ParameterType
port optionalintegerUDP port (default 27015)
name optionalstringyour player name (default: the Network tab's)
maxPlayers optionalintegerdefault 8
Returns
boolean|niltrue, or nil and an error

# net.join(address, port?, name?) → boolean|nil

Joins a session. Asynchronous: net_joined for peer 0 means you are in; net_left for peer 0 carries the reason it failed.

ParameterType
addressstringIPv4 address or host name
port optionalintegerdefault 27015
name optionalstringyour player name
Returns
boolean|niltrue when the attempt started, or nil and an error

# net.leave()

Leaves the session (the host ends it for everyone).

# net.kick(peer, reason?)

The host removes a player.

ParameterType
peerinteger
reason optionalstring

# net.active() → boolean

Hosting, joining or joined.

Returns
boolean

# net.connected() → boolean

Hosting, or accepted by the host.

Returns
boolean

# net.isHost() → boolean

Whether this game hosts.

Returns
boolean

# net.self() → integer

Your player id.

Returns
integeryour id (0 = host), -1 outside a session

# net.name() → string

Your player name.

Returns
string

# net.port() → integer

The UDP port in use.

Returns
integer

# net.error() → string

The last error.

Returns
string

# net.peers() → table

Everyone in the session, you included.

Returns
tablea list of {id, name, ping, host, self}

# net.stats(peer?) → table

Traffic statistics.

ParameterType
peer optionalintegerone player (default: all traffic)
Returns
table{ping, bytesSent, bytesReceived, packetsSent, packetsReceived, resends, sendRate, receiveRate}

# net.send(to, topic, data, reliable?) → boolean|nil

Sends a message.

ParameterType
tointeger|nila player id, nil = everyone else
topicstringyour message name, e.g. "mymod:score"
dataanystring, number, boolean or table (as JSON)
reliable optionalbooleandefault true; false for small, frequent state
Returns
boolean|niltrue, or nil and an error
net.send(nil, "arena:score", {red = 3, blue = 1})

# net.on(topic, fn, priority?) → integer

Listens for messages of one topic.

ParameterType
topicstring
fnfunctionfn(e): e.from, e.topic, e.data, e.reliable
priority optionalinteger
Returns
integerlistener id (events.off)
net.on("arena:score", function(e) score = e.data end)

# net.discover(port?) → boolean|nil

Asks the local network for games (answers in net.lanGames a moment later).

ParameterType
port optionalintegerdefault 27015
Returns
boolean|nil

# net.lanGames() → table

Games found on the local network.

Returns
tablea list of {address, port, name, players, maxPlayers}

# net.sync(on?) → boolean

The automatic sync of creatures, players and hits (switch it on in every game).

ParameterType
on optionalboolean
Returns
booleanwhether the automatic sync is on

# net.syncSettings(settings?) → table

Tunes the automatic sync.

ParameterType
settings optionaltable{rate, nearRadius, sweepSeconds, hits, hunters}
Returns
tablethe current settings

# net.huntRemotes(on?) → boolean

Whether creatures notice and attack the other players too (default on).

ParameterType
on optionalboolean
Returns
boolean

# net.mirroring() → boolean

Whether this game shows the host's creatures.

Returns
boolean

# net.standIn(peer) → Creature|nil

The figure of another player in your game.

ParameterType
peerinteger
Returns
Creature|nil

# net.standInPeer(c) → integer|nil

The player a figure stands for (nil: a normal creature).

ParameterType
cCreature
Returns
integer|nil

# net.remoteHunter(peer) → table|nil

Another player's latest state.

ParameterType
peerinteger
Returns
table|nil{x, y, z, yaw, pitch, weapon, health, inHunt, hidden}

# net.hostIndex(c) → integer|nil

The creature's index in the host's game: the same number in every game.

ParameterType
cCreature
Returns
integer|nil

# net.creature(hostIndex) → Creature|nil

This game's handle for the host's creature.

ParameterType
hostIndexinteger
Returns
Creature|nil

# net.lastHitBy(c) → integer|nil

Host: the player whose shot last hurt it.

ParameterType
cCreature
Returns
integer|nil

# net.packCreatures(filter?) → table

Your own sync: the creatures' state.

ParameterType
filter optionaltable{huntableOnly, aliveOnly, radius}
Returns
tablechunks (strings) to send

# net.applyCreatures(chunk) → integer

Your own sync: follow the host's creatures.

ParameterType
chunkstring
Returns
integercreatures in it, -1 = not applicable

# net.packHunter() → string

Your own sync: your hunter's state.

Returns
string

# net.applyHunter(peer, data) → boolean

Your own sync: show a player's figure.

ParameterType
peerinteger
datastring
Returns
boolean

# net.packHit(c, damage, kill) → string

Your own sync: a hit for the host.

ParameterType
cCreature
damagenumber
killboolean
Returns
string

# net.applyHit(peer, data) → boolean

Host: apply a player's hit.

ParameterType
peerinteger
datastring
Returns
boolean