MCPcopy Index your code
hub / github.com/Bendzae/bevy_replicon_snap

github.com/Bendzae/bevy_replicon_snap @v0.2.6

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.2.6 ↗ · + Follow
88 symbols 116 edges 7 files 14 documented · 16% updated 18mo ago★ 382 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

CI crates.io

bevy_replicon_snap

A Snapshot Interpolation plugin for the networking solution bevy_replicon in the Bevy game engine.

This library is a very rough proof of concept and not meant to be used in productive games

Features

  • Basic but customizable snapshot interpolation for replicated components
  • Client-Side prediction:
  • Owner predicted: Owner client of the entity predicts, other clients interpolate

In the examples you can find a clone of the Simple Box example of bevy_replicon, in 3 versions: no interpolation or prediction, interpolated, predicted. I recommend to look at the diffs between those examples to gain a better understanding how this plugin works.

Usage

Setup

Add the bevy_replicon plugin and this plugin to your bevy application.

The plugin needs to know the maximum server tick rate to estimate time between snapshots so it needs to be passed in on initialization:

const MAX_TICK_RATE: u16 = 30;

...

.add_plugins((
    DefaultPlugins,
    RepliconPlugins.build().set(ServerPlugin {
        tick_policy: TickPolicy::MaxTickRate(MAX_TICK_RATE),
        ..default()
    }),
    RepliconRenetPlugins,
    SnapshotInterpolationPlugin {
        max_tick_rate: MAX_TICK_RATE,
    },
))

...

Interpolation

To allow a Component to be interpolated it needs to implement the traits: Interpolate, Serialze and Deserialize.

This lib provides a basic derive macro for Interpolate but for complex types you will have to implement it yourself.

use bevy_replicon_snap_macros::{Interpolate};

#[derive(Component, Deserialize, Serialize, Interpolate, Clone)]
struct PlayerPosition(Vec2);

Next you need to register the component for Interpolation:

app.replicate_interpolated::<PlayerPosition>()

this also registers the component for replication by bevy_replicon.

Last Step is to add the Interpolated Component to any entity that should be interpolated.

commands.spawn((
    PlayerPosition(Vec2::ZERO),
    Replicated,
    Interpolated,
    ...
));

Client-Side Prediction

To use client side prediction you need to implement the Predict trait for any component and event combination to specify how a event would mutate a component. This library will then use this implementation to generate respective server and client systems that take care of predicting changes on client-side and correcting them should the server result be different. The context type T can used to pass in any context needed for the calculation.

impl Predict<MoveDirection, MovementSystemContext> for PlayerPosition {
    fn apply_event(
        &mut self,
        event: &MoveDirection,
        delta_time: f32,
        context: &MovementSystemContext,
    ) {
        self.0 += event.0 * delta_time * context.move_speed;
    }
}

Additionally you need to register the Event as a predicted event aswell as the event and component combination:

app
  .replicate_interpolated::<PlayerPosition>()
  .add_client_predicted_event::<MoveDirection>(ChannelKind::Ordered)
  .predict_event_for_component::<MoveDirection, MovementSystemContext, PlayerPosition>()

Finally, make sure the entities that should be predicted have the OwnerPredicted component:

commands.spawn((
    PlayerPosition(Vec2::ZERO),
    Replicated,
    OwnerPredicted,
    ...
));

Compatitbiliy

bevy bevy_replicon bevy_replicon_snap
0.15.0 0.29 0.26
0.14.0 0.28 0.25

Alternatives

  • bevy_timewarp An awesome predict/rollback library that also has integration with bevy_replicon

Extension points exported contracts — how you extend this code

Predict (Interface)
This trait defines how an event will mutate a given component and is required for prediction. [1 implementers]
src/prediction.rs
AppInterpolationExt (Interface)
(no doc) [1 implementers]
src/interpolation.rs
AppPredictionExt (Interface)
(no doc) [1 implementers]
src/prediction.rs
Interpolate (Interface)
(no doc)
src/interpolation.rs

Core symbols most depended-on inside this repo

insert
called by 4
src/interpolation.rs
insert
called by 3
src/prediction.rs
apply_event
called by 2
examples/owner_predicted.rs
remove_stale
called by 1
src/prediction.rs
predict
called by 1
src/prediction.rs
latest_snapshot
called by 1
src/interpolation.rs
latest_snapshot_tick
called by 1
src/interpolation.rs
build
called by 1
examples/interpolated.rs

Shape

Method 40
Class 28
Function 12
Enum 4
Interface 4

Languages

Rust100%

Modules by API surface

src/interpolation.rs17 symbols
examples/owner_predicted.rs17 symbols
examples/no_interpolation_or_prediction.rs17 symbols
src/prediction.rs16 symbols
examples/interpolated.rs16 symbols
src/lib.rs4 symbols
macros/src/lib.rs1 symbols

For agents

$ claude mcp add bevy_replicon_snap \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact