[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

Synchronizing movement in a server based online game

Right now my basic game architecture is as follows:
I have an input manager that simply waits for the user to input something, decides what that input does, and fires the appropriate event.

The client class is the class that communicates with the server.  It does two things:  First of all, it waits for packets from the server, parses them, and fires the appropriate event.  Next, it waits for events that are fired by the avatar (main players model), and if they are not cancelled, packs them up and sends them to the server.

The model manager is the class (self referencing singleton) that manages all the actions of the models.  It subscribes to all the events fired by the client and the input manager, and calls the appropriate methods on all the models in the game.

I also have the main window, which pretty much just acts as the device context for rendering (although for now it handles the camera).

So I have finished the basic implementation of this architecture, but am running into some thread sync (I think) issues.  The first problem i have run in to is coming up with an effective way to handle movement.  Right now, whenever the avatar moves, it fires an event that the client subscribes to and sends to the server.  The problem is that whenever someone hits the "w" key, which starts moving the model, that event will be fired many times per second.  So to fix this just as a temporary fix, I made it so that the move event is only sent to the server once every 10 times.  For some reason, the movement is only updated once on other players' machines.
Now, it could just be a bug in my code (which i can fix), or i can be doing something that is wrong in a broader sense.  Any suggestions?
  • 3
1 Solution
davidastleAuthor Commented:
One thing i forgot to mention is that the movement is only updated whenever the scene is rendered.  In my old system, in the OnPaint method, I just updated the movement, found the current location, and sent it to the server.  This worked well enough (I don't care much about latency yet).  However, in my new system, i still update the movement on the OnPaint method.  This will fire the Move event in the avatar class, which the client is subscribed to.  The event just passes in the ID of the object that moved.  The server then grabs the object from the modelmanager, and sends this out in a new event.  I know that this method seems sloppy (the fact that the client is accessing the modelmanager to grab the model, and the fact that the model isn't initially sent in the Move event in the model), but I decided that it is more flexible if the client can somehow send out the object that moved in all of its events.  This is because if the client just sent out the ID, every class that subscribes to the events would have to get the model from the model manager anyway.  Is this perhaps my threading issue, that the server has to get the model from the modelmanager every time the move event is fired?  I am locking the model manager class when i get the model, and am locking the list of models when i render them.
davidastleAuthor Commented:
Also note that the avatar always moves perfectly fine on the clients computer, but the other players' models only move once and then stop!
>I made it so that the move event is only sent to the server once every 10 times.  For some reason, the
>movement is only updated once on other players' machines.
Once every 300-500 ms might be a better idea ... What if key was pressed 9 times and then player stopped moving it further ?
The nature of the symptoms indicates that there sould be a problem with your code.
-Are the events generated by other players being correctly sent?
-Are the events generated by other players being correctly received?
-Are these events being processed correctly, in terms of updating data structures, rendering etc?

Try eliminating these possibilities one by one
davidastleAuthor Commented:
Well this was a stupid question to ask in the first place.  I found the problem (wasn't locking correctly).  Sorry, ill give some points for trying to help me out - and in the future hopefully i won't ask stupid questions.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now