Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

[Netcode] How does a GhostCollection match ghost types?

Discussion in 'NetCode for ECS' started by edalbeci, Jan 22, 2020.

  1. edalbeci


    Jan 21, 2018
    How does a GhostCollection match ghost types? The GhostCollection docs don't seem to say. I'm having trouble reverse engineering (or finding) the criteria used for a GhostCollection when matching ghost types. Here's what I think are the most insightful case, with a vanilla unity project:

    I created 3 cube prefabs, each with increasing x scale so I could tell them visually apart. Cube1 has physics components, Cube 2 has a TestComponent with a synchronized [GhostDefaultField], and Cube3 has both. I instantiate all three cubes in succession, in a system:

    Code (CSharp):
    1.                 var ghostCollection = GetSingleton<GhostPrefabCollectionComponent>();
    2.                 var cube1GhostId = TinyGhostSerializerCollection.FindGhostType<Cube1SnapshotData>();
    3.                 var cube1Prefab = EntityManager.GetBuffer<GhostPrefabBuffer>(ghostCollection.serverPrefabs)[cube1GhostId].Value;
    4.                 EntityManager.Instantiate(component.prefabCube1);
    However, despite these three entities having different archetypes, a GhostCollection with the prefabs in order -- Cube1, Cube2, Cube3 -- doesn't work. All three instantiated cubes are the same (Cube1). However, if I reverse the order -- Cube3, Cube2, Cube1 -- this works. All three instantiated cubes are different. This begs a few more specific questions:
    • Are archetypes used for matching?
    • Are components without any GhostDefaultFields checked when matching?
    • Are components with GhostDefaultFields checked when matching?
  2. Orimay


    Nov 16, 2012
    Archetypes are used for matching :c
    Serializers are looked up per chunk, so they could use SharedComponentData for this purpose (to find the matching prefab), but they don't. I managed to handle this manually. Here's how:
    DOTS NetCode: MMO locations, portals, items
  3. timjohansson


    Unity Technologies

    Jul 13, 2016
    Yes, the serailizer will go over the list of ghost types in the order they are defined in the collection. For each type if checks if the current entity has all the components the ghost type needs. It does this matching per chunk, based on the archetype owning the chunk. If it does have all the components, if picks that type. This means the order in the collection matters.
    We will probably change this to be more robust in the future.

    It will check for all components which have the "server" checkbox checked, regardless of if they have any serialized data or not.