Skip to content

Introduce BlueprintSet #28

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

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `Blueprint` set to add systems that initialize complement components without one frame delay.

### Changed

- Move `has_authority()` to `server` module.
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ This is easily done using a system with an [`Added`] query filter.
This way, you detect when such entities are spawned into the world, and you can
do any additional setup on them using code. For example, if you have a
character with mesh, you can replicate only your `Player` component and insert
necessary components after replication:
necessary components after replication. Use [`BlueprintSet`] inside [`PreUpdate`] for it:

```rust
# use bevy::prelude::*;
Expand All @@ -151,7 +151,7 @@ necessary components after replication:
app.replicate::<Transform>()
.replicate::<Visibility>()
.replicate::<Player>()
.add_systems(Update, player_init_system);
.add_systems(PreUpdate, player_init_system.in_set(BlueprintSet));

fn player_init_system(
mut commands: Commands,
Expand Down Expand Up @@ -376,7 +376,7 @@ pub mod prelude {
parent_sync::{ParentSync, ParentSyncPlugin},
renet::{RenetClient, RenetServer},
replication_core::{
AppReplicationExt, NetworkChannels, Replication, ReplicationCorePlugin,
AppReplicationExt, BlueprintSet, NetworkChannels, Replication, ReplicationCorePlugin,
ReplicationRules,
},
server::{has_authority, ServerPlugin, ServerSet, TickPolicy, SERVER_ID},
Expand Down
10 changes: 8 additions & 2 deletions src/parent_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy::{
reflect::ReflectMapEntities,
},
prelude::*,
scene,
};

use crate::{
Expand All @@ -21,7 +22,12 @@ impl Plugin for ParentSyncPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Option<Entity>>()
.replicate::<ParentSync>()
.add_systems(PreUpdate, Self::sync_system.after(ClientSet::Receive))
.add_systems(
PreUpdate,
Self::sync_system
.after(scene::scene_spawner_system)
.after(ClientSet::Receive),
)
.add_systems(
PostUpdate,
(Self::update_system, Self::removal_system)
Expand Down Expand Up @@ -177,7 +183,7 @@ mod tests {
}

#[test]
#[ignore = "will work if Bevy move scene spawning to `PreUpdate`"]
#[ignore = "will work when Bevy move scene spawning to `PreUpdate`"] // TODO 0.12: remove ignore.
fn scene_update_sync() {
let mut app = App::new();
app.add_plugins((
Expand Down
17 changes: 16 additions & 1 deletion src/replication_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@ use bevy::{
ecs::{archetype::Archetype, component::ComponentId},
prelude::*,
reflect::GetTypeRegistration,
scene,
utils::{HashMap, HashSet},
};
use bevy_renet::renet::{ChannelConfig, SendType};

use crate::client::ClientSet;

pub struct ReplicationCorePlugin;

impl Plugin for ReplicationCorePlugin {
fn build(&self, app: &mut App) {
app.register_type::<Replication>()
.init_resource::<NetworkChannels>()
.init_resource::<ReplicationRules>();
.init_resource::<ReplicationRules>()
.configure_set(
PreUpdate,
BlueprintSet
.after(ClientSet::Receive)
.after(scene::scene_spawner_system),
);
}
}

/// Systems that runs in `PreUpdate` right after receiving state from server or after scene deserialization.
///
/// Systems that initialize entities with complementary components (such as [`GlobalTransform`]) should be added here to avoid a 1-frame delay.
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub struct BlueprintSet;

pub(super) const REPLICATION_CHANNEL_ID: u8 = 0;

/// A resource to create channels for [`bevy_renet::renet::ConnectionConfig`]
Expand Down