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

Support custom ticks from FixedUpdate games #68

Merged
merged 29 commits into from
Oct 2, 2023
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
af6a5d1
Make changes to NetworkTick, now a Resource, trigger sends.
RJ Oct 2, 2023
4ae3339
remove unused derive_more dep
RJ Oct 2, 2023
61ba5ed
add custom deserialization with NetworkTick test
RJ Oct 2, 2023
bfae493
typos and tweaks
RJ Oct 2, 2023
0076a0a
Bring derive_more back
Shatur Oct 2, 2023
001865f
Remove Default for ReplicationPlugins
Shatur Oct 2, 2023
feeb463
Return `Result<Option<NetworkTick>>` from `deserialize_tick`
Shatur Oct 2, 2023
5c0d5c3
Reorder imports
Shatur Oct 2, 2023
7bdae96
Put network tick increment inside `ServerPlugin` for consistency
Shatur Oct 2, 2023
26287c9
Rename `ServerTicks` into `AckedTicks`
Shatur Oct 2, 2023
f8413f9
Rename despawn_entity_fn into despawn_fn
Shatur Oct 2, 2023
314f3bc
Rework despawn API
Shatur Oct 2, 2023
2dc568f
Use singular
Shatur Oct 2, 2023
b50fc31
Add removal function to `replicate_with` and remove helpers
Shatur Oct 2, 2023
d4a6a7d
Remove tests for custom removals
Shatur Oct 2, 2023
6f818c0
Use dedicated newtype resource for current tick
Shatur Oct 2, 2023
11ce556
Update docs
Shatur Oct 2, 2023
bb6ebd5
Merge branch 'master' into user_tick
Shatur Oct 2, 2023
44ef227
Reduce diffs
Shatur Oct 2, 2023
9e22d9c
Better docs
Shatur Oct 2, 2023
f68b2a2
Use more consistent naming
Shatur Oct 2, 2023
bbd142a
Merge branch 'master' into user_tick
Shatur Oct 2, 2023
ef5b664
Use `NetworkTick` as resource directly
Shatur Oct 2, 2023
18b8bc4
Fix doctests
Shatur Oct 2, 2023
bb9d25e
Fix formatting
Shatur Oct 2, 2023
907cfc2
Fix typo
Shatur Oct 2, 2023
9c5c7c6
Fix doctests
Shatur Oct 2, 2023
108c817
Apply doc suggestions [skip ci]
Shatur Oct 2, 2023
27f0084
Apply suggestions from @UkoeHB
Shatur Oct 2, 2023
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
Prev Previous commit
Next Next commit
Use NetworkTick as resource directly
Shatur committed Oct 2, 2023

Unverified

This user has not yet uploaded their public signing key.
commit ef5b6642c53acafe9f66023e2ad40562990459b5
8 changes: 3 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -135,10 +135,10 @@ you can insert [`Ignored<T>`] component and replication will be skipped for `T`.

### NetworkTick, and fixed timestep games.

The [`ServerPlugin`] sends replication data in `PostUpdate` any time the [`CurrentTick`] resource
The [`ServerPlugin`] sends replication data in `PostUpdate` any time the [`NetworkTick`] resource
changes. By default, its incremented in `PostUpdate` per the [`TickPolicy`].
RJ marked this conversation as resolved.
Show resolved Hide resolved

If you set [`TickPolicy::Manual`], you can increment [`CurrentTick`] at the start of your
If you set [`TickPolicy::Manual`], you can increment [`NetworkTick`] at the start of your
`FixedTimestep` game loop. This value can represent your simulation step, and is made available
to the client in the custom deserialization, despawn and component removal functions.

@@ -398,9 +398,7 @@ pub mod prelude {
},
NetworkChannels, NetworkTick, RepliconCorePlugin,
},
server::{
has_authority, AckedTicks, CurrentTick, ServerPlugin, ServerSet, TickPolicy, SERVER_ID,
},
server::{has_authority, AckedTicks, ServerPlugin, ServerSet, TickPolicy, SERVER_ID},
ReplicationPlugins,
};
}
2 changes: 1 addition & 1 deletion src/replicon_core.rs
Original file line number Diff line number Diff line change
@@ -78,7 +78,7 @@ fn channel_configs(channels: &[SendType]) -> Vec<ChannelConfig> {
/// This is mapped to the bevy Tick in [`crate::server::AckedTicks`].
///
/// See also [`crate::server::TickPolicy`].
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Hash, PartialEq, Resource, Serialize)]
pub struct NetworkTick(u32);

impl NetworkTick {
18 changes: 7 additions & 11 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ impl Plugin for ServerPlugin {
DespawnTrackerPlugin,
))
.init_resource::<AckedTicks>()
.init_resource::<CurrentTick>()
.init_resource::<NetworkTick>()
.configure_set(
PreUpdate,
ServerSet::Receive.after(NetcodeServerPlugin::update_system),
@@ -61,7 +61,7 @@ impl Plugin for ServerPlugin {
PostUpdate,
ServerSet::Send
.before(NetcodeServerPlugin::send_packets)
.run_if(resource_changed::<CurrentTick>()),
.run_if(resource_changed::<NetworkTick>()),
)
.add_systems(
PreUpdate,
@@ -98,8 +98,8 @@ impl ServerPlugin {
}

/// Increments current server tick which causes the server to send a diff packet this frame.
pub fn increment_tick(mut current_tick: ResMut<CurrentTick>) {
current_tick.increment();
pub fn increment_tick(mut network_tick: ResMut<NetworkTick>) {
network_tick.increment();
}

fn acks_receiving_system(mut acked_ticks: ResMut<AckedTicks>, mut server: ResMut<RenetServer>) {
@@ -143,13 +143,13 @@ impl ServerPlugin {
mut set: ParamSet<(&World, ResMut<RenetServer>, ResMut<AckedTicks>)>,
replication_rules: Res<ReplicationRules>,
despawn_tracker: Res<DespawnTracker>,
current_tick: Res<CurrentTick>,
network_tick: Res<NetworkTick>,
removal_trackers: Query<(Entity, &RemovalTracker)>,
) -> Result<(), bincode::Error> {
let mut acked_ticks = set.p2();
acked_ticks.register_network_tick(**current_tick, change_tick.this_run());
acked_ticks.register_network_tick(*network_tick, change_tick.this_run());

let buffers = prepare_buffers(&mut buffers, &acked_ticks, **current_tick)?;
let buffers = prepare_buffers(&mut buffers, &acked_ticks, *network_tick)?;
collect_changes(
buffers,
set.p0(),
@@ -391,10 +391,6 @@ pub enum TickPolicy {
Manual,
}

/// Stores current server [`NetworkTick`].
#[derive(Default, Deref, DerefMut, Resource)]
pub struct CurrentTick(NetworkTick);

/// Stores information about ticks.
///
/// Used only on server.