Replication
You can use the Replicate
bundle to initiate replicating an entity from the local World
to the remote World
.
It is composed of multiple smaller components that each control an aspect of replication:
ReplicationTarget
to decide who to replicate toVisibilityMode
to enable interest managementControlledBy
so the server can track which entity is owned by each clientReplicationGroup
to know which entity updates should be sent together in the same messageReplicateHierarchy
to control if the children of an entity should also be replicatedDisabledComponent<C>
to disable replication for a specific componentReplicateOnceComponent<C>
to specify that some components should not replicate updates, only inserts/removalsOverrideTargetComponent<C>
to override the replication target for a specific component
By default, every component in the entity that is part of the ComponentRegistry
will be replicated. Any changes in
those components will be replicated.
However the entity state will always be 'consistent': the remote entity will always contain the exact same combination
of components as the local entity, even if it's a bit delayed.
You can remove the ReplicationTarget
component to pause the replication. This can be useful when you want to despawn the
entity on the server without replicating the despawn.
(e.g. an entity can be despawned immediately on the server, but needs to remain alive on the client to play a dying
animation)
You can find some of the other usages in the advanced_replication section.
Replicating resources
You can also replicate bevy Resources
. This is useful when you want to update a Resource
on the server and keep synced
copies on the client. This only works for Resources
that also implement Clone
, and should be limited to resources which are cheap to clone.
- Then, to replicate a
Resource
, you can use thecommands.replicate_resource::<R>(replicate)
method. You will need to provide an instance of theReplicate
struct to specify how the replication should be done (e.g. to which clients should the resource be replicated). To stop replicating aResource
, you can use thecommands.stop_replicate_resource::<R>()
method. Note that this won't delete the resource from the client, but it will stop updating it.