Full passive turn-based multiplayer example somewhere?


i learn best when I have running examples that I can edit and learn from.
SO … is there a complete running example for passive turn-based multiplayer, where the players don’t have to be online at the same time? That would be a great help for me.

I guess not. Too bad.

Hi @kragil welcome :wave:

As you’ve noticed there’s no official tutorial on how to implement a passive (often called asynchronous) multiplayer approach like you see in games like Words With Friends. This is not because its complicated to implement but it’s actually quite easy to achieve. The steps to follow are:

  • Use the storage engine features in the server with write permissions set to 0 (zero) so that only server-side functions can change the state of the storage object.
  • Use a couple of RPCs to implement the input checks and apply the state changes to represent the transition between turns in the asynchronous multiplayer match.
  • Use an in-app notification (with optional push notifications) to indicate when the match has ended and rewards are distributed).
  • Track which matches a player has active in a second storage object that is their “active_matches” object.

I’ve made a note for us to add this to the official documentation in the future but in the meantime you could ask any questions you have as you implement it for your game.

1 Like

OK, thanks a lot for the answer.
Just be clear:
So I can’t use the Matchmaker, the Authoritative Multiplayer features and the Match Handler?
I should implement them myself? If so could I maybe misuse the tournament feature for some parts?
Thanks again.

So I can’t use the Matchmaker, the Authoritative Multiplayer features and the Match Handler?

@kragil That’s not quite true though it depends on the multiplayer experience you want to offer. You can definitely use the authoritative multiplayer engine (match handlers) with a “suspend/resume” approach to progress the game state but that would be more useful if you want to offer a hybrid of the asynchronous multiplayer model and the active multiplayer model.

For example if you want players who are both online to feel the “active/rapid” experience of their multiplayer match but also allow players to come back if they have to go offline and resume their turn later. As always with multiplayer the devil is in the details.

Do you have a reference example for the type of multiplayer game you want to create?

@novabyte Id like to do something like Draw Something, where you just start a match, do something and then close the app and somebody else can pick up from there and then the next time you log in somebody did something and it is your turn again.

@kragil You can look for example here:

I am using nk.StorageWrite with custom json data

github /challenge-league/nakama-plugin-challenge-league/blob/a9406e680efd230069f9aa4c66f4b9febb099c36/match_state.go#L168

github /challenge-league/nakama-plugin-challenge-league/blob/master/match.go

@dkozlov Hey cool! Thank you. I will have a look, although I am not a Go programmer, but I guess I will manage :wink:

@kragil I would definitely use a suspend/resume pattern with the Draw Something style of game. Have each match handler track its state and when the user who’s turn it is leaves the match handler you can suspend the match so that it can be resumed when they come back.

You’ll need to bookkeep a little things like what active matches each user has that they’re part of and store the match state as a serialized object to JSON. The whole pattern is probably around 200-300 lines of code to implement.

@novabyte Now I am confused. I thought I can’t match players if they are not online at the same time and therefor I cannot use the match handler. Is that not true?