Tic-Tac-Toe - Part 3 - Multiplayer, with a Lobby

Favourite 32 favourites
Tutorial written by blackhornetOriginally published on 5th, May 2014 - 2 revisions

Part 3 - Creating a multiplayer tic-tac-toe game, utilizing a lobby server.
This is a continuation of my tic-tac-toe series. We're making a jump here from beginner to advanced, so there's going to be much less hand-holding, and some complex communication mechanisms, although at is base, there is very little communication needed to make a two-player board game, so hopefully it won't be so bad. Please review all of the official multiplayer documentation and tutorials first! I'm not going to re-explain all of the multiplayer plugin details, just the implementation of a lobby/alternating-player game.


C2's multiplayer plugin allows fairly simple connections between two people to play a game, but doesn't deal with people choosing who they play against. This is where the concept of a lobby comes in. Each player starts their game, and rather than immediately playing some random person, they enter a lobby first, where they can see other players and choose to play against someone chosen specifically. I'm not promoting a very complicated mechanism, just one at its simplest. There's lots of ways to expand on how a nice lobby would work - these are just the basics.

As people join the lobby, each peer is automatically updated via Multiplayer: On peer connect, and the peer adds the new player to their own list of players. Once a player picks someone else to play against, they send a message to the host (lobby server) which forwards a message to the other player telling them the details of how to connect to the first player. Both players actually disconnect from the lobby at this point, which removes them from all lists of available players, and they set up their own private connection to each other so that they can play their game. The lobby is out of the loop at this point.


The lobby is a dedicated server/host that must be running at all times. There can only be one, and steps must be taken in the server code and the peer code to enforce this. The lobby itself is just a list showing all of the people who are logged in. Even that isn't necessary, but is convenient for testing. The GUI is just a status field (TextStatus) and a list (PeerList).

The lobby code follows the standard connect/log-in/join-room model of all multiplayer implementations. The only main difference is that when On signalling joined room is triggered, we check that we are the host, and that MyAlias is exactly the name we passed in to Log in with alias. This ensures that there is one, and only one, active lobby. If a second was run, it would have a different alias, so we need to ignore that instance and shut it down.

Open full size image

Next we deal with the lobby leaving the room. Here we just wait 3 second, and if we are still connected, just join the lobby room again. Otherwise, we disconnect from the signalling server.
Open full size image

When we do get a disconnect triggered, we clear our list, wait 5 seconds, and start the connect process all over again.

On an error we log the error. Frankly, I'm not sure what else to do here at the moment.

Now we start dealing with peer events. When a peer connects, we just add them to our GUI list so that we can see what is going on.

When a peer disconnects, we search for that peer in our GUI list and remove it.
Open full size image

Next we just log any message that comes in.

Finally, we get to the meat of the lobby, which isn't all that much. When we get a StartGameWithClient message, the Message text will contain just the PeerID of the X player, an underscore, and the PeerID of the O player. Event 21 just decodes this into its separate parts. Next we see if the peer sending the message has the same ID as the X player, and if so, send the GameRequest message to the O player. Otherwise, the sender must be O, so we need to send the message to the X player. We send the same message we received so that the other player clearly knows who is X and who is O.

The remaining events are just some testing code to test disconnects and recovery.

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.


Wsoukkachang 1,433 rep

Nice! Very useful!

Monday, May 05, 2014 at 4:19:32 AM
qu0y 3,126 rep

So considerate, thanks for being always so helpful!

Monday, May 05, 2014 at 2:29:55 PM
Scofano 2,618 rep

Thanks for sharing.

Monday, May 05, 2014 at 5:50:29 PM
Tedg 9,849 rep

Thanks for share this !)

Monday, May 05, 2014 at 6:53:22 PM
DUTOIT 12.7k rep

Oh, Wow, going to savour this one :) Looks fantastic.

Monday, May 05, 2014 at 7:54:54 PM
AbelaNET 14.7k rep

Thanks for sharing

Tuesday, May 06, 2014 at 8:22:39 AM
beardedeagle 2,015 rep

Multiplayer is the next thing I have to implement into my game and this will be very helpful to get me there. Thank you!

Monday, July 07, 2014 at 7:08:18 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.