Search Unity

Resolved Confused about how snapshots work

Discussion in 'NetCode for ECS' started by vectorized-runner, Jun 29, 2021.

  1. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    398
    The more I read the docs the more confused I get, could youexplain these to me please, thanks.

    1- If I don't add GhostComponentAuthoring to a prefab, then snapshots aren't applied to the Entity at all, right?

    2- What is the difference between GhostComponent and GhostField attribute? Is there a case where I'd use both of them in a component?

    3- When would I add or don't add GhostField attribute to my components? Can you give me concrete example on both?

    4- Should I add GhostField to a component field which gets updated in a prediction loop? (I mean should I do/don't to it always)

    5- What is the difference between GhostComponentAuthoring's component inspector and adding
    GhostComponent to the components? Which one overrides the other?

    6- Are prediction-interpolation-synchronization mutually exclusive? How they all come together?
     
    Krooq likes this.
  2. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    894
    1- Yes, without an authoring component on the prefab, it is not considered a ghost.
    2- The GhostComponent and GhostField are two completely different things and yes, you can have both of them declared. The GhostField MUST be used to mark the component fields that should be serialized. The GhostComponent on the other end, allow you to specify to which Ghost type should be replicated to (Predicted, Interpolated), if Server/Client only and other things.
    3 - The GhostField must be added to a component that need be replicated. Without ghosts fields, a component is not considered replicated. It is not mandatory to mark all fields as replicated. You may have a dependent field witch value can be recomputed by using other replicated data for example. Or a field that is only used by the server/client during the simulation and that not need to be replicated (this is more rare)
    4 - Yes, if something is used inside the prediction loop you must in general add the GhostField for it. Otherwise, when the entity state is rollback to the server state, some component data are not restored correctly and the simulation prediction can be completely wrong.
    5 - The GhostComponentAuthoring is used to configure the prefab ghost. The GhostComponent just provide the default for how a component should be handled by the different ghost type and mode. The GhostComponentAuthoring overrides the GhostComponent (where possible)
    6- I didn't understood exactly what you meant for synchronization, but Predicted-Interpolated behaviour are mutually exclusive at the moment. It is possible eventually to configure at spawn time the behavior of ghost if the SupportedGhostMode is set to ALL (but require some user logic).
     
    vectorized-runner likes this.
  3. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    398
    Thanks for clearing those up. I still didn't quite understand how GhostComponent works though. What's the difference between using InterpolatedClient or PredictedClient? The sentence "the component is only available on clients where the ghost is predicted" on the docs got me really confused. How can clients have different ghost configuration?

    Also I'm guessing if the component is processed on GhostPredictionSystemGroup, then I should have PredictedClient on, in ClientSimulationSystemGroup InterpolatedClient on
     
  4. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    894
    The name are a little confusing probably. We need to clarify better that section.

    There are two type of ghosts: Interpolated and Predicted. When you configure a prefab you specify what mode is using.
    A component may be added to different type of ghost prefab in general. And based on the modality, it may be enabled/disabled as necessary or not to sent to the client.
    For example: there may be components that are necessary only if the ghost is a predicted one (because the client also run the server logic) but that on a interpolated ghost may be completely useless (because no simulation is running).
    The InterpolatedClient, PredictedClient allow you to specify for witch ghost mode the component should be serialized to the client and/or present on the entity. Based on the configuration, the component can be stripped from the prefab and or removed after a ghost is spawned.

    If the ghost is predicted, it present a PredictedGhostComponent. The prediction logic is usually the same the server already does, and must be inside the PredictedGhostSystemGroup.
    Note also that on the server there is no distinction in between predicted or interpolated. All ghosts are "predicted", meaning that they have a PredictedGhostSystemGroup are all simulated inside the PredictedGhostSystemGroup.
     
  5. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    398
    So if I make my Default Ghost Mode Owner Predicted, then make the GhostComponent InterpolatedClient, then the component will be stripped during conversion and will not exist on the owner client and the server at runtime, but will exist on other clients since they interpolate it, right?

    If I add the stripped component at runtime somehow, will it stay or the NetCode will strip it again?
     
  6. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    894
    NetCode does not support adding / removing component at runtime (they will not be synced). And yes, if you add / re-add a component it will not be stripped.
     
  7. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,129
    @CMarastoni From what I know future Entities version will have enable/disable component feature which I believe able to implement into Netcode to make it sync at runtime. I would like to know which Netcode version is planned to deliver this feature?
     
  8. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    894
    We are planning to use that feature in the future for disabling components and simplify other bits. In particular also, we are planning to use enable bits for replicating tags. We don't have any reasonable timeline/roadmap for that yet (since the enable bit feature is still in development).
     
    Lukas_Kastern and optimise like this.
  9. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    Hmm, I didn't realize that you can't remove and add components for ghost prefabs.

    So the best thing here is to create components that have an "isEnabled" or something along those lines and modify that information across the network?