lightyear icon indicating copy to clipboard operation
lightyear copied to clipboard

Add a `DespawnBehaviour` component on replicated entities to specify how to despawn them if the sender disconnects

Open cBournhonesque opened this issue 1 year ago • 1 comments

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

  1. have some ad-hoc logic to despawn the resource replication entities when the client disconnects.
  2. Add Confirmed on 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?
  3. have a general component DespawnBehaviour that is added on the client when it receives an entity replicated from the server. The DespawnBehaviour would 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

cBournhonesque avatar Apr 20 '24 06:04 cBournhonesque

Implemented option 2 for now.

cBournhonesque avatar Apr 21 '24 19:04 cBournhonesque

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).

vladbat00 avatar Jun 30 '24 14:06 vladbat00

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

cBournhonesque avatar Jun 30 '24 18:06 cBournhonesque