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 Creating Ghosts without Prefabs

Discussion in 'NetCode for ECS' started by chefneil, Dec 6, 2022.

  1. chefneil

    chefneil

    Joined:
    Aug 21, 2020
    Posts:
    29
    Hey team,

    I am implementing ECS + NetCode into a cross platform project that includes WebGL. Since WebGL doesn't yet support subscenes (nor does it support the hybrid renderer) I am looking to implement my own systems for synchronizing GameObjects to corresponding Entities.

    This is all fine and I've created a POC that works outside of a NetCode project, but now that I'm bringing it into NetCode I'm hitting the edge of NetCode docs :)

    It seems the docs only explain creating Ghosts with prefabs where NetCode deterministically creates GhostTypes from the prefab GUID. I would like my own way of creating ghosts and setting types but I don't see any APIs for doing this.

    How would I go about:
    1) on InGame RPC server-side, create a Player entity that is a ghost (a cube for example) with my own ID that says this is a Player
    2) client-side sees the creation of this ghost and knows the type so it can create whatever non ECS things are needed for rendering
     
  2. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    You must have a prefab for the ghost which exists on both client and server, but you can create the prefab at runtime by using GhostPrefabCreation.ConvertToGhostPrefab.

    The flow would be
    1) Create an entity with the components you want, this must be done the exact same way on both client and server
    2) Call GhostPrefabCreation.ConvertToGhostPrefab on both client and server
    3) Instantiate the prefab on the server to spawn the player

    The guid for the prefab you create will be a uuid5 of the prefab name, so make sure the name in the config you pass in is unique.

    For client-side creation of GameObjects used for rendering you can use GhostPresentationGameObjects. Before you create the ghost prefab in the previous step you create an entity with just the GhostPresentationGameObjectPrefab pointing to a GameObject to use on the client and a GameObject to use on the server.
    Add a GhostPresentationGameObjectPrefabReference to the entity you pass into ConvertToGhostPrefab where the Prefab field points to the entity with the GhostPresentationGameObjectPrefab.

    Netcode will instantiate / destroy and update the position of the GameObject automatically.
     
    Opeth001 and Occuros like this.
  3. chefneil

    chefneil

    Joined:
    Aug 21, 2020
    Posts:
    29
    Thanks for this - looks like exactly what I need. One point of clarification: when you say "done exactly the same way" what does that mean specifically? Just that components need to be added in the same order and with the same fields? Anything else to look out for?
     
  4. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    I meant that you have to add the exact same set of component to the entity. The order you add the doesn't really matter - but in my experience by far the easiest way to get this right and avoid it going out of sync is to run the exact same code for prefab creation on both client and server
     
  5. chefneil

    chefneil

    Joined:
    Aug 21, 2020
    Posts:
    29
    Ok excellent - another non urgent question: for the GameObject representation, are there any plans to support a pooling strategy?

    I would be nice if I could instead add something like a GhostPresentationGameObjectPoolPrefabReference that takes some sort of IGameObjectPool interface that I can implement.
     
  6. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    It is something we have thought about, but we do not have any concrete plans to add it. We wanted to get some feedback on presentation GameObjects first so we know if we need any other fundamental changes to them.
     
  7. chefneil

    chefneil

    Joined:
    Aug 21, 2020
    Posts:
    29
    Cool - I got everything working! Thanks for your help.

    A note:
    Code (CSharp):
    1. GhostPrefabCreation.ConvertToGhostPrefab(world.EntityManager, e, ghostConfig);
    Errors out on thin clients. I wrapped it in a try catch and it seems to be working fine, but just has error thrown due to a missing singleton.
     
  8. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,068
    It looks like you didn't create the ghost prefab in the thin client world.
     
  9. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,068
    What about components that aren't supposed to exist on the server?
    Like MeshRenderer....
    In our case, we want to make sure that the server doesn't reference assets that will never be used to save memory and performance (costs)