Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am looking for the best approach to build an observer that can monitor the changes in the game object for purposes of client/server synchronization.

For example my game may have multiple game objects : players, doors, mobs. When the player is connected, he may have some objects visible, for these visible objects, if there are any changes, they will be reported back to the player at the next sync frame.

For example if I have the following game object class:

class Player {
  int      health = 30;
  Vector3f positon = new Vector3f(0.0f, 0.0f, 0.0f);
}

I would like to have a (possibly dynamically generated) observer class

class Observer<Player> {
   Player p;
   boolean healthChanged      = false;
   boolean positionChanged    = false;
}

So if I have one player object in the game, and two network sessions are connected, I would expect to have two observer objects watching the player object. And when the game would do something like

player.setHealth(player.health - 10). 

I would want the observers to set healthChanged flag to true.

Here are some approaches that I am considering:

Option 1. Manually build the logic into my game classes to notify the observers any time something changes. Any time a change is made, I would generate a change event to the observer classes, so that they set the appropriate flags, ie

public setHealth(int newHealth) {
   ...
   for(PlayerObserver o : this.getObservers()) {
     o.propertyChanged(PROP_HEALTH);
   }
}

Option 2. Use AspectJ and create cuts in my game objects.

Option 3. Research how hibernate creates their proxies and try to use the same approach for detecting changes.

Performance is going to be a key factor as I want to minimize the latency as much as possible. Optionally if I want to save the game objects state to a database, can I still use hibernate to save/load them or should I use a different approach for persistance?

share|improve this question
The Java Observer interface allows any Object to be passed to notifyObservers. You could pass back this? – Anko Mar 11 at 23:45

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.