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 Spawn procedural object

Discussion in 'NetCode for ECS' started by vildauget, Mar 8, 2023.

  1. vildauget

    vildauget

    Joined:
    Mar 10, 2014
    Posts:
    120
    Hi, any thoughts / ideas on how to spawn a procedural object on server?

    It seems we need to use prefabs. Would it be possible to change the mesh filter and material on the clients after it has spawned, or does that mess up the ghost? If so, maybe it's best to keep the entity as it is, but use ghosted values on it to create a new entity representing the ghost?

    All ideas for procedural content appreciated, to get me on the right path.
     
  2. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    284
    This highly depends on how procedural your content is.

    we synchronize the seed between client and server and then generate the procedural mesh + collider on the client and server side.

    (well the server only needs the mesh for the collider generation, not visualization)
     
    WAYNGames likes this.
  3. vildauget

    vildauget

    Joined:
    Mar 10, 2014
    Posts:
    120
    Yes, that's exactly what I'd like to do, as well. Are your seeds ghosts based on prefab? Do you edit that ghost on the server and the client (without sync), or do you use the values from the ghost to create and sync local entities?

    To make clear what I'm struggling with, it seems to me that everything is based on hardcoded, saved prefabs, and I'm trying to figure out best way to go around that to keep things dynamic.
     
  4. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    284
    We regenerate everything on client and server based on seed, the performance is even that good that we can do it at runtime without dropping any frames.

    So yes we implemented it in a way that we sync the seed, then generate all the meshes and colliders.

    So as long as you can guarantee that everything is done deterministically and exactly the same on client and server, you should be fine (even with physics etc.).

    So we spawn the same ghost entity on client and server, but then modify the mesh and collider in a deterministic way in all worlds, the rest of the state (Positions, abilities etc.) are all handled through normal netcode.

    Hope that helps, otherwise just let me know.
     
    vildauget likes this.
  5. vildauget

    vildauget

    Joined:
    Mar 10, 2014
    Posts:
    120
    Thank you so much, @Occuros . This is just the encouragement and path I needed to dive into the gritty details of things.
     
    Occuros likes this.
  6. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    774
    mesh and material can be changed on the client without problem. Technically you can have a presentation on the client and on the server any (all meshes and graphics data are not present).
    Adding component is also valid, but they will be not replicated.
    If you need to create ghost prefab procedurally then you need to use the GhostPrefabCreation api to generate them on the fly (need to be done on both client and server identical)
     
    vildauget likes this.
  7. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    774
    I would add though that collider etc should be still "compatible". So for example: you may have a different visual but the let's say bounding box collider should be the same size. So collision and eventual physics simulation are very similar
     
  8. vildauget

    vildauget

    Joined:
    Mar 10, 2014
    Posts:
    120
    Yes, thank you, that makes sense.

    Your reference to GhostPrefabCreation api is key here, wonderful - and it also led me to this thread where it's discussed some more, https://forum.unity.com/threads/creating-ghosts-without-prefabs.1370169/