Add a `DespawnBehaviour` component on replicated entities to specify how to despawn them if the sender disconnects
Context
In most situations, you want to despawn entities that were replicated from a remote if the remote disconnects:
- client disconnects from the game and you want to despawn game entities
- a resource was being replicated from server to client via a replicated entity. If the client disconnects from the server, the replicated entity should be despawned on the client, which should then remove the replicated resource on the client.
Options
- have some ad-hoc logic to despawn the resource replication entities when the client disconnects.
- Add
Confirmedon every replicated entity and despawn any replicated entity (Confirmed/Interpolated/Predicted) when we disconnect from the remote. Also maybe despawn any replicate-entity on the send side if the send side disconnects? - have a general component
DespawnBehaviourthat is added on the client when it receives an entity replicated from the server. TheDespawnBehaviourwould be an enum:
#[derive(Default)]
enum DespawnBehaviour {
#[default]
DespawnIfRemoteDisconnects,
Keep
}
that defines whether or not the entity gets despawned if the remote gets disconnected.
For client->server replication, the entity would get despawned on the server only if the client that owns the entity is disconnected.
In general (for server->client replication), when the client disconnects we don't receive the EntityRemove messages so we don't despawn the entity that were replicated; which is why this is necessary.
Implementation
I think going with option 2 is easier now. The only issue is there might be cases where users might want a replicated entity to not be despawned after we disconnect with the remote; we can switch to option 3 when a user mentions the need for it.
TODO:
- implement this and check that resource replication works correctly after disconnecting and reconnecting
Implemented option 2 for now.
Do we also want to make this configurable on the server side? I think users might want to control despawning entities manually in this case as well (e.g. to allow clients to reconnect and take back control of an entity).
Yes that was the plan. On the cb/0.16 branch I added a Lifetime option https://github.com/cBournhonesque/lightyear/blob/cb/0.16/lightyear/src/server/replication.rs#L181
to configure if the entity is despawned or not when the controlling client is disconnected