2
\$\begingroup\$

I have some experience in creating singleplayer games, but for a new project I want to implement also a multiplayer.

I can imagine what kind of information (position, textureId, ...) i have to send over the network. But hey, I can't figure out how I "could" represent a game waiting room. Which means :

  1. One player creates a new game (What happens here?Does the server code calls a new instance of the game, but does not start the game yet???)

  2. The player, who created the game then either waits for additional players(in maximum 4) or he starts a solo game (Should my game has something like a value for currentPlayers ? Or is this managed by server code?)

  3. Also can you explain what exactly server code means? Which common functions should it handle (in context of creating a game room)?

  4. How can I make sure, that other players can also start new games

As you can see I am a complete beginner to networking, but i do know about TCP and UDP, but the only thing i have done with it was a simple echo server... What am i looking for are simple pseudocodes oder examples, but please explain briefly.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I accomplished what you describe in a previous project using a game state machine, and a network class for both server and client.

Have an abstract base class GameState, and derive a few states from it:

  • MenuState
  • LobbyState
  • PlayingState

And another abstract class Network, and derive a couple of more classes:

  • Server
  • Client

The base Network class contains a list of players.

You will need some kind of StateManager class that handles a stack of states. You begin by pushing the MenuState onto the state stack as the program starts. The user then navigates to the menu option for creating a server or joining a server, and you push the lobby state once the connection is established.

void Game::CreateServer( const string& name )
{
  mNetwork = new Server(name);
  if( mNetwork ){
    StateManager.PushState( new LobbyState() );
    // Some game loop will then use the current state to render the lobby, etc.
  }
}

void Game::JoinServer( const string& hostname )
{
  mNetwork = new Client(hostname);
  if( mNetwork ){
    StateManager.PushState( new LobbyState() );
  }
}

Players joining/leaving must be managed:

void Server::PlayerConnected( PlayerID id )
{
  mPlayers.push_back(id);
  BroadcastPlayerConnected(id);
}

void Client::PlayerConnected( PlayerID id )
{
  // This is called when client receives server broadcast of player connecting.
  mPlayers.push_back(id);
}

The server should also be able to receive chat messages if you choose to support that, and "ready up" messages, character selection, and whatever else you choose for the lobby UI, so the server records the information and then broadcasts it to all connected clients, who then replicate the server's information.

Then when the host wishes to start the game, you simply push the PlayingState, and then pop it off once the game is done to return to the lobby.

void Game::Start()
{
  StateManager.PushState( new PlayingState() );
  // Game loop should handle loading, gameplay etc. using the current state.
  mNetwork->Update( Network::GameStarted ); // Server implementation broadcasts game started event.
}

void Game::Stop()
{
  StateManager.PopState();
  mNetwork->Update( Network::GameEnded );
}

Keep in mind this is just a rudimentary example, and you will certainly have to consider more design work than this. I'm just laying out the groundwork.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .