New product announcement 🎉 Satori - LiveOps to know your players, deliver features, run experiments and schedule events

Match Runtime Reference

This page lists all functions exposed in the match handler Dispatcher type.

Match runtime

BroadcastMessage

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.

Parameters
NameDefaultDescription
opCode int64 REQUIREDNumeric message op code.
data []byte REQUIREDData as slice of bytes to be sent to the provided presences.
presences []runtime.Presence REQUIREDList of presences (a subset of match participants) to use as message targets, or 'nil' to send to the whole match.
sender runtime.Presence REQUIREDA presence to tag on the message as the sender, or nil.
Returns
NameDescription
error errorAn optional error that may indicate a problem broadcasting data to match participants.
1
2
3
4
5
6
7
func (m *Match) MatchLoop(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, messages []runtime.MatchData) interface{} {
	var opCode int64 = 123
	var data []byte = []byte("test")
	dispatcher.BroadcastMessage(opcodeHello, b, nil, nil) // Broadcast to all match participants.

	return state
}

MatchKick

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
NameDefaultDescription
presences []runtime.Presence REQUIREDA list of match participant presences to remove from the match.
Returns
NameDescription
error errorAn optional error that may indicate a problem kicking the selected match participants.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
func (m *Match) MatchLoop(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, messages []runtime.MatchData) interface{} {
	if tick >= 10 {
		// For example we'll kick everyone that sends a message on or after tick 10.
		for _, message := range messages {
			// Each message implements the runtime.Presence interface to identify its sender.
			dispatcher.MatchKick(message)
		}
	}

	return state
}

MatchLabelUpdate

Sets a new label for the match.

Parameters
NameDefaultDescription
label string REQUIREDNew label to set for the match.
Returns
NameDescription
error errorAn optional error that may indicate a problem applying the new match label.
1
2
3
4
5
6
7
8
func (m *Match) MatchLoop(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, messages []runtime.MatchData) interface{} {
	// As an example update the match label in the 10th match tick.
	if tick == 10 {
		dispatcher.MatchLabelUpdate("Crossed 10 ticks!")
	}

	return state
}