Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not send removals for hidden entities #370

Merged
merged 6 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- RTT, bytes per second and packet loss information for `RepliconClient` and `ConnectedClients`.
- `ClientSet::Diagnostics` for systems that collect client diagnostics.

### Fixed

- Sending removals and despawns for hidden entities.

### Changed

- Make `core::replication::replication_rules::ReplicationRules` public.
Expand Down
19 changes: 13 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,12 @@ impl ServerPlugin {
&mut replicated_clients,
&mut set.p5(),
)?;
collect_removals(&mut messages, &mut serialized, &removal_buffer)?;
collect_removals(
&mut messages,
&mut serialized,
&replicated_clients,
&removal_buffer,
)?;
collect_changes(
&mut messages,
&mut serialized,
Expand Down Expand Up @@ -420,8 +425,7 @@ fn collect_despawns(
for entity in despawn_buffer.drain(..) {
let entity_range = serialized.write_entity(entity)?;
for ((message, _), client) in messages.iter_mut().zip(replicated_clients.iter_mut()) {
let visibility = client.visibility().state(entity);
if visibility != Visibility::Hidden {
if client.visibility().is_visible(entity) {
message.add_despawn(entity_range.clone());
}
client.remove_despawned(entity);
Expand All @@ -442,14 +446,17 @@ fn collect_despawns(
fn collect_removals(
messages: &mut ReplicationMessages,
serialized: &mut SerializedData,
replicated_clients: &ReplicatedClients,
removal_buffer: &RemovalBuffer,
) -> bincode::Result<()> {
for (&entity, remove_ids) in removal_buffer.iter() {
let entity = serialized.write_entity(entity)?;
let entity_range = serialized.write_entity(entity)?;
let ids_len = remove_ids.len();
let fn_ids = serialized.write_fn_ids(remove_ids.iter().map(|&(_, fns_id)| fns_id))?;
for (message, _) in messages.iter_mut() {
message.add_removals(entity.clone(), ids_len, fn_ids.clone());
for ((message, _), client) in messages.iter_mut().zip(replicated_clients.iter()) {
if client.visibility().is_visible(entity) {
message.add_removals(entity_range.clone(), ids_len, fn_ids.clone());
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions tests/despawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,42 @@ fn after_spawn() {
assert!(client_app.world().entities().is_empty());
}

#[test]
fn hidden() {
let mut server_app = App::new();
let mut client_app = App::new();
for app in [&mut server_app, &mut client_app] {
app.add_plugins((
MinimalPlugins,
RepliconPlugins.set(ServerPlugin {
tick_policy: TickPolicy::EveryFrame,
visibility_policy: VisibilityPolicy::Whitelist, // Hide all spawned entities by default.
..Default::default()
}),
));
}

server_app.connect_client(&mut client_app);

let server_entity = server_app.world_mut().spawn(Replicated).id();

server_app.update();
server_app.exchange_with_client(&mut client_app);
client_app.update();
server_app.exchange_with_client(&mut client_app);

server_app.world_mut().despawn(server_entity);

server_app.update();
server_app.exchange_with_client(&mut client_app);
client_app.update();

assert_eq!(
client_app.world().entities().total_count(),
0,
"client shouldn't spawn or despawn hidden entity"
);
}

#[derive(Component, Deserialize, Serialize)]
struct DummyComponent;
43 changes: 43 additions & 0 deletions tests/removal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,49 @@ fn with_despawn() {
assert!(client_app.world().entities().is_empty());
}

#[test]
fn hidden() {
let mut server_app = App::new();
let mut client_app = App::new();
for app in [&mut server_app, &mut client_app] {
app.add_plugins((
MinimalPlugins,
RepliconPlugins.set(ServerPlugin {
tick_policy: TickPolicy::EveryFrame,
visibility_policy: VisibilityPolicy::Whitelist, // Hide all spawned entities by default.
..Default::default()
}),
))
.replicate::<DummyComponent>();
}

server_app.connect_client(&mut client_app);

let server_entity = server_app
.world_mut()
.spawn((Replicated, DummyComponent))
.id();

server_app.update();
server_app.exchange_with_client(&mut client_app);
client_app.update();
server_app.exchange_with_client(&mut client_app);

server_app
.world_mut()
.entity_mut(server_entity)
.remove::<DummyComponent>();

server_app.update();
server_app.exchange_with_client(&mut client_app);
client_app.update();

assert!(
client_app.world().entities().is_empty(),
"client shouldn't know about hidden entity"
);
}

#[derive(Component, Deserialize, Serialize)]
struct DummyComponent;

Expand Down