Send a message to one or more presences. This may be called at any point in the match loop to give match participants information about match state changes. May also be useful inside the match join callback to send initial state to the user on successful join. Note that when broadcasting to multiple presences, if any presence is invalid then the broadcast will not occur.
Parameters
Name
Default
Description
op_codenumberREQUIRED
Numeric message op code.
datastringREQUIRED
Data to be sent to the provided presences, or nil.
presencestableREQUIRED
List of presences (a subset of match participants) to use as message targets, or 'nil' to send to the whole match.
sendernkruntime.PresenceREQUIRED
A presence to tag on the message as the sender, or 'nil'.
Returns
Name
Description
errorerror
An optional error that may indicate a problem broadcasting data to match participants.
1
2
3
4
5
6
7
8
9
10
11
12
localnk=require("nakama")functionmatch_loop(context,dispatcher,tick,state,messages)localopcode=1234localmessage={["hello"]="world"}localencoded=nk.json_encode(message)localpresences=nil-- send to all.localsender=nil-- used if a message should come from a specific user.dispatcher.broadcast_message(opcode,encoded,presences,sender)returnstateend
Removes participants from the match. Call at any point during the match loop to remove participants based on misbehavior or other game-specific rules.
Parameters
Name
Default
Description
presencestableREQUIRED
A list of match participant presences to remove from the match.
Returns
Name
Description
errorerror
An optional error that may indicate a problem kicking the selected match participants.
1
2
3
4
5
6
7
localnk=require("nakama")functionmatch_loop(context,dispatcher,tick,state,messages)-- Assume we store presences in statedispatcher.match_kick(state.presences)returnstateend