Search Unity

Question Predicted Randomness in Multiplyer Games

Discussion in 'NetCode for ECS' started by Opeth001, May 30, 2023.

  1. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,116
    Hello everyone,

    I have a situation where I need to predict simple random events on both the client and server sides. For example, let's say there's a 20% chance of a critical hit.

    Here are my questions:

    * What's the best way to make accurate predictions?
    • It seems reasonable to use the server tick as a seed for prediction. This way, we can ensure consistent results.
    * How can we ensure that the server always applies the same tick values during each simulation frame, even when replaying the game?
     
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    - If prediction is a must, I would pre-randomize the numbers on the beginning of the match and keep a stack of the future values synced (i.e. the stack would say that the hit number 7 and the hit number 20 will be critical). Everytime the stack advances the server can then send new values to save for the future.
    - Using the technique above, you just need to store this stack somewhere.

    Also, you need to consider that things can happen in different orders for different clients, so ou will need to have 1 stack per context (i.e. for the hit example, you need for each different attack/character you have). Unless, of course, you have a deterministic gameplay, but if you already had that then I believe that dealing with random would not be an issue as you could just use the same initial seed for everybody.

    To be honest, I would say that the best is to simply not predict things that are randomized, and instead just make the server send to the clients the randomized events/values as soon as they are calculated (and calculate as soon that the server can tell that the value will be needed). For replay, you will still need some kind of stack to store those values for replaying later.
     
    Opeth001 likes this.
  3. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,116
    in our case the gameplay and replays must be 100% deterministic in order to make Debugging and Anti Cheating operational.
    I think we'll just initiate a seed that will change every simulation tick and it will be used in any random operation.

    Thanks ;)
     
    brunocoimbra likes this.