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

[Netcode] Ghost Creation Detection on client

Discussion in 'NetCode for ECS' started by Neodamus, Aug 11, 2020.

  1. Neodamus

    Neodamus

    Joined:
    Jan 12, 2018
    Posts:
    14
    It seems a fairly standard setup that every time a client joins the server, the server will create a Player ghost, which will cause a ghost to be created on all connected clients.

    I want to detect on client1 when client2 ghost is created and execute some code at that point.

    The simplest solution seems to be having a CommponentSystem update loop to count the number of Player ghosts at all times and detect any changes in count, but..

    Does anyone know of a way that the client can detect when a new Player ghost is created, more like an event, rather than having to constantly check for changes by querying in the update loop?
     
  2. Lukas_Kastern

    Lukas_Kastern

    Joined:
    Aug 31, 2018
    Posts:
    97
    You can create a component that states that the entity has been processed and only query for ghosts that do not have this component.

    After querying just add the component to all the ghosts that you've just processed.
     
    Neodamus and bb8_1 like this.
  3. Neodamus

    Neodamus

    Joined:
    Jan 12, 2018
    Posts:
    14
    Thank you for this, Lukas. Seems like a solution that can be used for many cases in ECS that I wasn't familiar with until now. :)
     
    Lukas_Kastern likes this.
  4. Neodamus

    Neodamus

    Joined:
    Jan 12, 2018
    Posts:
    14
    Just a brief follow up...

    - I've made it so that when a Player ghost is first created on client, I add a "PlayerJoined" tag
    - I then query for any player with the "PlayerJoined" tag in the update loop, and check for when the ghost actually starts to receive snapshots because I've found there are at least a couple frames where the player ghost is created, but not receiving any snapshots, even in local testing.
    - Once snapshots are verified as being received, I remove the "PlayerJoined" tag and add the "PlayerActive" tag, which should be the final state of the player.
     
    Lukas_Kastern and florianhanke like this.
  5. Lukas_Kastern

    Lukas_Kastern

    Joined:
    Aug 31, 2018
    Posts:
    97
    So as long as you do not update your system between the spawning and update system you shouldn't have to check if it's verified.

    Are you setting the values in same frame as your are creating the entity on the server?
     
    Neodamus likes this.
  6. Neodamus

    Neodamus

    Joined:
    Jan 12, 2018
    Posts:
    14
    Thank you for that. Adding this attribute to the system made the extra verification step no longer necessary:
    Code (CSharp):
    1. [UpdateAfter(typeof(GhostSimulationSystemGroup))]