0
\$\begingroup\$

I'm having a code design problem involving synchronization between the server and clients. I have a class GameWorld that handles all of the simulation details (positions, velocities, projectiles, what have you) that exists in two places:

  • on the server, it is contained in the class GameRoom which handles all of the players connected to a specific lobby, reads and sends messages.
  • on the client, it's in the class GameplayGameState (using the stack of states pattern).

The problem is that i want to include the simulation code in this single GameWorld class to avoid the various bugs involved in duplicating the code in both sides (maybe i change something in one but forget to do the same in the other). However, there is one thing that needs to change between the two: on the server, GameWorld needs to notify GameRoom somehow about events (like "this player died", "this player shot in that direction"), so that GameRoom can then broadcast the event to the clients. On the client, however, i need no such thing, but i do need a way for GameWorld to handle similar events for other reasons (for example, a dying player doesn't need to notify any clients but it does need to play a sound or display a message or something).

Programmatically, how would I go about implementing different behaviors for different sides? Keep in mind the code for GameWorld is separate and can't have ANY knowledge of its outside overlords. How can two different programs get the information they need without GameWorld knowing what to give them? keep in mind polling is off the table, because it would be ridiculously inefficient to scan the GameWorld every tick for changes, keeping the previous state for reference. GameWorld itself needs to generate the events.

\$\endgroup\$
  • 1
    \$\begingroup\$ Not sure the syntax in C++, but sounds like you want to use events. That is, GameRoom/GameplayGameState will pass function pointers to GameWorld for certain known events, then when that event occurs in GameWorld, it checks if there's anyone subscribed and pushes the notification by calling the function(s). The event subscribers can then decide what more specific things happen when that event occurs. \$\endgroup\$ – milk Aug 3 '16 at 18:37
  • \$\begingroup\$ I have absolutely no idea why this didn't occur to me, i've used events before. Thanks! please post this as an answer so i can mark it. \$\endgroup\$ – asdfguy Aug 3 '16 at 21:27

Your Answer

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

Browse other questions tagged or ask your own question.