Part 2: BasicsNow that you are able to use those classes, let me explain some things:
Socket:A socket is basically a class that handles package sending and receiving to other machines within your local network, we have two types of sending/receivig, UDP and TCP, for games UDP is highly recomended since is much faster for large packages and great quantities of information (that's our case since we should be sending for every player our position, and receiving from every player, their position)
Sockets are best worked by 2 components, a Server and a Client. A server receives all players information, and share all the information with every player. A client simply sends your information to the server, that sends that information to other players (clients). On a local area network (LAN) you always have the Host (Server) and the othe players (Clients), on big games that have massive multiplayers (MMORPGS for example) every player is a client, and the Server is probably on some massive computer server located in the headquarters of the company who develops the game.
Thread:Threads are made to run parallel codes at the same time other code from your game is already running, we are using this to receive server's information, and to server to receive player's information. When you run your game, there is already a thread running, the Main Thread, if we run the code to receive sockets information from other pc, the current Thread
stops until it receives some information, if we put this code on our Main Thread, the game simply stop, and only continue after we receive some information, so we need our own thread so the game can still run even if our network thread is stopped
Idealization:Ok, so, how exacly do we use those things for make our game,
Multiplayer?
Well, here is some basic concept for Client and Server, and how they will work on our game:
Client:
Code:
void onUpdate(){
//Do whatever we need to do
String information = myIp + "-" + transform.pos.X + "-" + transform.pos.Y //We condense our player's information to a string that can be read by the Server, in this case, our IP, and //our position X and Y
byte[] condensed = information.transformToByteArray(); //Not actual function, is just to demonstrate what will we do
socket.send(condensed, serverIP, port) //serverIP is the server's IP, something like //192.168.0.1 and wich port we are using to send those informations
}
void threadMethod(){ //This is our thread method, this will be running parallel to other //threads in the game, we will use this to receive server's information
while(true){//a loop so we can always be receiving new information
byte[] info = socket.receive(serverIP, port);// Again, not real methods, just using to //explain what we are doing
String data = into.toString()//dont know if this work (probably no)
Player p = players.get(data.split("-")[0]) //"players" is a hashmap wich contains all //players, keyed by its IP so we can retreive the players position by simply searching its IP
p.position.x = parseInt(data.split("-")[1]);
p.position.y = parseInt(data.split("-")[2]);
//If you want to retreive more information about the players, for example name, HP,MP, //etc you just need to send those informations to the server, by the fact that every client is //running this same code, if you send your name, then it means that everyone is sending //their name, and you just need to get it from the server
}
}
Server:Code:
HashMap<Players> players;
void onUpdate(){
foreach(Player p in players){
String information = p.IP+ "-" + p.transform.pos.X + "-" + p.transform.pos.Y //For every player, we get their information
byte[] condensed = information.transformToByteArray();
foreach(Player p2 in players){
if(p2 != p){
socket.send(condensed, p2.IP, port) //and send their information to every other player //(that is not itself for obvious reasons)
}
}
}
}
void threadMethod(){
while(true){//same loop of client so we can always receive new information
byte[] info = socket.receive(serverIP, port);
String data = into.toString()
Player p = players.get(data.split("-")[0])
p.position.x = parseInt(data.split("-")[1]);
p.position.y = parseInt(data.split("-")[2]);
}
}
and thats BASICALLY it, this is not everything you need to do, because depending on your game, you may have different approaches to sending and receiving to/from server. The thing is, you need to think WHAT you need to send, and WHAT you need to receive to make your game actually multiplayer, on a top shooter for example, you need to receive players posX, posY, HP, maybe their Name. Remember that in MOST cases you need to send and receive your IP so the server can know who are you on his player's list. You also may need to send animation position, or simply if the player is actually moving, so you can play the animation of other players in your game