...when i try to use the "writeObject(obj)" method. As far as i can make out from what ive read on the net, i need to do soemthing to do with Serialization for the readObject() and writeObject methods but im not sure what.

i think the problem is the fact that im using the readObject() and writeObject() methods in the wrong way. I was trying to use them to send data (a Sprite object) but im starting to think thats not what they are for, or else im using them arseways.

What methods can i use to send an object as a packet between the client and server? Any simple code example?thanks.

Serialization works by recursivly traverse all the objects referenced by the object you try to write. They all have to implement Serializable. Although I think you can overide "private void writeObject(java.io.ObjectOutputStream out)" and parse the objects yourself. Look at the java.io.Serializable javadoc.

Since you post in the network section I assume you are trying to send the object over the net. Then you have to carefully design the objects you want to send. Try to only use primitive types, and remove all objects. As an example you do not want to have a reference to a BufferedImage in your sprite. It would cause the whole image to be sent every time you send your sprite. Instead keep a int id that looks to image up from a table.

You may also consider ditching serialization and parse your object manually using DataInputStream and DataOutputStream.

Its a LAN game, not Internet. Heres what im trying to do, maybe you can suggest a better way.

When i move my player (an object of the Sprite class) i want to update all the clients screens so i was creating a copy of the Sprite object and trying to send that. The reason i dont simply send the players new x,y coords is because thats not enough information i.e. it doesnt say what direction the player is facing (he can walk in 8 different directions).Can you suggest a way for me to do this? Maybe a bit of sample code? Thanks.

Even if it's a lan game you don't want to send the sprite image everytime time you send a packet

Quote

The reason i dont simply send the players new x,y coords is because thats not enough information i.e. it doesnt say what direction the player is facing (he can walk in 8 different directions).

Then just send all the information that is needed.

Quote

Can you suggest a way for me to do this? Maybe a bit of sample code?

Ok then:

1 2 3 4 5 6

// socketOut is the socket output streamDataOutputStreamdataOut = newDataOutputStream(socketOut);dataOut.writeInt(posx);dataOut.writeInt(posy);dataOut.writeFloat(direction);// todo: write all other information that needs to be sent

Look at the javadoc for java.io.DataOutput java.io.DataInput, and all the other classes in java.io for that mather.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org