projectharmonia / bevy_replicon

Server-authoritative networking crate for the Bevy game engine.
https://crates.io/crates/bevy_replicon
Apache License 2.0
350 stars 31 forks source link

Allow enabling replication manually #310

Closed aecsocket closed 3 months ago

aecsocket commented 4 months ago

In some cases, you don't want the server to replicate entities to the client at all. For example, if you just started a connection between client and server, you may want to not replicate anything until you send an explicit signal that replication can start. Importantly, events are still synchronized between the client and server, just that replication is not. This can be used to implement, for example, a configuration stage for the connection where the server can send pre-game data to the client (similar to what Minecraft does), and only afterwards start replicating. It should also be possible to start and stop replication at any point in time. On the server side, this could be implemented as a toggleable property on the client.

UkoeHB commented 4 months ago

Should be doable, we'd need to be careful it doesn't mess up change detection.

Shatur commented 3 months ago

Decision

We discussed it in Discord and I think it makes sense to implement a simplified version described below.

Instead of providing a way to enable and disable replication for a client at any time, we can provide a way to disable replication by default and let users decide when they want to start replication. Once replication for a client is enabled, you can't disable it. We basically add an intermediate user-controlled stage between connection and replication.

It will be possible to implement dynamic enabling/disabling for a client at in the future if we decide so. But right now I don't think that there is a use case for it.

Also this requires #313.

API and implementation details

We will need to add addition field to ServerPlugin: https://github.com/projectharmonia/bevy_replicon/blob/899783dae83b408186383e20afaa65631a34f3eb/src/server.rs#L45

Something like replicate_after_connect set to true by default.

Then when we create ConnectedClients resource, we store this parameter inside it: https://github.com/projectharmonia/bevy_replicon/blob/899783dae83b408186383e20afaa65631a34f3eb/src/core/connected_clients.rs#L18

When we create a ConnectedClient, we pass this boolean to new and reset: https://github.com/projectharmonia/bevy_replicon/blob/899783dae83b408186383e20afaa65631a34f3eb/src/core/connected_clients.rs#L175 https://github.com/projectharmonia/bevy_replicon/blob/899783dae83b408186383e20afaa65631a34f3eb/src/core/connected_clients.rs#L224

Now every time we iterate over all clients for replication, we need ignore all clients with disabled replication. Maybe it worth to create a helper that iterate only over clients with replication_enabled == true: https://github.com/projectharmonia/bevy_replicon/blob/899783dae83b408186383e20afaa65631a34f3eb/src/core/connected_clients.rs#L86

We also need to provide a function called ConnectedClient::enable_replication that doesn't accept any parameters and ConnectedClient::is_replication_enabled.

That's it.