Class NetworkBehaviour
Scripts which inherit from NetworkBehaviour can be used to gain insight of, and perform actions on the network.
Inheritance
Inherited Members
Namespace: FishNet.Object
Assembly: FishNet.Runtime.dll
Syntax
public abstract class NetworkBehaviour : MonoBehaviour
Fields
MAXIMUM_NETWORKBEHAVIOURS
Maximum number of allowed added NetworkBehaviours.
Declaration
public const byte MAXIMUM_NETWORKBEHAVIOURS = 254
Field Value
Type | Description |
---|---|
System.Byte |
UNSET_NETWORKBEHAVIOUR_ID
Id for when a NetworkBehaviour is not valid.
Declaration
public const byte UNSET_NETWORKBEHAVIOUR_ID = 255
Field Value
Type | Description |
---|---|
System.Byte |
Properties
ClientManager
ClientManager for this object.
Declaration
public ClientManager ClientManager { get; }
Property Value
Type | Description |
---|---|
ClientManager |
ComponentIndex
ComponentIndex for this NetworkBehaviour.
Declaration
public byte ComponentIndex { get; }
Property Value
Type | Description |
---|---|
System.Byte |
HasAuthority
True if IsOwner, or if IsServerInitialized with no Owner.
Declaration
[PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "OnStartServer", "")]
[PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "OnStartNetwork", " Use (base.Owner.IsLocalClient || (base.IsServerInitialized && !Owner.Isvalid) instead.")]
[PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "Awake", "")]
[PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "Start", "")]
public bool HasAuthority { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsBehaviourReconciling
True if this NetworkBehaviour is reconciling. If this NetworkBehaviour does not implemnent prediction methods this value will always be false. Value will be false if there is no data to reconcile to, even if the PredictionManager IsReconciling. Data may be missing if it were intentionally not sent, or due to packet loss.
Declaration
public bool IsBehaviourReconciling { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsClient
Declaration
[Obsolete("Use IsClientInitialized. Note the difference between IsClientInitialized and IsClientStarted.")]
public bool IsClient { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsClientInitialized
True if this object has been initialized on the client side. This is set true right before client start callbacks and after stop callbacks.
Declaration
public bool IsClientInitialized { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsClientOnly
Declaration
[Obsolete("Use IsClientOnlyInitialized. Note the difference between IsClientOnlyInitialized and IsClientOnlyStarted.")]
public bool IsClientOnly { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsClientOnlyInitialized
Declaration
public bool IsClientOnlyInitialized { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsClientOnlyStarted
True if only the client is started and authenticated.
Declaration
public bool IsClientOnlyStarted { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsClientStarted
True if the client is started and authenticated.
Declaration
public bool IsClientStarted { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsDeinitializing
True if the NetworkObject for this NetworkBehaviour is deinitializing.
Declaration
public bool IsDeinitializing { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsHost
Declaration
[Obsolete("Use IsHostInitialized. Note the difference between IsHostInitialized and IsHostStarted.")]
public bool IsHost { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsHostInitialized
True if this object has been initialized on the server and client side.
Declaration
public bool IsHostInitialized { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsHostStarted
True if client and server are started.
Declaration
public bool IsHostStarted { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsManagerReconciling
True if a reconcile is occuring on the PredictionManager. Note the difference between this and IsBehaviourReconciling.
Declaration
public bool IsManagerReconciling { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsNetworked
True if the object will always initialize as a networked object. When false the object will not automatically initialize over the network. Using Spawn() on an object will always set that instance as networked. To check if server or client has been initialized on this object use IsXYZInitialized.
Declaration
public bool IsNetworked { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsOffline
True if client nor server are started.
Declaration
public bool IsOffline { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsOwner
True if the local client is the owner of this object.
Declaration
[PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "OnStartServer", "")]
[PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "OnStartNetwork", " Use base.Owner.IsLocalClient instead.")]
[PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "Awake", "")]
[PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "Start", "")]
public bool IsOwner { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsOwnerOrServer
Declaration
[Obsolete("Use HasAuthority")]
public bool IsOwnerOrServer { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsServer
Declaration
[Obsolete("Use IsServerInitialized. Note the difference between IsServerInitialized and IsServerStarted.")]
public bool IsServer { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsServerInitialized
True if this object has been initialized on the server side. This is set true right before server start callbacks and after stop callbacks.
Declaration
public bool IsServerInitialized { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsServerOnly
Declaration
[Obsolete("Use IsServerOnlyInitialized. Note the difference between IsServerOnlyInitialized and IsServerOnlyStarted.")]
public bool IsServerOnly { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsServerOnlyInitialized
Declaration
public bool IsServerOnlyInitialized { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsServerOnlyStarted
True if only the server is started.
Declaration
public bool IsServerOnlyStarted { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsServerStarted
True if server is started.
Declaration
public bool IsServerStarted { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsSpawned
True if this NetworkBehaviour is initialized for the network.
Declaration
public bool IsSpawned { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
LocalConnection
The local connection of the client calling this method.
Declaration
public NetworkConnection LocalConnection { get; }
Property Value
Type | Description |
---|---|
NetworkConnection |
NetworkManager
NetworkManager for this object.
Declaration
public NetworkManager NetworkManager { get; }
Property Value
Type | Description |
---|---|
NetworkManager |
NetworkObject
NetworkObject this behaviour is for.
Declaration
public NetworkObject NetworkObject { get; }
Property Value
Type | Description |
---|---|
NetworkObject |
NetworkObserver
NetworkObserver on this object.
Declaration
public NetworkObserver NetworkObserver { get; }
Property Value
Type | Description |
---|---|
NetworkObserver |
ObjectId
Unique Id for this _networkObjectCache. This does not represent the object owner.
Declaration
public int ObjectId { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
ObserverManager
ObserverManager for this object.
Declaration
public ObserverManager ObserverManager { get; }
Property Value
Type | Description |
---|---|
ObserverManager |
Observers
Observers for this NetworkBehaviour.
Declaration
public HashSet<NetworkConnection> Observers { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.HashSet<NetworkConnection> |
Owner
Owner of this object.
Declaration
public NetworkConnection Owner { get; }
Property Value
Type | Description |
---|---|
NetworkConnection |
OwnerId
ClientId for this NetworkObject owner.
Declaration
public int OwnerId { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
PredictionManager
PredictionManager for this object.
Declaration
public PredictionManager PredictionManager { get; }
Property Value
Type | Description |
---|---|
PredictionManager |
RollbackManager
RollbackManager for this object.
Declaration
public RollbackManager RollbackManager { get; }
Property Value
Type | Description |
---|---|
RollbackManager |
SceneManager
SceneManager for this object.
Declaration
public SceneManager SceneManager { get; }
Property Value
Type | Description |
---|---|
SceneManager |
ServerManager
ServerManager for this object.
Declaration
public ServerManager ServerManager { get; }
Property Value
Type | Description |
---|---|
ServerManager |
TimeManager
TimeManager for this object.
Declaration
public TimeManager TimeManager { get; }
Property Value
Type | Description |
---|---|
TimeManager |
TransportManager
TransportManager for this object.
Declaration
public TransportManager TransportManager { get; }
Property Value
Type | Description |
---|---|
TransportManager |
Methods
CanLog(LoggingType)
True if can log for loggingType.
Declaration
public bool CanLog(LoggingType loggingType)
Parameters
Type | Name | Description |
---|---|---|
LoggingType | loggingType | Type of logging being filtered. |
Returns
Type | Description |
---|---|
System.Boolean |
ClearBuffedRpcs()
Clears all buffered RPCs for this NetworkBehaviour.
Declaration
public void ClearBuffedRpcs()
ClearReplicateCache()
Clears cached replicates for server and client. This can be useful to call on server and client after teleporting.
Declaration
public virtual void ClearReplicateCache()
CreateReconcile()
Override this method to create your reconcile data, and call your reconcile method.
Declaration
public virtual void CreateReconcile()
Despawn(NetworkObject, Nullable<DespawnType>)
Despawns a NetworkObject. Only call from the server.
Declaration
public void Despawn(NetworkObject nob, DespawnType? despawnType = null)
Parameters
Type | Name | Description |
---|---|---|
NetworkObject | nob | NetworkObject to despawn. |
System.Nullable<DespawnType> | despawnType | What happens to the object after being despawned. |
Despawn(Nullable<DespawnType>)
Despawns this _networkObjectCache. Can only be called on the server.
Declaration
public void Despawn(DespawnType? despawnType = null)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<DespawnType> | despawnType | What happens to the object after being despawned. |
Despawn(GameObject, Nullable<DespawnType>)
Despawns a GameObject. Only call from the server.
Declaration
public void Despawn(GameObject go, DespawnType? despawnType = null)
Parameters
Type | Name | Description |
---|---|---|
UnityEngine.GameObject | go | GameObject to despawn. |
System.Nullable<DespawnType> | despawnType | What happens to the object after being despawned. |
EmptyReplicatesQueueIntoHistory_Start()
This is overriden by codegen to call EmptyReplicatesQueueIntoHistory(). This should only be called when client only.
Declaration
protected virtual void EmptyReplicatesQueueIntoHistory_Start()
EmptyReplicatesQueueIntoHistory<T>(BasicQueue<T>, RingBuffer<T>)
Replicates which are enqueued will be removed from the queue and put into replicatesHistory. This should only be called when client only.
Declaration
protected void EmptyReplicatesQueueIntoHistory<T>(BasicQueue<T> replicatesQueue, RingBuffer<T> replicatesHistory)
where T : IReplicateData
Parameters
Type | Name | Description |
---|---|---|
BasicQueue<T> | replicatesQueue | |
RingBuffer<T> | replicatesHistory |
Type Parameters
Name | Description |
---|---|
T |
GetInstance<T>()
Returns class of type if found within CodegenBase classes.
Declaration
public T GetInstance<T>()
where T : Component
Returns
Type | Description |
---|---|
T |
Type Parameters
Name | Description |
---|---|
T |
GiveOwnership(NetworkConnection)
Gives ownership to newOwner.
Declaration
public void GiveOwnership(NetworkConnection newOwner)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | newOwner |
GiveOwnership(NetworkConnection, Boolean)
Gives ownership to newOwner.
Declaration
public void GiveOwnership(NetworkConnection newOwner, bool includeNested)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | newOwner | |
System.Boolean | includeNested |
OnDespawnServer(NetworkConnection)
Called on the server before a despawn message for this object has been sent to connection. Useful for sending remote calls or actions to clients.
Declaration
public virtual void OnDespawnServer(NetworkConnection connection)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | connection |
OnOwnershipClient(NetworkConnection)
Called on the client after gaining or losing ownership.
Declaration
public virtual void OnOwnershipClient(NetworkConnection prevOwner)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | prevOwner | Previous owner of this object. |
OnOwnershipServer(NetworkConnection)
Called on the server after ownership has changed.
Declaration
public virtual void OnOwnershipServer(NetworkConnection prevOwner)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | prevOwner | Previous owner of this object. |
OnSpawnServer(NetworkConnection)
Called on the server after a spawn message for this object has been sent to clients. Useful for sending remote calls or data to clients.
Declaration
public virtual void OnSpawnServer(NetworkConnection connection)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | connection | Connection the object is being spawned for. |
OnStartClient()
Called on the client after initializing this object.
Declaration
public virtual void OnStartClient()
OnStartNetwork()
Called when the network has initialized this object. May be called for server or client but will only be called once. When as host or server this method will run before OnStartServer. When as client only the method will run before OnStartClient.
Declaration
public virtual void OnStartNetwork()
OnStartServer()
Called on the server after initializing this object. SyncTypes modified before or during this method will be sent to clients in the spawn message.
Declaration
public virtual void OnStartServer()
OnStopClient()
Called on the client before deinitializing this object.
Declaration
public virtual void OnStopClient()
OnStopNetwork()
Called when the network is deinitializing this object. May be called for server or client but will only be called once. When as host or server this method will run after OnStopServer. When as client only this method will run after OnStopClient.
Declaration
public virtual void OnStopNetwork()
OnStopServer()
Called on the server before deinitializing this object.
Declaration
public virtual void OnStopServer()
OnValidate()
Declaration
protected virtual void OnValidate()
OwnerMatches(NetworkConnection)
Returns if a connection is the owner of this object.
Declaration
public bool OwnerMatches(NetworkConnection connection)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | connection |
Returns
Type | Description |
---|---|
System.Boolean |
ReadPayload(NetworkConnection, Reader)
Called before network start callbacks, but after the object is initialized with network values. This may be used to read information from predicted spawning, or simply have values set before initialization without depending on SyncTypes.
Declaration
public virtual void ReadPayload(NetworkConnection connection, Reader reader)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | connection | Connection sending the payload. When coming from the server connection.IsValid will return false. |
Reader | reader |
Reconcile_Client_Start()
This is called when the networkbehaviour should perform a reconcile. Codegen overrides this calling Reconcile_Client with the needed data.
Declaration
protected virtual void Reconcile_Client_Start()
Reconcile_Reader_Local<T>(UInt32, PooledReader)
Reads a local reconcile from the client.
Declaration
public T Reconcile_Reader_Local<T>(uint tick, PooledReader reader)
where T : IReconcileData
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | tick | |
PooledReader | reader |
Returns
Type | Description |
---|---|
T |
Type Parameters
Name | Description |
---|---|
T |
Reconcile_Reader<T>(PooledReader, ref T, Channel)
Reads a reconcile from the server.
Declaration
public void Reconcile_Reader<T>(PooledReader reader, ref T lastReconciledata, Channel channel)
where T : IReconcileData
Parameters
Type | Name | Description |
---|---|---|
PooledReader | reader | |
T | lastReconciledata | |
Channel | channel |
Type Parameters
Name | Description |
---|---|
T |
Reconcile_Server<T>(UInt32, ref T, T, Channel)
Sends a reconcile to clients.
Declaration
public void Reconcile_Server<T>(uint methodHash, ref T lastReconcileData, T data, Channel channel)
where T : IReconcileData
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | methodHash | |
T | lastReconcileData | |
T | data | |
Channel | channel |
Type Parameters
Name | Description |
---|---|
T |
RegisterInstance<T>(T, Boolean)
Registers a new component to this NetworkManager.
Declaration
public void RegisterInstance<T>(T component, bool replace = true)
where T : Component
Parameters
Type | Name | Description |
---|---|---|
T | component | Reference of the component being registered. |
System.Boolean | replace | True to replace existing references. |
Type Parameters
Name | Description |
---|---|
T | Type to register. |
RegisterInvokeOnInstance<T>(Action<Component>)
Invokes an action when a specified component becomes registered. Action will invoke immediately if already registered.
Declaration
public void RegisterInvokeOnInstance<T>(Action<Component> handler)
where T : Component
Parameters
Type | Name | Description |
---|---|---|
System.Action<UnityEngine.Component> | handler | Action to invoke. |
Type Parameters
Name | Description |
---|---|
T | Component type. |
RemoveOwnership()
Removes ownership from all clients.
Declaration
public void RemoveOwnership()
Replicate_Replay_Authoritative<T>(UInt32, ReplicateUserLogicDelegate<T>, RingBuffer<T>, Channel)
Replays an input for authoritative entity.
Declaration
protected void Replicate_Replay_Authoritative<T>(uint replayTick, ReplicateUserLogicDelegate<T> del, RingBuffer<T> replicatesHistory, Channel channel)
where T : IReplicateData
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | replayTick | |
FishNet.Object.Prediction.Delegating.ReplicateUserLogicDelegate<T> | del | |
RingBuffer<T> | replicatesHistory | |
Channel | channel |
Type Parameters
Name | Description |
---|---|
T |
Replicate_Replay_NonAuthoritative<T>(UInt32, ReplicateUserLogicDelegate<T>, RingBuffer<T>, Channel)
Replays an input for non authoritative entity.
Declaration
protected void Replicate_Replay_NonAuthoritative<T>(uint replayTick, ReplicateUserLogicDelegate<T> del, RingBuffer<T> replicatesHistory, Channel channel)
where T : IReplicateData
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | replayTick | |
FishNet.Object.Prediction.Delegating.ReplicateUserLogicDelegate<T> | del | |
RingBuffer<T> | replicatesHistory | |
Channel | channel |
Type Parameters
Name | Description |
---|---|
T |
Replicate_Replay<T>(UInt32, ReplicateUserLogicDelegate<T>, RingBuffer<T>, Channel)
Replays inputs from replicates.
Declaration
protected void Replicate_Replay<T>(uint replayTick, ReplicateUserLogicDelegate<T> del, RingBuffer<T> replicatesHistory, Channel channel)
where T : IReplicateData
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | replayTick | |
FishNet.Object.Prediction.Delegating.ReplicateUserLogicDelegate<T> | del | |
RingBuffer<T> | replicatesHistory | |
Channel | channel |
Type Parameters
Name | Description |
---|---|
T |
Reset()
Declaration
protected virtual void Reset()
ResetState(Boolean)
Resets this NetworkBehaviour so that it may be added to an object pool.
Declaration
public virtual void ResetState(bool asServer)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | asServer |
SendServerRpc(UInt32, PooledWriter, Channel, DataOrderType)
Sends a RPC to server.
Declaration
protected void SendServerRpc(uint hash, PooledWriter methodWriter, Channel channel, DataOrderType orderType)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | hash | |
PooledWriter | methodWriter | |
Channel | channel | |
DataOrderType | orderType |
SendTargetRpc(UInt32, PooledWriter, Channel, DataOrderType, NetworkConnection, Boolean, Boolean)
Sends a RPC to target.
Declaration
protected void SendTargetRpc(uint hash, PooledWriter methodWriter, Channel channel, DataOrderType orderType, NetworkConnection target, bool excludeServer, bool validateTarget = true)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | hash | |
PooledWriter | methodWriter | |
Channel | channel | |
DataOrderType | orderType | |
NetworkConnection | target | |
System.Boolean | excludeServer | |
System.Boolean | validateTarget |
Spawn(NetworkObject, NetworkConnection, Scene)
Spawns an object over the network. Can only be called on the server.
Declaration
public void Spawn(NetworkObject nob, NetworkConnection ownerConnection = null, Scene scene = default(Scene))
Parameters
Type | Name | Description |
---|---|---|
NetworkObject | nob | GameObject instance to spawn. |
NetworkConnection | ownerConnection | Connection to give ownership to. |
UnityEngine.SceneManagement.Scene | scene |
Spawn(GameObject, NetworkConnection, Scene)
Spawns an object over the network. Can only be called on the server.
Declaration
public void Spawn(GameObject go, NetworkConnection ownerConnection = null, Scene scene = default(Scene))
Parameters
Type | Name | Description |
---|---|---|
UnityEngine.GameObject | go | GameObject instance to spawn. |
NetworkConnection | ownerConnection | Connection to give ownership to. |
UnityEngine.SceneManagement.Scene | scene |
ToString()
Outputs data about this NetworkBehaviour to string.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
System.String |
Overrides
TryRegisterInstance<T>(T)
Tries to registers a new component to this NetworkManager. This will not register the instance if another already exists.
Declaration
public bool TryRegisterInstance<T>(T component)
where T : Component
Parameters
Type | Name | Description |
---|---|---|
T | component | Reference of the component being registered. |
Returns
Type | Description |
---|---|
System.Boolean | True if was able to register, false if an instance is already registered. |
Type Parameters
Name | Description |
---|---|
T | Type to register. |
UnregisterInstance<T>()
Unregisters a component from this NetworkManager.
Declaration
public void UnregisterInstance<T>()
where T : Component
Type Parameters
Name | Description |
---|---|
T | Type to unregister. |
UnregisterInvokeOnInstance<T>(Action<Component>)
Removes an action to be invoked when a specified component becomes registered.
Declaration
public void UnregisterInvokeOnInstance<T>(Action<Component> handler)
where T : Component
Parameters
Type | Name | Description |
---|---|---|
System.Action<UnityEngine.Component> | handler | Action to invoke. |
Type Parameters
Name | Description |
---|---|
T | Component type. |
WritePayload(NetworkConnection, Writer)
Called when writing a spawn. This may be used to deliver information for predicted spawning, or simply have values set before initialization without depending on SyncTypes.
Declaration
public virtual void WritePayload(NetworkConnection connection, Writer writer)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | connection | Connection receiving the payload. When sending to the server connection.IsValid will return false. |
Writer | writer |