1
\$\begingroup\$

So I've been coding a little project in JS recently, using node.js and socket.io to create a server, and HTML5 canvas to draw the client side. The user receives info from the server every 20ms, and the client interprets the data and displays it. If the user presses a key on the keyboard, it stores this information and sends it to the server instatly - so currently, the code is basically 'is keyboard pressed? if yes, send data to server'.

This has all worked great so far, however recently I increased the servers enemies (AI objects) from only 10 to a whopping 100... this lagged the server. Although I will work out enemy spawning efficiency later, the problem that immediately became apparent is that the user's character moved slower when the server was lagging. This meant that chasing enemies were able to easily catch up to the character and kill him, whereas usually the character could run away with relative ease.

This leads me to the question, what is the most efficient way that the server can interpret the keypresses of the user, and ensure that even if it is lagging that enemies will move at the same proportional speed to if the server wasn't lagging.

Sorry if this question is long-winded, any advice or help would be greatly appreciated. I assume that the same methodologies should apply from different coding languages, so if you have a vague idea how it works in a language you've used... please still share! :)

Thanks,

Cameron

\$\endgroup\$
2
\$\begingroup\$

In desktop games this is usually resolved by having the client assume it's allowed to do something (and running it's own physics engine etc). You could do the same thing here. The client uses the server to sync for online save or multiplayer and the server can reject events sent from the client; if this is the case the clients environment is set to what the server considers the current valid state.

That said; your problem is almost certainly that your code doesn't have a delta; your player should move X per second not X per key press.

So (no particular language);

Update (float delta){
    If (keypressed){
           Player.x+=delta*SPEED_PER_SECOND;
    }
}

This ensures a lagging server moves in larger jumps if lagging but at the same velocity

\$\endgroup\$
  • \$\begingroup\$ Thanks for your reply. You are correct, I haven't used anything to 'fill in the gaps' of the player pressing the keys (which is what delta seems to do, after a bit of research). I've read a bit more about it here. I'll attempt to add it to my server code and get back to you when I know the results. Thanks! :) \$\endgroup\$ – Cameron C Jan 11 '16 at 12:52

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.