How and when to use GhostObjects
Compare GhostObjects with the entity-based ghost workflow so you can choose the right approach for your project.
GhostObjects and the entity-based ghost workflow are based on the same Netcode core. Both have the same networking features, such as server authority, prediction, interpolation, ghost fields, and remote procedure calls (RPCs). What differs is the way you access those features: through GameObjects and MonoBehaviours, or through entities, components, and systems.
Choosing between GhostObjects and the entity-based workflow is therefore the same choice as choosing between GameObjects and the entity component system (ECS). Use GhostObjects for the same reasons you use GameObjects, and use the entity-based workflow for the same reasons you use ECS. You don't have to choose only one, because the two approaches coexist in the same project.
For an introduction to GhostObjects and their building blocks, refer to Introduction to GhostObjects.
Understand the shared replication pipeline
When you author an object with GhostObjects, Netcode still creates a ghost entity for it and replicates it through the same systems that drive entity ghosts, including snapshot serialization, the prediction loop, and interpolation. As a result, the networking behavior is identical: the server has authority, clients receive snapshots, and objects can be predicted or interpolated in exactly the same way.
The difference is how you author and script the object, and how much work happens each frame to keep the GameObject and its ghost entity in sync. This is what the rest of this page compares.
Benefits of GhostObjects
GhostObjects make networked development approachable for GameObject workflows:
- No entities code required: You add a GhostObject component, declare replicated state with
GhostField<T>, and write logic inGhostBehaviourmethods such asPredictionUpdate. You don't write systems, bakers, or entity queries. - Familiar patterns: The GhostObject component works like a Rigidbody component: add it to make an object networked, and use it as the single access point for data inside your
GhostBehaviourscripts. This is similar to theNetworkObjectandNetworkBehaviourpattern in other networking solutions, which makes it easier to discover and adopt. - Faster to learn and iterate: Teams that already work with GameObjects and MonoBehaviours can build networked gameplay without first learning ECS.
- Optimization with ECS when you need it: You can still use ECS. A
GhostComponentRefexposes a plain component that both yourGhostBehaviourand a Burst-compiled system can read and write, so you can optimize performance-critical paths without leaving GhostObjects.
Benefits of the entity-based workflow
The entity-based workflow is the lower-level API and remains the best choice when performance and control matter most:
- Peak performance and scale: The entity-based workflow is built on ECS, so it benefits from the Burst compiler, the job system, and chunk-packed data. It scales to large numbers of ghosts more efficiently.
- No per-object bridging overhead: GhostObjects synchronize each GameObject's transform with its ghost entity every frame and call your
GhostBehaviourmethods from inside the prediction loop. Many of those operations can't use the Burst compiler, so you lose some performance. The entity-based workflow avoids this managed overhead and is fully Burst-compiled.
Combine both approaches
Because both approaches use the same replication pipeline, you can mix them in the same project and even the same world. You might author most objects as GhostObjects for development speed, keep performance-critical objects as entity ghosts, and share components between GameObject and ECS code with GhostComponentRef. To adopt GhostObjects in a project that already uses entity ghosts, refer to Hybrid GhostObjects and entities in the same project.
For example, a game has 10,000 mobs, each with a replicated health value, and any mob that moves out of bounds loses all its health. Instead of a GhostField<int>, each mob's GhostBehaviour declares a GhostComponentRef<MobHealth>. A Burst-compiled system then checks the positions of all the mobs in one job and sets their health, while a MyCharacter script heals a single mob through the same component:
// reused between GameObject and ECS flows
[Serializable]
public struct MobHealth : IComponentData
{
[GhostField] public int Value;
}
// Each mob GameObject shares its health with ECS code through a GhostComponentRef.
public partial class Mob : GhostBehaviour
{
public GhostComponentRef<MobHealth> Health;
public void Heal(int quantiy)
{
MobHealth health = Health.Read();
health.Value += quantiy;
Health.Write(health);
}
}
// GameObject code changes a single mob's health through the same component.
public partial class MyCharacter : GhostBehaviour
{
public void Heal(Mob mob, int amount)
{
mob.Heal(amount);
}
}
// A Burst-compiled system checks every mob at once and sets the health of mobs out of bounds to 0.
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial struct MobOutOfBoundsSystem : ISystem
{
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
new KillOutOfBoundsJob { MaxDistance = 100f }.ScheduleParallel();
}
[BurstCompile]
partial struct KillOutOfBoundsJob : IJobEntity
{
public float MaxDistance;
void Execute(ref MobHealth health, in LocalTransform transform)
{
if (math.length(transform.Position) > MaxDistance)
{
health.Value = 0;
}
}
}
}
Choose the right approach
The networking features are the same in both approaches, so base your decision on whether GameObjects or ECS suit your project better. These are guidelines, not strict rules, and you can combine both approaches.
| Consider GhostObjects if | Consider the entity-based workflow if |
|---|---|
| Your project uses GameObjects and MonoBehaviours | Your project already uses entities |
| Your team is more familiar with GameObjects than with ECS | Your team is comfortable working with ECS |
| You rely on GameObject-based features, packages, or tools | You need the highest performance and scale |