What's the best way to add and query match labels in Go?

So, I have a system where I want to query a match by a label and then create that match if it does not exist.

Something like:

lobbyCode := "123"

// Proper way to query a label?
matches, err := nk.MatchList(ctx, 1, false, fmt.Sprintf("code=%v", lobbyCode), nil, nil, "")
if err != nil {
logger.Error("Failed to list matches when looking up lobby code: %v", err)
return "", err
}

if len(matches) > 0 {
return matches[0].MatchId, nil
}

matchID, err := nk.MatchCreate(ctx, "", map[string]interface{}{})
if err != nil {
logger.Error("Failed to create match with lobby code: %v", err)
return "", err
}

match, err := nk.MatchGet(ctx, matchID)
if err != nil {
logger.Error("Failed to get newly created match: %v", err)
return matchID, err
}

// Here I want to set the code label
//match.label["code"] = lobbyCode

Areas of this code I am unsure about is how to go about querying a specific label in a MatchList call and then setting a label outside of the handler.

Thanks in advanced!

@josephbmanley I’m not sure I understand your use case :thinking:. You cannot set a match label from outside the match handler. The match handler controls its own lifecycle authoritatively so it can update its own label at any point in the handler functions but never from the outside. I’ve so far never seen a use case to set the match label from outside of the match.

Is it possible you want to achieve a “find a match to join or create one if none exists” pattern? Have a look at this code for an example of it:

nakama-project-template/match_rpc.go at master · heroiclabs/nakama-project-template · GitHub

Alternatively let me know in more detail about your game design requirements and I can see if there’s a better solution to solve the use case.

1 Like

Thanks for response!

Yeah, sure our game has been using a lobby code system for multiplayer matchmaking. So if you and your friend both put in the code “example” you’ll connect to the same server and create one if it doesn’t already exist.

My current thought for this right now is just storing these lobby codes directly on the database and querying it for the match id.

1 Like