Skip to content

Commit bb256e7

Browse files
authored
Minor doc updates (#439)
* Document visibility better - Move example to the component. - Clarify that it's a component for replicated clients. * Update documentation on identifiers Some backends don't have IDs that are persistent across reconnects. * Clarify that `ClientEntityMap` inserted only after replication is started. * Clarify that not all components are present on connected clients * Clarify how to run examples
1 parent d07c7b1 commit bb256e7

6 files changed

Lines changed: 55 additions & 45 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818

1919
### Changed
2020

21-
- Connected clients are now represented as entities with `ConnectedClient` components. Backends are responsible for spawning and despawning entities with this component. `ClientId` is accessible from `ConnectedClient::id` in case you need an identifier that is persistent across reconnects.
21+
- Connected clients are now represented as entities with `ConnectedClient` components. Backends are responsible for spawning and despawning entities with this component. `ClientId` is accessible from `ConnectedClient::id` in case you need to identify which client belongs to which connection.
2222
- Statistics for connected clients now accessible via `ClientStats` component.
2323
- Replicated entities now represented by connected clients with `ReplicatedClient` component.
2424
- To access visibility, use `ClientVisibility` component on replicated entities.

bevy_replicon_example_backend/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ To run an [example](examples) use the following command:
1010
```bash
1111
cargo run -p bevy_replicon_example_backend --example <example name>
1212
```
13+
14+
In all examples, you need to start the server first since connecting via TCP in the Rust standard library is blocking.
15+
You won't have this issue with a real backend.

src/core/connected_client.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ impl ConnectedClient {
4848

4949
/// Returns client ID provided by backend.
5050
///
51-
/// Unlike [`Entity`], it's a persistent identifier that could
52-
/// be used to identify the same client after reconnects.
51+
/// Can be used to identify which client belongs to which connection.
5352
///
5453
/// See also [`ClientIdMap`].
5554
pub fn id(&self) -> ClientId {

src/lib.rs

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ If you need to send information from clients to the server, use
5353
[events](#network-events-and-triggers).
5454
5555
Replication is enabled by default for all connected clients via [`ReplicatedClient`] component.
56-
It can be disabled via [`ServerPlugin::replicate_after_connect`] is set to `false`.
56+
It can be disabled by setting [`ServerPlugin::replicate_after_connect`] to `false`. Note that
57+
some components on connected clients are only present after replication starts.
58+
See the required components for [`ReplicatedClient`].
5759
5860
For implementation details see [`ReplicationChannel`](core::channels::ReplicationChannel).
5961
@@ -449,45 +451,8 @@ basis.
449451
You can control which parts of the world are visible for each client by setting visibility policy
450452
in [`ServerPlugin`] to [`VisibilityPolicy::Whitelist`] or [`VisibilityPolicy::Blacklist`].
451453
452-
In order to set which entity is visible, you need to use the [`ClientVisibility`] component
453-
on replicated clients.
454-
455-
```
456-
# use bevy::prelude::*;
457-
# use bevy_replicon::prelude::*;
458-
# let mut app = App::new();
459-
app.add_plugins((
460-
MinimalPlugins,
461-
RepliconPlugins.set(ServerPlugin {
462-
visibility_policy: VisibilityPolicy::Whitelist, // Makes all entities invisible for clients by default.
463-
..Default::default()
464-
}),
465-
))
466-
.add_systems(Update, update_visibility.run_if(server_running));
467-
468-
/// Disables the visibility of other players' entities that are further away than the visible distance.
469-
fn update_visibility(
470-
mut clients: Query<&mut ClientVisibility>,
471-
moved_players: Query<(&Transform, &PlayerOwner), Changed<Transform>>,
472-
other_players: Query<(Entity, &Transform, &PlayerOwner)>,
473-
) {
474-
for (moved_transform, &owner) in &moved_players {
475-
let mut visibility = clients.get_mut(*owner).unwrap();
476-
for (entity, transform, _) in other_players
477-
.iter()
478-
.filter(|(.., &other_owner)| *other_owner != *owner)
479-
{
480-
const VISIBLE_DISTANCE: f32 = 100.0;
481-
let distance = moved_transform.translation.distance(transform.translation);
482-
visibility.set_visibility(entity, distance < VISIBLE_DISTANCE);
483-
}
484-
}
485-
}
486-
487-
/// Points to client entity.
488-
#[derive(Component, Deref, Clone, Copy)]
489-
struct PlayerOwner(Entity);
490-
```
454+
To set which entity is visible, you need to use the [`ClientVisibility`] component
455+
on replicated clients (not to be confused with replicated entities).
491456
492457
Check also the [corresponding section](https://github.com/projectharmonia/bevy_replicon#visibility)
493458
in our README for more high-level abstractions.

src/server/client_entity_map.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ be inserted into the client's [`ServerEntityMap`](crate::core::server_entity_map
1818
to propagate the mappings ensures any replication messages related to the pre-mapped
1919
server entities will synchronize with updating the client's [`ServerEntityMap`](crate::core::server_entity_map::ServerEntityMap).
2020
21-
### Example:
21+
It's a required component for [`ReplicatedClient`](super::ReplicatedClient). So if you want to map entities before enabling
22+
replication, you need to insert this component, already filled with entities.
23+
24+
### Examples
2225
2326
```
2427
use bevy::prelude::*;

src/server/client_visibility.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,47 @@ use super::VisibilityPolicy;
88

99
/// Entity visibility settings for a client.
1010
///
11-
/// Present on connected clients after [`ReplicatedClient`](super::ReplicatedClient) insertion.
11+
/// Dynamically marked as required for [`ReplicatedClient`](super::ReplicatedClient)
12+
/// based on the value from [`ServerPlugin::visibility_policy`](super::ServerPlugin::visibility_policy).
13+
///
14+
/// # Examples
15+
///
16+
/// ```
17+
/// # use bevy::prelude::*;
18+
/// # use bevy_replicon::prelude::*;
19+
/// # let mut app = App::new();
20+
/// app.add_plugins((
21+
/// MinimalPlugins,
22+
/// RepliconPlugins.set(ServerPlugin {
23+
/// visibility_policy: VisibilityPolicy::Whitelist, // Makes all entities invisible for clients by default.
24+
/// ..Default::default()
25+
/// }),
26+
/// ))
27+
/// .add_systems(Update, update_visibility.run_if(server_running));
28+
///
29+
/// /// Disables the visibility of other players' entities that are further away than the visible distance.
30+
/// fn update_visibility(
31+
/// mut clients: Query<&mut ClientVisibility>,
32+
/// moved_players: Query<(&Transform, &PlayerOwner), Changed<Transform>>,
33+
/// other_players: Query<(Entity, &Transform, &PlayerOwner)>,
34+
/// ) {
35+
/// for (moved_transform, &owner) in &moved_players {
36+
/// let mut visibility = clients.get_mut(*owner).unwrap();
37+
/// for (entity, transform, _) in other_players
38+
/// .iter()
39+
/// .filter(|(.., &other_owner)| *other_owner != *owner)
40+
/// {
41+
/// const VISIBLE_DISTANCE: f32 = 100.0;
42+
/// let distance = moved_transform.translation.distance(transform.translation);
43+
/// visibility.set_visibility(entity, distance < VISIBLE_DISTANCE);
44+
/// }
45+
/// }
46+
/// }
47+
///
48+
/// /// Points to client entity.
49+
/// #[derive(Component, Deref, Clone, Copy)]
50+
/// struct PlayerOwner(Entity);
51+
/// ```
1252
#[derive(Component)]
1353
pub struct ClientVisibility {
1454
/// Wrapped enum to make its fields private.

0 commit comments

Comments
 (0)