6 Replies - 590 Views - Last Post: 15 October 2013 - 07:09 AM Rate Topic: -----

#1 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 370
  • Joined: 11-July 12

Java Sockets. What I need to send?

Posted 09 October 2013 - 06:59 AM

I would like to know, what is the best way to exchange information between players over socket. I mean, do I need to send primitives(such as playerX coordinate, playerY coordinate, ballX coordinate, ballY coordinate, the player status, for example which item he has in his hands and etc). Or I need to send whole Player object, ball object and etc(if it is even possible). Or no one of these?

This post has been edited by novakasss: 09 October 2013 - 07:00 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Java Sockets. What I need to send?

#2 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1199
  • View blog
  • Posts: 7,304
  • Joined: 07-September 06

Re: Java Sockets. What I need to send?

Posted 09 October 2013 - 09:35 AM

It really depends, how much of the information does each player need to know about the other that they're seeing?

If it is say an MMO you need to send the position, rotation, name, health, buffs, debuffs, guild, armor, dyes, appearance, and current actions (if any) at a minimum. Now, that may sound like sending the entire object over is a good idea, but it is generally more difficult than that. I'd suggest just sending what you actually need to, it will make the packet size smaller and as a result the game will be more responsive.

However, I'm not a game programmer by any means, so someone else may have better input.
Was This Post Helpful? 1
  • +
  • -

#3 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 370
  • Joined: 11-July 12

Re: Java Sockets. What I need to send?

Posted 09 October 2013 - 09:47 AM

View PostBetaWar, on 09 October 2013 - 09:35 AM, said:

It really depends, how much of the information does each player need to know about the other that they're seeing?

If it is say an MMO you need to send the position, rotation, name, health, buffs, debuffs, guild, armor, dyes, appearance, and current actions (if any) at a minimum. Now, that may sound like sending the entire object over is a good idea, but it is generally more difficult than that. I'd suggest just sending what you actually need to, it will make the packet size smaller and as a result the game will be more responsive.

However, I'm not a game programmer by any means, so someone else may have better input.

I don't need to share a lot of information, just update players and balls coordinates. Also when player has the ball.
But I think it can be hard to know when client is sending ball's x, when players y. Of course I could make some sequence, first server accepts x of ball and etc. But is it good approach? Cause it looks very primitive.
Was This Post Helpful? 0
  • +
  • -

#4 frostraver  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 93
  • Joined: 11-September 12

Re: Java Sockets. What I need to send?

Posted 14 October 2013 - 04:00 PM

I think that, I'm still working on this myself, you should try and send the players data as much as possible. So, about every tick in your game. Send and receive every tick so everything should update nicely on your screen when another player moves.

It looks primitive yes but that's the main way to do it. You could however put your player's information in a struct for example and send everything at once (one of my ideas, as I said still trying myself).

I hope this helps.

Regards,
Frostraver
Was This Post Helpful? 1
  • +
  • -

#5 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 370
  • Joined: 11-July 12

Re: Java Sockets. What I need to send?

Posted 15 October 2013 - 05:23 AM

I also would like to ask, which way is more convenient in game development.
For example if player is with the ball, his boolean 'isWithBall' is true.
Is is better to send this boolean to the server, and then another players uses this boolean and for example: "Hm.. if some one else is with the ball, so I can't be with it too. I will set my isWithBall boolean to false".
Or it is better to not send these booleans, player just gets other players coordinates, then inside his code, checks for collisions, sets appropriate player's boolean isWithBall to true and etc.

Which way is better? I don't know a lot under the surface, so I would like to hear experienced programmer's opinion.

This post has been edited by novakasss: 15 October 2013 - 05:24 AM

Was This Post Helpful? 0
  • +
  • -

#6 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1199
  • View blog
  • Posts: 7,304
  • Joined: 07-September 06

Re: Java Sockets. What I need to send?

Posted 15 October 2013 - 06:41 AM

This gets a little more interesting. You definitely want the server to be involved with who has the ball, because otherwise you are leaving it up to the computers to not lag and miss a collision -- thus leading to multiple players seeing a completely different game.

