0
\$\begingroup\$

I wish to make a Java based multiplayer game. I am learning all about sockets and so forth. I have built a very basic UDP system that serializes an object with all the information about the game state and then sends that to the client. The client sends input which changes the game state.

My question is, is serializing an entire object an acceptable way to send game info over the internet.

It's super simple but my placeholder images are lagging quite a bit across the screen on a localhost. This could be for several reasons but I was wondering if serializing and then sending and then de-serializing was just too slow a method? This game will be fast paced and reflex oriented.

\$\endgroup\$
  • \$\begingroup\$ What does your profiler say? Generally, you send just enough info for the game to work, not more. Also, lag can be compensated by client side prediction; based on the input and the world state, the client can guess what the server will reply and show that to the player, until the actual state is received. \$\endgroup\$ – Vaillancourt May 11 at 23:33
  • \$\begingroup\$ Currently it sends the x and y positions for ten players. Also a string as a map name. Nothing too crazy and far less than what I would want to be in there anyway. \$\endgroup\$ – Galindan May 11 at 23:46
  • \$\begingroup\$ Doesn't look like it's that much, as you say. I suggest you profile a game session and see where is the bottle neck. Also, as I said, you don't want to just wait for the server to "authorize" the action, you want to start perform them right away. As an example, in Diablo III, if your character becomes disconnected, you'll still be able to move around, and start attacks. You won't see any mobs, and the spell will not come out, but you'll still see the character do something. That's really something that you have to put into your game design. \$\endgroup\$ – Vaillancourt May 12 at 0:36
1
\$\begingroup\$

is serializing an entire object an acceptable way to send game info over the internet.

Yes and No, if the objects data is reasonably small and serialization is fast there's no problem. You generally want to send the least amount of data possible so java's default serialization is not your candidate for game networking, it's slow and generates large byte blobs.

Kryo on the other hand is a very good example for an easy to use and performance oriented serialization library, it allows you to register an object beforehand so that only the objects field-values and a class id (usually single byte) are sent. The default Java serialization generates a lot more data compared to Kryo since it saves additional info which you don't need for game networking but is handy for storage and when classes change.

There even is the Kryonet networking library which is built on top of Kryo. It's reasonably fast and also allows you to send over whole objects with fast, automatic serialization.

It's super simple but my placeholder images are lagging quite a bit across the screen on a localhost.

Serialization is likely not your problem, you could have timing and/or interpolation problems, especially since you said that you only send a few values. Once you're running your game online you will need interpolation to handle network jitter.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.