Recomedation to use maps for in game players

I want to use a concurrent map, let say

when player joined mach
the map will be updated like this

 InGamePlayers[userID] = "matchID3ey72872y7he7823he238eh238e.nakama"

when player exit or game over then i will delete him

I am doing this to make sure player can not call any RPC or change profile or join another game or do anything while he is in game

as my game logic if the player leave the game due to bad network or died battery the player can join again.
when player opens the game it will call RPC function to check if player active in game and proceed to resume.

if val, ok := InGamePlayers[userID] ; ok {
    //send match Id to the player to proceed to resume.
}

Do you recommend this approach or there is a better way.

You’re on the right track, but I’d recommend a small tweak. The solution to track which players are in a match is indeed correct, but you should not store it in global variables/maps if you can avoid it.

You haven’t specified, but since you’re using the Go runtime I’ll also assume you’re using authoritative multiplayer already. It’s best to use the match label for this data - when a user joins the match place their ID into the match label, when they leave remove it. This will allow you to search the match labels using a match listing query to find which match the user is in, if any.

If your match label looks like this:

{"user_ids":["user1", "user2"]}

You can use this corresponding match listing query, for example:

+label.user_ids:user1

This will only return matches where “user1” is part of the users list. Hope this helps!

2 Likes

@zyro really good solution, this was totally out of my radar