Class GhostBehaviour
The base class for all Monobehaviours in need to access Ghost features.
Inherited Members
Namespace: Unity.Netcode
Assembly: Unity.NetCode.dll
Syntax
public abstract class GhostBehaviour : MonoBehaviour
Properties
CanWriteState
Check to make sure you can write to your ghost's data. Should be true when on the authority (server) or when predicting on any client. This can be on your local owner client or on other clients predicting you.
Declaration
public bool CanWriteState { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Ghost
Access to the Ghost for this GhostBehaviour. Only valid after the object is fully spawned and initialized. This is the "bridge" between GameObject and entities and the main point of access for Netcode features.
Declaration
public GhostObject Ghost { get; }
Property Value
| Type | Description |
|---|---|
| GhostObject |
IsClient
Whether this ghost lives in a client world.
Declaration
public bool IsClient { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsServer
Whether this ghost lives in a server world.
Declaration
public bool IsServer { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
Awake()
Unity message that registers this behaviour with its GhostObject. Overrides must call the base implementation.
Declaration
public virtual void Awake()
GetComponentTypes()
Returns the ECS component types backing this behaviour's ghost data. Overridden by source generators.
Declaration
protected virtual IEnumerable<ComponentType> GetComponentTypes()
Returns
| Type | Description |
|---|---|
| IEnumerable<ComponentType> | The component types to add to the ghost entity. |
InitializeComponentRef<T>(ref GhostComponentRef<T>, bool)
Initializes the given GhostComponentRef. This is called by source generators and shouldn't be called manually
Declaration
protected void InitializeComponentRef<T>(ref GhostComponentRef<T> field, bool withInitialValue) where T : unmanaged, IComponentData
Parameters
| Type | Name | Description |
|---|---|---|
| GhostComponentRef<T> | field | The component reference to initialize. |
| bool | withInitialValue | determines if Initialization should use the serialized InitialValue |
Type Parameters
| Name | Description |
|---|---|
| T | The component type referenced. |
InitializeGhostField<T>(ref GhostField<T>, ComponentType, bool)
Initializes the given GhostField. This is called by source generators and shouldn't be called manually
Declaration
protected void InitializeGhostField<T>(ref GhostField<T> field, ComponentType type, bool withInitialValue) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| GhostField<T> | field | The field to initialize. |
| ComponentType | type | The generated component type backing the field. |
| bool | withInitialValue | determines if Initialization should use the serialized InitialValue |
Type Parameters
| Name | Description |
|---|---|
| T | The unmanaged value type of the field. |
InitializeGhostInput<T>(ref GhostInput<T>, bool)
Initializes the given GhostInput. This is called by source generators and shouldn't be called manually
Declaration
protected void InitializeGhostInput<T>(ref GhostInput<T> field, bool withInitialValue) where T : unmanaged, IInputComponentData
Parameters
| Type | Name | Description |
|---|---|---|
| GhostInput<T> | field | The input to initialize. |
| bool | withInitialValue | determines if Initialization should use the serialized InitialValue |
Type Parameters
| Name | Description |
|---|---|
| T | The input component type. |
InitializeRuntimeGameObject(bool)
Links this behaviour's ghost data to the runtime entity. Overridden by source generators.
Declaration
protected virtual void InitializeRuntimeGameObject(bool withInitialValue)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | withInitialValue | Whether to apply the serialized initial values to the entity components. |
OnGhostDespawn()
Invoked when the GhostObject gets despawned or destroyed. This method runs both client and server side.
Declaration
public virtual void OnGhostDespawn()
OnGhostSpawn()
Invoked when the GhostObject gets spawned, the related Entity is linked, and the Network has finished being set-up.
Declaration
public virtual void OnGhostSpawn()
Remarks
Invoked before this GhostBehaviour's Awake(): the GhostObject spawns the ghost from its own Awake, which runs first. When DelaySpawning is set to true, OnGhostSpawn() will be invoked when Spawn() is called. GhostId is already assigned here, except for client predicted spawns.
PredictionGatherInput(PredictionGatherInputContext)
Override this method to gather your inputs for prediction.
Declaration
public virtual void PredictionGatherInput(PredictionGatherInputContext context)
Parameters
| Type | Name | Description |
|---|---|---|
| PredictionGatherInputContext | context | Context for the input gathering of the current frame. |
Remarks
This runs in your frame from the GhostInputSystemGroup group.
Declare your input as a GhostInput<TInput> field and follow its PrepareToCapture() / Capture(in TInput) flow. See GhostInput<TInput> for the details.
Here are some reasons why gathering inputs from this method is recommended:
- This method will only get called on clients and on the ghosts the client owns, before the prediction loop. This ensures there's minimal latency between when an input is detected and the associated network command is sent to the server. It also helps reduce the potential for mispredictions.
- Prediction will modify your inputs to adapt to the current tick it's simulating when rolling back and replaying. You can't gather inputs from prediction.
- If your owner switches, that owner switch gets applied in a system executing before input gathering. If you try to update your inputs from Update() (which executes before all SimulationGroup systems), you'd get a value for GhostOwnerIsLocal that's not updated yet, but will be for prediction when it runs. Same for new spawns.
- With forced input latency (ForcedInputLatencyTicks) or without partial ticks (FullTicksOnly, the default), Netcode writes the latest input stored in the command buffer back into your input struct right before this method, so changes made outside of this method are overwritten.
PredictionPhysicsUpdate(PredictionPhysicsUpdateContext)
Override this to implement predicted logic that interacts with predicted physics.
Declaration
public virtual void PredictionPhysicsUpdate(PredictionPhysicsUpdateContext context)
Parameters
| Type | Name | Description |
|---|---|---|
| PredictionPhysicsUpdateContext | context | Context for predicted physics. |
PredictionUpdate(PredictionUpdateContext)
Override this to implement your own prediction logic.
To access current predicted deltaTime, use context.
To access other time related information, use Time.
See "Introduction to Prediction" and other various articles in the manual for more details on prediction.
Declaration
public virtual void PredictionUpdate(PredictionUpdateContext context)
Parameters
| Type | Name | Description |
|---|---|---|
| PredictionUpdateContext | context | Context for the predicted tick being simulated. |
Remarks
With FullTicksOnly this is only called on full ticks, like the rest of the PredictedSimulationSystemGroup. Visual smoothness between ticks is then handled by FullTickSmoothing, the same smoothing a single-world host uses. Enabling partial ticks project wide (PartialTickModeSelection) also calls this on partial ticks (NetworkTime.IsPartialTick), with a TickedDeltaTime that is a fraction of the tick. Full ticks only does not mean TickedDeltaTime is constant: with prediction step batching (MaxPredictionStepBatchSizeRepeatedTick), rollback resimulation can batch multiple ticks into a single call, with a delta time that is a multiple of the simulation tick delta time.
TearDownComponentRef<T>(ref GhostComponentRef<T>)
Tears down the given GhostComponentRef. This is called by source generators and shouldn't be called manually
Declaration
protected void TearDownComponentRef<T>(ref GhostComponentRef<T> field) where T : unmanaged, IComponentData
Parameters
| Type | Name | Description |
|---|---|---|
| GhostComponentRef<T> | field | The component reference to tear down. |
Type Parameters
| Name | Description |
|---|---|
| T | The component type referenced. |
TearDownGhostField<T>(ref GhostField<T>)
Tears down the given GhostField. This is called by source generators and shouldn't be called manually
Declaration
protected void TearDownGhostField<T>(ref GhostField<T> field) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| GhostField<T> | field | The field to tear down. |
Type Parameters
| Name | Description |
|---|---|
| T | The unmanaged value type of the field. |
TearDownGhostInput<T>(ref GhostInput<T>)
Tears down the given GhostInput. This is called by source generators and shouldn't be called manually
Declaration
protected void TearDownGhostInput<T>(ref GhostInput<T> field) where T : unmanaged, IInputComponentData
Parameters
| Type | Name | Description |
|---|---|---|
| GhostInput<T> | field | The input to tear down. |
Type Parameters
| Name | Description |
|---|---|
| T | The input component type. |
TearDownRuntimeGameObject()
Unlinks this behaviour's ghost data from the runtime entity. Overridden by source generators.
Declaration
protected virtual void TearDownRuntimeGameObject()