However, the best way to accomplish it? I can't really say.

You could approach it with a structure like so:
bool playerMoved
bool ballMoved
int playerX
int playerY
int ballX
int ballY



Or something along those lines. Basically, you read in 18 bytes and look at the first 2, from there you can see which of the next 16 bytes actually need to be looked at to update properly.

You could also choose to instead approach it like so:
int movementType
int x
int y



Where the movementType tells you if it was a player or the ball moving (and could store additional information in there) and then has the associated x and y coordinates.

You could also extend the second model easily to string information together:
int records
int movementType
int x
int y
...



Where the first integer tells you how many 12-byte records follow, and then you just read in each of the 12 byte records to update the ball, and players, and anything else that needs that record type.

The point here is that things can be as simple, or as complicated as you want.
Was This Post Helpful? 1
  • +
  • -

#7 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 370
  • Joined: 11-July 12

Re: Java Sockets. What I need to send?

Posted 15 October 2013 - 07:09 AM

View PostBetaWar, on 15 October 2013 - 06:41 AM, said:

This gets a little more interesting. You definitely want the server to be involved with who has the ball, because otherwise you are leaving it up to the computers to not lag and miss a collision -- thus leading to multiple players seeing a completely different game.

However, the best way to accomplish it? I can't really say.

You could approach it with a structure like so:
bool playerMoved
bool ballMoved
int playerX
int playerY
int ballX
int ballY



Or something along those lines. Basically, you read in 18 bytes and look at the first 2, from there you can see which of the next 16 bytes actually need to be looked at to update properly.

You could also choose to instead approach it like so:
int movementType
int x
int y



Where the movementType tells you if it was a player or the ball moving (and could store additional information in there) and then has the associated x and y coordinates.

You could also extend the second model easily to string information together:
int records
int movementType
int x
int y
...



Where the first integer tells you how many 12-byte records follow, and then you just read in each of the 12 byte records to update the ball, and players, and anything else that needs that record type.

The point here is that things can be as simple, or as complicated as you want.


Im not very confident in reading information like that. I will show you what I have done yet:
This is server:
		public void sendInformation(){
			try {
				dos.writeInt(players.size()-1); //Sends amount of players
				for(Connected c : players){
					if(c.nickname != nickname){
						dos.writeUTF(c.nickname); //Sends nickname
						dos.writeUTF(c.team);
						dos.writeDouble(c.playerX); //Sends player's coordinates
						dos.writeDouble(c.playerY);
						dos.writeBoolean(c.controlsTheBall); //Sends whether player is controlling the ball
						if(c.controlsTheBall){
							dos.writeDouble(ballX); //Sends ball's coordinates
							dos.writeDouble(ballY);
						}
						dos.writeInt(score1);  //Sends score
						dos.writeInt(score2);
						dos.writeBoolean(c.isShooting); //Sends whether player is shooting
					}
				}	
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		public void getInformation(){
			try {
				if(controlsTheBall = dis.readBoolean()){ // If player is controlling the ball, read ball's coordinates
					double bx = dis.readDouble();
					double by = dis.readDouble();
					setBallCoords(bx,by);
				}
				isShooting = dis.readBoolean(); // If player is shooting
				if(dis.readBoolean()){ // If player made shot, read results
					int s1 = dis.readInt();
					int s2 = dis.readInt();
					setScore(s1,s2);
				}
				playerX = dis.readDouble();
				playerY = dis.readDouble();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}


Is this a good way to get and then send information? I mean, I have never done this before, actually this is the first time I am working with sockets, servers and clients communication.
Server sends information and then client, make appropriate decisions with information it has got.For example, if player is controlling the ball, then he should decide where the ball is, cause he is moving it around. Or for example if player has made a shot, he scores, so he should know the real result. Is that okay? :D/> Wouldn't it make more troubles in the future if game will become more complicated, or maybe here is some key mistakes?

This post has been edited by novakasss: 15 October 2013 - 07:11 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1