Jump to content

How should one implement socket multiplayer?


The_dude8080
 Share

Recommended Posts

Hello. I know there are some tutorials out there. Mostly there are html5- no game engine focused. Since I am using Phaser I have some doughts on how to proceed. My game has multiple classes like player classes. The class code is something like this:

var Player = function () {

    sprite...
    physics properties...
    groups... etc...

}

Well this was fine in offline. I think that I should apply an id to every player in the game (associated to unique socket id). Probably add a property ".id" to the Player class. How should I do that? Should I create a new Player for every client that joins. The flow would be something like this then. Client connects to server => I create a new socket id => I send that id to the front game => Create a new class Player with that id in its properties. Would that be ok? And another thing should I create the players with Game.player = new Player(id); (where Game is my state) or not add the players directly to the Game state but create a list of players {} and add each player to the world and to that list?

Sorry if it sounds confusing but I really need help on this.

 

 

Link to comment
Share on other sites

49 minutes ago, drhayes said:

That all sounds like a good first approach to me. I would try exactly that. So people can join your game at any time, no lobby or anything?

I will try that then. Related to that there is something that is also confusing me. Well you need the server to run the game ( game here like the entire game where you load your sprites for all the game states). I can detect when a client joins the server (not a specific match or lobby but the "entire game"). However as the player is not really doing anything multiplayer while he is on the game but on a multiplayer lobby / server he doesn't need to have any id right? I am not sure if I am clear...

Link to comment
Share on other sites

3 hours ago, drhayes said:

That's up to you, I think. I mean, there's definitely a difference between someone logged in and still setting up or just watching vs. someone who's playing your game right now, so yes? But what that means inside the server you are now going to write (!!!), I don't know. But good luck! :)

Thanks! I won't forget to show you when I have something decent ;)

Link to comment
Share on other sites

When the client connects, check if the max limit of players has been reached, if it has kill the connection or put the player in spectator mode.

If not, create the player object on the server, with ID and everything. Send the information to the client, and wait for the client to request a spawnpoint (By pressing spawn/play).

There is no need to create an object if the client is not allowed to play, but if you want spectators or a queue system, create arrays with a simple object like:

Queue:

{ID: socket.id}

Then, when a spot opens, you'll simply create a new player object based on the first entry in the queue array, where you have the socket.id ready for processing.

 

When it comes to adding the object to the world.player object or an array, why not do both?

world.player = new Player(socket.id);

world.player.name = something;

world.players.push(world.player);

world.player = {}; //To clear the object for the next player

 

I just don't hope this could possibly create a race condition if 2 players connect at the same time :P

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...