Search Unity

Netcode cube example DeltaTime

Discussion in 'NetCode for ECS' started by shortenda, Jun 6, 2022.

  1. shortenda

    shortenda

    Joined:
    Sep 30, 2017
    Posts:
    5
    Hello,

    I have a question regarding the example code shown in the cube example, specifically in the
    MoveCubeSystem.

    It seems like we apply the DeltaTime for the current Update call to all of the previous tick's
    CubeInputs. If I understand correctly we will sometimes process multiple tick's inputs (e.g., if there is a rollback to a server snapshot and the inputs are replayed over the top of that?) and in that case we'll use the current DeltaTime instead of the DeltaTime from when they were generated.

    Concretely if we say we generate 60 inputs per second (a tick every 16ms) and then there's a short stall locally (say, of 30 ms), followed by receiving a snapshot from the server, we would then use the DeltaTime of 30 ms instead of the delta time of 16ms. This, I believe, would result in each input's delta being ~doubled. Of course, this will be resolved with the next snapshot update, but would result in some temporary local wackiness.

    Am I misunderstanding how this works, or is this a real issue? If so, what might be a good way of resolving this?

    Perhaps if you had a ghost component that stores the DeltaTime that is locally predicted and updated each frame to the DeltaTime and read in the system then you could use that? Maybe make it a Singleton component so that it doesn't need to be re-done for each entity? Is this a horrible idea?
     
  2. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    Entities has its own Time.DeltaTime - the regular one is Time.deltaTime (lower case 'd'). When the prediciton loop runs we change the entities version of Time.DeltaTime and Time.ElapsedTime to be the values for the tick you are currently predicting.
    So as long as you're using systems to update and get the time from the system it's correct, if you get UnityEngine.Time.deltaTime it would not be correct.
     
  3. shortenda

    shortenda

    Joined:
    Sep 30, 2017
    Posts:
    5
    That makes sense, thanks for the info!
     
  4. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,129
    Is that ok to use Time.deltaTime to do timing like do something after 1.5 second or it's still better to use tick to do timing?
     
  5. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    If you want something to be tied to real-time regardless of how fast the game is running you can. An example would be show a tooltip for 1.5s where you do not want the game running slower to keep the tip longer.

    For anything that should be tied to game time you should not use it. For example if you have a 1.5s cooldown and it for some reason is not predicted you should still use the entities time.