How to correctly end an authoritative match in Go?

I’m struggling to end an authoritative match using the GO runtime.

Reading the docs it says:

Authoritative match handlers will only stop when any of the callbacks return a nil state. You can choose to do this at any point during the lifetime of the match, whether or not there are still players connected to it.

I’ve been having problems with the matchmaker returning devices to the previous matches, instead of starting a new one.

I currently run the MatchTerminate function at the end of the MatchLeave event if presences reaches 0.

But Debugging the match loop has shown the the loop continues even after MatchTerminate is called. I assume the way I am returning a “nil state” is the problem.

This is my MatchTerminate function:

func (m *Match) MatchTerminate(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, graceSeconds int) interface{} {
if state.(*MatchState).Debug {
logger.Printf(“match terminate match_id %v tick %v”, ctx.Value(runtime.RUNTIME_CTX_MATCH_ID), tick)
logger.Printf(“match terminate match_id %v grace seconds %v”, ctx.Value(runtime.RUNTIME_CTX_MATCH_ID), graceSeconds)
}

return nil
}

Thanks for any help in advance!

Hi @Waka!

Good question, the contents of your MatchTerminate look correct but you’re not supposed to call it yourself. Like all match callbacks they’re intended to be available to Nakama itself to call when appropriate. If you call MatchTerminate yourself then the server’s match management component is not involved there and won’t know that you intend to end the match - it’s just a function call you’ve made.

Returning nil to end matches only works when the server calls the function, not when you do it yourself. In your case inside MatchLeave instead of calling MatchTerminate just return nil and the server will pick that up.

1 Like

@zyro Ah great. Thanks for your help. So I can perform whatever actions I would like to within the Match**** functions, but just allow Nakama itself to call them using it’s own events system?

Thanks for the help that has cleared things right up!

Waka

That’s right, all match callbacks are intended to be called by Nakama. You just react appropriately inside the functions and the server will handle the rest. :+1: