We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have been working on this game and I tried to make a multiplayer function with a server and a client but I can't seem to get it working with many problems such as a chance that the server will send out a null pointer exception when a client disconnects. I have been able to get the players to show on the other user's screen but it would lag such as when you move, you would move 10 seconds later on the other screen. I can post a GitHub of all the code below but ill write in the important bits below:
GitHub:
https://github.com/rockyhawk64/Trimate.git
CLIENT:
Client Sending Info:
positions();
send = name[0].replaceAll("=", "-") + "=" + x + "=" + y + "=" + bulX + "=" + bulY + "=" + dirr + "=" + gun[0] + '\n';
client.write(send);
Placing Other People On The Screen:
void positions(){
if(masf != null){
for(int i = 0; i <= split(masf,'$').length-1; i++) {
String coll[] = split(masf, '$');
String col[] = split(coll[i], '=');if(col[0].equals(client.ip())){
//if the IP is itself
}else{
if(col[6].equals("down")){
image(Edown, x+width/2+camX-float(col[2]), y+height/2+camY-float(col[3]));
}
if(col[6].equals("up")){
image(Eup, x+width/2+camX-float(col[2]), y+height/2+camY-float(col[3]));
}
if(col[6].equals("left")){
image(Eleft, x+width/2+camX-float(col[2]), y+height/2+camY-float(col[3]));
}
if(col[6].equals("right")){
image(Eright, x+width/2+camX-float(col[2]), y+height/2+camY-float(col[3]));
}
}
}
}
}
Recieve Server Message:
void clientEvent(Client client) {
masf = client.readStringUntil('\n');
}
SERVER:
import processing.net.*;
Server server;
String iM = "";
int port = 5204;
String players = "";
String positions[] = {};
String col[];
void setup(){
size(800, 400,P2D);
server = new Server(this, port);
}
void draw(){
background(0);
Client client = server.available();
if (client != null) {
iM = client.readStringUntil('\n');
if(positions.length == 0){
String join[] = {client.ip() + "=" + iM};
positions = join;
}
String found = null;
String ip = client.ip();
if(positions.length != 0){
for(int i = 0; i <= positions.length-1; i++) {
if(positions != null){
if(ip.equals(split(positions[i], "=")[0])){
found = str(i);
break;
}
}
}
}
if(found != null){
positions[int(found)] = client.ip() + "=" + iM;
}else{
append(positions, client.ip() + "=" + iM);
}
server.write(join(positions, '$'));
}
if(positions != null){
col = new String[0];
for(int i = 0; i <= positions.length-1; i++) {
if(split(positions[i], "=")[0].equals(split(players, "$"))){
//not used
}else{
for(int ii = 0; col.length == positions.length; ii++) {
if(ii != i){
append(col, positions[ii]);
}
}
}
}
positions = col;
}
}
void serverEvent(Server server, Client client) {
players = players + "$" + client.ip();
println(client.ip() + " Joined The Game");
}
void disconnectEvent(Client someClient) {
col = new String[0];
for(int i = 0; i <= split(players, '$').length-1; i++) {
if(split(players, '$')[i].equals(someClient.ip())){
for(int ii = 0; col.length == split(players, '$').length; ii++) {
if(ii != i){
append(col, split(players, '$')[ii]);
}
}
}
}
players = join(col, '$');
}