Is there a way to process a Ghost Instance on the server before it is destroyed when the client disconnects?
You would have to listen for the disconnect event on the server:
https://docs.unity3d.com/Packages/com.unity.netcode@1.4/manual/network-connection.html#listening-for-client-connection-events
And then collect/handle what you need at that point.
It doesn’t work because the ghostInstance has already been destroyed before the Disconnected event occurs.
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
[UpdateAfter(typeof(NetworkReceiveSystemGroup))]
[BurstCompile]
public partial struct NetCodeConnectionEventListener : ISystem
{
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var connectionEventsForClient = SystemAPI.GetSingleton<NetworkStreamDriver>().ConnectionEventsForTick;
foreach (var evt in connectionEventsForClient)
{
if( evt.State == ConnectionState.State.Disconnected)
{
Debug.Log("GhostInstanceOnServer " + SystemAPI.QueryBuilder().WithAll<GhostInstance>().Build().CalculateEntityCount());
}
}
}
}
0
Internally we check for the GhostCleanup component. Which is unfortunately internal.
You can try making this public in your own version of the package.
I can check if there is another way to do this using existing public APIs.
The ServerWorld will only automatically delete the owned ghost entity of the disconnecting player if said ghost entity is explicitly added (by user-code) to the LinkedEntityGroup
of the “NetworkConnection” (i.e. NetworkStreamConnection
) entity, so you can simply not add the ghost to the buffer, then manually clean it up yourself. Doing this gives you an opportunity to do your own (additional) processing.