Search Unity

Random number sync between server and client

Discussion in 'Multiplayer' started by Hun_el, Jul 29, 2018.

  1. Hun_el

    Hun_el

    Joined:
    Aug 28, 2017
    Posts:
    5
    Hello,
    I'm learning how to use UNet and at the moment I'm just trying to build some simple samples that will help in the long run on the development of my application. Right now, I have a simple scene with 2 players and a text field where I feed random numbers that updates every 2 seconds. The problem that I have is that the numbers are always different between client/server or server/client/client.
     
  2. Omniglitch

    Omniglitch

    Joined:
    May 29, 2017
    Posts:
    37
    Hun_el likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Get the random number only on the server. Set it to a syncvar. Any client just checks the syncvar, and does not generate its own random number.
     
    Hun_el likes this.
  4. YVanhoutte

    YVanhoutte

    Joined:
    Mar 5, 2018
    Posts:
    8
    Alternatively you could let the server generate a seed, send the seed over to all clients, have the clients use this seed for their random numbers generator, and they should then generate the same results as the server every time (as long as they generate the same amount of numbers, else they'll go out of sync).
    The advantage of this would be that if you need to generate a lot of random numbers at once (for example hundreds of units rolling for critical damage) you don't need to send all their results from server to all the clients. Instead you just tell the clients to roll X amount of random numbers, and you'll get the same result.
    Someone correct me if I'm wrong with that here, but I believe that's how RTS games for example solve this issue.
     
  5. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I too was going to suggest a seed. I'm confused why others would recommend syncing the variable, doesn't that just introduce extra networking? If you introduce a seed value on connect, then RNG should be deterministic.
     
  6. YVanhoutte

    YVanhoutte

    Joined:
    Mar 5, 2018
    Posts:
    8
    I suppose it's a matter of scale and convenience. If you're having few networked objects that synchronise their state constantly, like in a multiplayer FPS, it makes little sense to extract your RNG-based calculations from this synchronization. Just let the server handle the calculations and send the results over to all clients.
    This becomes less feasible when you're dealing with many objects at once, like in an RTS game, where it makes more sense the entire game is deterministic and only synchronize player inputs over the net (this is called lock-step).
    In that case determining a random seed and synchronizing that at the start of the game would make perfect sense.
    I'm mostly just suggesting it as an alternative, in case it could be useful.
     
  7. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    292
    I've tried the "send the seed" path before and it is fraught with peril. Ensuring that all your other code is deterministic, and that all your calls to the RNG are done in the same order is _very very hard_. This was for a simple turn-based card game, and it became untenable for me. Now, maybe I'm just not good enough to make this work (possible!), but the downsides outweighed the upsides. YMMV, IMHO, etc etc. Good Luck!