Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Commands issue when trying to use Remote Players Prediction

Discussion in 'NetCode for ECS' started by PhilSA, Mar 27, 2022.

  1. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    In my project, I've noticed an issue where commands written at a specific tick sometimes seem to disappear during the following frames. A detailed description of the problem follows, but you may want to read the rest of the post before you read that part in spoilers, because I think my real problem is a faulty prediction setup

    This is what's happening, in chronological order, on the local-owned player on Client:
    • a playerCommands system in GhostInputSystemGroup updates at tick 797
      • checks if commands already existed for tick 797 by doing a GetDataAtTick(797); finds out that's not the case
      • adds commands at tick 797 to commands buffer using AddCommandData()
    • a prediction system in GhostPredictionSystemGroup updates at tick 797
      • successfully does a GetDataAtTick(797) that returns commands of tick 797
      • everything working as expected so far, and we've confirmed that a command at tick 797 does exist in the commands buffer
    • a playerCommands system in GhostInputSystemGroup updates at tick 797 once again (new frame, but still on same tick),
      • checks if commands already existed for tick 797 by doing a GetDataAtTick(797); but it returns commands of tick 796. This is the part I wasn't expecting. Where did the previous commands of tick 797 go? I've tried to manually iterate on the commands buffer at this point to find the latest tick, but even this returns 796. The commands of tick 797, that we successfully retrieved during the last prediction frame, seem to have disappeared
      • adds commands at tick 797 to commands buffer using AddCommandData()
    I'm not sure why this situation happens at the moment, and why the commands added for tick 797 seem to have disappeared from the buffer during the next frame

    After trying out a bunch of stuff, it looks like the problem disappears when I don't try to use Remote Player Prediction. So my guess is that the problem is simply that I haven't set this up correctly. Here's what I do
    • my ICommandData buffer is built-into the Player ghost prefab instead of being added at runtime. So it's always present regardless of if we're the local player or not, and regardless of if we're on Client or Server.
    • my ICommandData ghost is set up as described here; It has a regular [GhostComponent()] without params, and all fields have a [GhostField] without params
    • my Player prefab has "Support Auto Commands Target" and ghost mode is "Predicted"
     
    Last edited: Mar 27, 2022
  2. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    I'm pretty sure if you're sending commands to remote clients, you need to use:
    Code (CSharp):
    1. [GhostComponent(OwnerSendType = SendToOwnerType.SendToNonOwner)]
    To prevent commands being incorrectly overwritten
     
  3. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    Tried this, and it makes a lot of sense, but it seems to not have solved the problem. I think I'll have to try to debug this more carefully and see if our local player commands still receive anything from server
     
    Last edited: Mar 27, 2022
  4. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    774
    The use of
    Code (csharp):
    1.  
    2. [GhostComponent(OwnerSendType = SendToOwnerType.SendToNonOwner)]
    3.  
    Is not really "necessary" for command. We ensure that behaviour by default in code-gen (you can see an info in the code-generator logs)
    But it is a good practice to add that.

    For the problem itself,

    This is the part I wasn't expecting. Where did the previous commands of tick 797 go

    The problem is due to the prediction backup. On the client, we backup the state of the predicted entities for the last full tick.
    And we restore the entities to that state for each partial tick.

    So in your case:
    196 <-- full, backup
    197 <- 1st partial (no restore because the previous was 196)
    197 <- 2nd partial (restore from last backup, so 196)

    When remote player prediction is on, we also backup the command buffer, something usually we don't do if the remote prediction is not active for that input command. And that may cause the problem your are seeing.
    Because now your input buffer is restore from the backup and such, you lose you last enqueued input.

    We are going to fix this in the next update. Thanks for reporting the problem.
     
    Last edited: Mar 28, 2022
    TRS6123 and PhilSA like this.
  5. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    180
    @PhilSA these forum posts help me so much, I'm very grateful that you take the time to write up your findings in a public forum. Also very grateful to the Unity team for their presence on the forum.

    This topic of input prediction seems to be one of the biggest sticking points in Netcode.
    It's not that the API is bad, the documentation probably just require a few more examples of these more advanced topics.
     
    PhilSA likes this.
  6. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    774
    I completely agree about the docs and sample. We are working on improving both. Stay tuned
     
    Krooq and Occuros like this.