Multiplayer Board in unity using Nakama?

hi Nakama community, I’m a unity game developer and I’m working on creating an online multiplayer board game using Nakama, the game is (backgammon)

for the backend side I used Nakama and everything works perfectly, but creating players, spawning and synchronizing is very hard or there is a very limited resource on the topic on the internet.

I followed the unity-nakama fish game for spawning and synching but the implementation is very complex and puzzling
I really can’t understand the (local player and remote player) spawning system, I’ve tried a lot of multiplayer services before such as (photon, mirror) but it’s my first time seeing two Gameobject being spawned for remote and local player

does anyone know a better way for doing things? a tutorial, a sample project anything that could help me, I will be very much thankful

what I understood from it is that you have to spawn two objects one for yourself and one for the others, and yours has the Sync local script in it and the other has sync Remote, but that doesn’t make any sense to me why do you need two, can’t you just spawn one object and make it send and receive action in one objects?? like the other projects I’ve seen in photon? you just have to specify if it’s local player or not??

Hi @sana2024,

Currently the Nakama Unity SDK does not support GameObject synchronisation/mirroring out of the box. This is something we are discussing internally but there is no planned release date for any such feature at present.

The synchronisation code in Fish Game uses a Local and Remote script to differentiate between the local player and the remote players, but this is just one way of achieving game state synchronisation and is more suited to a game where each player controls a single entity.

In a game like Backgammon you would likely be better off communicating each player’s moves across the network rather than trying to synchronise the position/state of all pieces on the board.

Please forgive me as I’m not familiar with how the game of Backgammon plays. But if I can relate my suggestion to Chess perhaps it will make it a little clearer.

On both clients, you initialise the board with 16 pieces on each side. On Player A’s (white) turn, they move a Pawn to E4. The client therefore sends this move to the server with a MOVE OpCode and a JSON payload.

{
  "move": "e4"
}

Both clients then move the appropriate White Pawn to E4 on the board.

On Player B’s (black) turn, they move their Knight to F6. Again, the opponents client sends this move to the server:

{
  "move": "Nf6"
}

Once again, both clients move the appropriate Black Knight to F4 on the board.

This sort of communication and synchronisation works well for turn-based games such as board games and should hopefully fit your game’s design better than a player spawning and continuous state synchronisation model.

I hope this helps.

Kind regards,

Tom

thanks tom for this well explained reply,
I’m building this game for a client and they wants me to use Nakama, as you know there is a lot of already made board games out there and I don’t want to re-create everything from scratch by myself.
I will find a library or a frame work with all the game rules in it, and I will use Nakama for the matchmaking and data synchronization and this is where I’m stuck since I don’t have a lot of knowledge on Nakama.

as you mentioned I don’t need to spawn every piece in the game, can you please give me more explanation on what do you mean by communicating each players move rather than synchronizing the position???

things that is needed to be synchronized between the two players are like these:

  1. the dice : we will throw two dices and both players need to see the random numbers showing on both dices

  2. each player should get a color and they can only move pieces with that color and then this piece movement should show on both players screen

is this possible with Nakama ?? since Nakama has only one way of synchronizing, send message and receive it???

I followed your fish game tutorial and I created something similar, I spawned to backgammon objects to the scene and I assigned each game object to a player, and if the player is the first in the session they get white pieces and if they’re the second they’ll get the black ones, and for the objects I created two one with synch remote script and one with sync local scrips, but the problem is when the game starts both player automatically synch to one position they stack on top of each other even tho I have two different spawn points??

as you mentioned I don’t need to spawn every piece in the game, can you please give me more explanation on what do you mean by communicating each players move rather than synchronizing the position???

As you’re using the Fish Game as a point of reference I’ll try to explain it with that context in mind. Fish game is a client authoritative realtime fast paced multiplayer shooter. In Fish Game, each player continuously sends their position to the server, which gets relayed to the other clients; in turn those clients update their local game visualisation to display each player in their correct position.

With a turn-based game like Backgammon, where the game is effectively “paused” until the current player takes their turn, there is no need to continuously send the positions of each player’s pieces. Instead, each client should communicate their move/action (e.g. white moves piece A to position X on the board) on their turn as I described previously with the Chess example.

is this possible with Nakama ?? since Nakama has only one way of synchronizing, send message and receive it???

Absolutely. There are two approaches you can take here. Client Authoritative where each player joins a match and on their turn they send their action to the server as an OpCode and JSON payload (again as explained above). In a client authoritative match, the server receives the message and relays it to the other connected clients. For example, Player A sends a message saying they moved White Piece A to Position X, the server receives the message and sends it on to the other clients so they can update their view of the board and know that it is now their turn.

The alternative is Server Authoritative where each client sends their message to the server, the server processes that message and chooses how to handle it. For example, Player A sends the same message as above; the server then checks to see if it is a valid move (or perhaps does something else), it then sends its own message out to the other player to let them know what move Player A made.

I would recommend reading our documentation on the Multiplayer Engine. For an example of a turn based game using Nakama you might want to take a look at our Tic-Tac-Toe Example; although written in JavaScript it may be useful. If you would like to go down the server authoritative route instead then start by taking a look at our Server Runtime Documentation.

I followed your fish game tutorial and I created something similar, I spawned to backgammon objects to the scene and I assigned each game object to a player, and if the player is the first in the session they get white pieces and if they’re the second they’ll get the black ones, and for the objects I created two one with synch remote script and one with sync local scrips, but the problem is when the game starts both player automatically synch to one position they stack on top of each other even tho I have two different spawn points??

Fish Game and Backgammon are very different in terms of gameplay mechanics and I do not believe that the state synchronisation code for a realtime shoot-em-up translates well to the turn-based board game you are trying to create. The Tic-Tac-Toe example I mentioned above would be more fitting here.

thanks a lot, these informations are very important to me, one last question, if I spawn a player game object is it possible if I put both sending and receiving method in the same script for it?? I mean instead of making two prefabs for synch local and synch remote is it possible for each player to have both??

Nakama’s realtime multiplayer engine is client agnostic and while we do provide client SDKs (like the Unity SDK you are using), we do not enforce any particular way of architecting your game’s code. So it really is up to you and your specific requirements when it comes to determining how you want to structure your synchronisation logic.

You mentioned you had been following the Fish Game tutorial. I think it may be beneficial to checkout our other Unity tutorials as well so that you can get a feel for some different ways to approach your game’s state synchronisation code.

Tutorials: Fish Game, Pirate Panic and Ninja Battle.

thanks for your help.

1 Like

just to curiosity, and understand if anyone else uses an approach like mine.
I use mirror with EPIC services transport during the match
and I use nakama for everything else, rankings, users, groups, presence, …

in practice I launch the nakama matchmaker, when all the users of the match return from nakama, I use nakama messaging to send all the connection codes of EPIC sertvices to all users (you can considering this code like the IP), after I change scene, the match scene and at this point I use mirror + EPIC transport.

EPIC services it is used to bypass the router and where is possible have P2P
but it also works if you decide to sharing the IP addresses of all clients, if routers and firewalls are not taken into account.
however EPIC services it is free, free traffic cost during the match.