Struct bevy_replicon::server::client_entity_map::ClientEntityMap
source · pub struct ClientEntityMap(/* private fields */);
Expand description
A resource that exists on the server for mapping server entities to
entities that clients have already spawned. The mappings are sent to clients as part of replication
and injected into the client’s ServerEntityMap
.
Sometimes you don’t want to wait for the server to spawn something before it appears on the client – when a client performs an action, they can immediately simulate it on the client, then match up that entity with the eventual replicated server spawn, rather than have replication spawn a brand new entity on the client.
In this situation, the client can send the server its pre-spawned entity id, then the server can spawn its own entity
and inject the ClientMapping
into its ClientEntityMap
.
Replication packets will send a list of such mappings to clients, which will
be inserted into the client’s ServerEntityMap
. Using replication
to propagate the mappings ensures any replication messages related to the pre-mapped
server entities will synchronize with updating the client’s ServerEntityMap
.
§Example:
use bevy::prelude::*;
use bevy_replicon::prelude::*;
#[derive(Event)]
struct SpawnBullet(Entity);
#[derive(Component)]
struct Bullet;
/// System that shoots a bullet and spawns it on the client.
fn shoot_bullet(mut commands: Commands, mut bullet_events: EventWriter<SpawnBullet>) {
let entity = commands.spawn(Bullet).id();
bullet_events.send(SpawnBullet(entity));
}
/// Validation to check if client is not cheating or the simulation is correct.
///
/// Depending on the type of game you may want to correct the client or disconnect it.
/// In this example we just always confirm the spawn.
fn confirm_bullet(
mut commands: Commands,
mut bullet_events: EventReader<FromClient<SpawnBullet>>,
mut entity_map: ResMut<ClientEntityMap>,
) {
for FromClient { client_id, event } in bullet_events.read() {
let server_entity = commands.spawn(Bullet).id(); // You can insert more components, they will be sent to the client's entity correctly.
entity_map.insert(
*client_id,
ClientMapping {
server_entity,
client_entity: event.0,
},
);
}
}
If the client is connected and receives the replication data for the server entity mapping,
replicated data will be applied to the client’s original entity instead of spawning a new one.
You can detect when the mapping is replicated by querying for Added<Replicated>
on your original
client entity.
If client’s original entity is not found, a new entity will be spawned on the client, just the same as when no client entity is provided.
Implementations§
source§impl ClientEntityMap
impl ClientEntityMap
sourcepub fn insert(&mut self, client_id: ClientId, mapping: ClientMapping)
pub fn insert(&mut self, client_id: ClientId, mapping: ClientMapping)
Registers mapping
for a client entity pre-spawned by the specified client.
This will be sent as part of replication data and added to the client’s
ServerEntityMap
.
Trait Implementations§
source§impl Debug for ClientEntityMap
impl Debug for ClientEntityMap
source§impl Default for ClientEntityMap
impl Default for ClientEntityMap
source§fn default() -> ClientEntityMap
fn default() -> ClientEntityMap
source§impl Deref for ClientEntityMap
impl Deref for ClientEntityMap
impl Resource for ClientEntityMap
Auto Trait Implementations§
impl Freeze for ClientEntityMap
impl RefUnwindSafe for ClientEntityMap
impl Send for ClientEntityMap
impl Sync for ClientEntityMap
impl Unpin for ClientEntityMap
impl UnwindSafe for ClientEntityMap
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given World
.