Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (623)
Games in Android Showcase (176)
games submitted by our members
Games in WIP (676)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Which networking approach on cannon-fodder like game ?  (Read 2348 times)
0 Members and 1 Guest are viewing this topic.
Offline chillum

Junior Newbie

« Posted 2007-05-10 02:01:26 »

First of all ... Hi  Smiley,

My name is chillum and i'm studying informatics on the University of Kassel / Germany.

I just found this Forum and i've read a few hours this night,

its just great Smiley.

If my english might sound weird in the following post, i excuse me in advance, it's pretty late and i'm damn tired atm, but i just want to get some opinions as soon as possible Wink

I'll begin why i'm interested in the network part of a multiplayer game Smiley

Yesterday I got an exercise to program a simple server/client chat with chatrooms, so i played with sockets and PrintStreams and so on.

After a few hours of reading and gathering of implementations types, i choosed the Multithreaded version and
finished it yesterday. It wasn't that hard. All runs fine.

It's based on a server with a thread, which listens for clients.
if a client connects, it opens a own new Thread on the server specially for one client.
This thread listens for an incoming String, processes it and send an answer back to the client.

Like I said, all runs fine, but i'm not happy about the processing of the incoming data.

It's only based on Strings and a wild bunch of if/else operations, which check if the user wants to create a room or something like that.

Now the game part :

Today i played a cannon-fodder like game, where 2 players play on the same keyboard and i felt to build a clone, but only with a multiplayer part and some "cool" modifications and new features Wink. There will be 2 player objects with a bunch of AI-comrades and even more Monsters, which have to be killed. I think there will be ~100 Objects on the GameField PLUS temporary objects like Bullets, Addons and so on.

And here the network part kicks in. My first idea was using the String Style Method, like in the chat-programm.

But then i thought at the amount of if/else operations and couldn't believe that this will be any kind of elegant or fun.

So i found the class ObjectStream and thought it should be much better to pass objects directly, if a monster position changes, the server sends the monster object with the new status/position to the client, and the client replaces his monster object with the new one.

But this could be very slow, i thought. So i searched a lot more and found RMI ... i thought PERFECT ... got a Bingo Sample from SUN to work and then realized, way to complicated for the first steps in game networking.

Now, late at night i had the idea to create an Object for every possible action in the game. E.g. MOVE ...

so i don't need to pass the whole monster or player object with all his data, but a special fitted object which only contains the changes, that should be made.

For Example:

class Player , which basically contains of
int ID;
int x,y; // coordinates on the screen
Weapon weapon; //current weapon

then i create a
class move, which contains only the changes occuring during movement
int id; //which object should be moved ?
int x,y; // to which position it wants to be moved or how many pixel refering to its old position

So when the player wants to move (or movement key is pressed )
a new move object is created and the constructor Move(id,x,y) is called.
eg Move doMove = new Move(id,x,y);
out.writeObject(doMove); //or something like that, don't worked with ObjectStreams before :/

the server gets the object, identifies it with isTypeOf and handles it.
The server reads out the id, and changes the x,y values of his Object with this id.
then he creates an own move object with the same id and sends it back to the client, which updates
his object.

I hope its clear what i mean  Tongue.

Could this type of networking work with an max. object count of  ~300 ?

Or should i use only functions which will code the performed action in a
String instead of doAction-Objects ?

I would prefer the Object approach Wink , it just fells ... ehrmnn ... right Wink

Hope this wasnt too confusing at all Smiley

And thanks for reading Smiley

Good Night/Morning all Grin
Offline endolf

JGO Coder

Medals: 7
Exp: 15 years

Current project release date: sometime in 3003

« Reply #1 - Posted 2007-05-10 07:15:15 »


Your on the right path using update messages. Some people around here have passed objects using object streams, when I've done simple things like this before I've written message passing libraries so that I don't have to serialise and deserialise java Objects, which can be considered slow. I've not micro bench marked it, but logic suggests it's faster to do a switch statement on an integer message code than it is to do a whole bunch of if then else statements to find the instanceof that matches and run that.

If you do want to use objects you could always have an interface for each object you pass around that has a perform method or the likes, and call that, and the object knows what it is meant to do, so a 'Move' object for example, knows how to perform the move, that way you get rid of the if then else block. Messages could be made smaller too.

JGN (forums here) has an idea of a sharable object, that handles the network layer forr you. I've not used it myself though.

Project Darkstar is designed for Massivly Multiplayer Online games, and is more difficult to get your head around, and probably offers more features than you want at this stage.

Welcome to the forums Smiley



P.S. Are you going for 2D or 3D game play?. For 2D I would recomend you look at Slick for 3D jMonkeyEngine. Other API's exist, but those seem to be popular ones.

Offline chillum

Junior Newbie

« Reply #2 - Posted 2007-05-10 09:47:13 »

Hi endolf,

thx for your comment. If i unterstood you right, the (String)Message Passing Method is the "standart" used one in small games ? Smiley

I would like to try the ObjectApproach, but i have to read something about the serialization, and Object Passing.

Is it Possible to send an Object of e.g. Type MOVE from client to server,

and the Server just saves it in an simple

Object newObject = objIn.readObject( );

and after that determine, if its an move, shoot or build a house object with "instanceof" ... (dunno where i got isTypeOf from  Huh )

But i don't understand this:

If you do want to use objects you could always have an interface for each object you pass around that has a perform method or the likes, and call that, and the object knows what it is meant to do, so a 'Move' object for example, knows how to perform the move, that way you get rid of the if then else block. Messages could be made smaller too.

How to determine if the passed Object is a "move", "walk" or "die" -Object without instanceOf or many if/else statements ?

I thought interfaces are only "raw blueprints" for a class and they can't have any code in themselves which
can change something at the gamestate, like movePlayer(this.x + newMove.x, this.y + newMove.y) ?

If you find the time, can you explain it again to me Smiley ? Thank you..

The game should be in 2D, but i'll try to write it without extern Game API's. It will probably be pain, but i want the learning effect, which comes with the pain Grin .

I did a SpaceInvaders Tutorial before ( think it was a spanish site), without scrolling but with BufferedImage, Sound, CollisionDetection aso. Was pretty much fun and not that hard at all.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline endolf

JGO Coder

Medals: 7
Exp: 15 years

Current project release date: sometime in 3003

« Reply #3 - Posted 2007-05-10 19:11:23 »


Message passing seems to be, but not very often strings. For example, in my current project a 'object update' message is a bunch of bytes, the first 4 are a byte encoded version of an integer, this integer is a message type id. Then a bunch more bytes representing the object ID as an int, then some bytes that represent floats that represent a vector object etc.

It means I transmit the minimal number of bytes I can. The raw data just looks like a heap of bytes that have no meaning, it's only the message classes that gives it any meaning. You could see this as fast object serialisation, but it's done outside of the standard serialisation methods.

When I was talking about passing objects with interfaces I meant that every object (move object, quit object) you passed across implemented a common interface, lets call it GameAction. When you do your Object newObject = objln.readObject(), because you know they all implement GameAction, you can do GameActionnewObject = (GameAction)objln.readObject();. Then you can do; the move object itself would have to know what to do to perform it's job. No instanceof needed as you don't care if it's a move or a walk or die object, the object knows.

You will need to figure out some way for the object to get hold of the game data model so it can do it's magic, but you could just pass that in.


public interface GameAction {
    public void run(GameData dataModel);

public class GameData {
    public Player getPlayer() {
        // Return the player object

    public GameObject getGameObject(int objectId) {
        // Look up and return the object

public class Walk implements GameAction {
    public void run(GameData gameData) {
        gameData.getPlayer().walkTo(this.x, this.y);

public class Move implements GameAction {
   public void run(GameData gameData) {

In your network layer, you now don't care if it's a move, or a walk, you just call run(GameData) on the newObject.

Hope that makes sence.


Offline chillum

Junior Newbie

« Reply #4 - Posted 2007-05-12 15:36:17 »

Thanks for this Explanation, I think i got it.

i have some spare time today, and i'll try to draw a rectangle on the server and then
move it slowly. I'll try the ObjectPassing to motivate myselfe before i break my head on the bytecoding approach Smiley

You helped me a lot, don't see the end of the tunnel, yet, but you turned the lights in it on Smiley

Thanks again  Cheesy
Offline sunsett

Senior Devvie


« Reply #5 - Posted 2007-05-14 13:38:13 »

As Endolf suggested, this is something that JGN inherently supports.  You might give it a try. Wink

....however, I'm somewhat biased since I wrote it.  Tongue
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

BurntPizza (17 views)
2015-10-07 02:11:23

KaiHH (15 views)
2015-10-06 20:22:20

KaiHH (11 views)
2015-10-06 19:41:59

BurntPizza (24 views)
2015-10-06 19:04:48

basil_ (44 views)
2015-09-30 17:04:40

shadowstryker (22 views)
2015-09-29 15:55:06

TheSpaceHedgehog (28 views)
2015-09-29 01:58:48

GamerC4 (54 views)
2015-09-24 21:10:38

GamerC4 (71 views)
2015-09-24 21:09:48

htuy (28 views)
2015-09-24 04:57:24
Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36 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‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!