Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have been working on a client-server architecture to enable LAN play for my open source game, Aigilas. In its current state, players stay synced in all running instances of the game but enemies and particle animations are out of sync.

What I cannot understand is how players are the only entities staying synced. I run the clients in lockstep, requiring the latest input states from each client to be announced before taking a turn in the game world. When the game world is built, I seed the random number generator (RNG) with a seed determined by the server. This seed appears to affect the RNG as expected; each client has the same procedurally generated game world and players start in all clients at the appropriate locations.

Particle animations and AI are chosen based on player positions and the RNG. How is it possible for them to become out of sync when the players are always in sync? To the best of my observations of both the game screen and the logs, players never go out of sync with respect to their positions. That leads me to believe that the RNG in C# is not acting deterministically across all clients.

The basic workflow for the game's client<->server communications are as follows:

  1. Server starts
  2. Client starts and gets RNG seed from server
  3. Client tells server when keys are pressed/depressed by a user
  4. Once a game has started, server tells all clients the current state of each player's keyboards
  5. Once an update has been received from the server, one turn of the game is simulated
  6. Repeat step 4

If there are any specific questions targeting a specific portion of the code, then I am happy link to the appropriate page on GitHub.

share|improve this question
add comment

3 Answers 3

up vote 4 down vote accepted

My guess is that the rng's are generating at different speeds. For instance, if a game is updating at 60 times per second, then theoretically it should be in sync with other games updating at that time. But if a game is updating at a different time, it will either generate future or past numbers generated by the master generator(the server).In other words, are you generating a random number at fixed time?

share|improve this answer
    
After a lot of debugging and testing different scenarios, I realized that the clients were not actually running in lockstep. That meant each client's RNG could get a few turns ahead while another client was a few turns behind. After adding more logic to ensure that clients run in lockstep with a RNG seed from the server, this error disappeared. Thank you for the tip, which lead me down the path towards a working solution! –  XBigTK13X Aug 2 '12 at 5:02
add comment

This completely synced approach only works if the (gameplay) framerate of both games is the same and unsynced values aren't used in sync-relevant calculations (for example isLocalPlayer or getCurrentTime), also the input has to be executed on all clients at the same frame and everything needs to be calculated in the same order.

If you need random numbers outside of the sync-relevant calculations you need two RNGs, one which is synchronized, and one which is not. This is because with every number you generate the seed changes, so for some results it must be called in the same order and with the same frequency.

share|improve this answer
add comment

particle animations are out of sync.

Does this matter? Normally I would not be running particle animations off the same random number generator that I use for important gameplay anyway.

That leads me to believe that the RNG in C# is not acting deterministically across all clients.

Why not simply log the results? Every time you generate a random number, log it. Then compare the lists from 2 clients.

share|improve this answer
    
I did not really care that particle animations were out of sync; it was merely another symptom that something was wrong. Logging the random values proved that they were out of sync. –  XBigTK13X Aug 2 '12 at 4:59
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.