Skip to main content

Class NetworkBehaviour

Base class which should be inherited by scripts which contain networking functionality.

Inheritance
System.Object
Syntax
public abstract class NetworkBehaviour : MonoBehaviour

Fields

_nextSyncTime

Declaration
protected double _nextSyncTime

SyncSettings

Sync settings for this NetworkBehaviour Settings will be hidden in inspector unless Behaviour has SyncVar or SyncObjects

Declaration
public SyncSettings SyncSettings

syncObjects

objects that can synchronize themselves, such as synclists

Declaration
protected readonly List<ISyncObject> syncObjects

COMPONENT_INDEX_NOT_FOUND

Declaration
public const int COMPONENT_INDEX_NOT_FOUND = -1

Properties

IsServer

Returns true if this object is active on an active server. This is only true if the object has been spawned. This is different from NetworkServer.active, which is true if the server itself is active rather than this object being active.

Declaration
public bool IsServer { get; }

IsClient

Returns true if running as a client and this object was spawned by a server.

Declaration
public bool IsClient { get; }

IsLocalClient

Returns true if we're on host mode.

Declaration
[Obsolete("use IsHost instead")]
public bool IsLocalClient { get; }

IsHost

Returns true if we're on host mode.

Declaration
public bool IsHost { get; }

IsLocalPlayer

This returns true if this object is the one that represents the player on the local machine. In multiplayer games, there are multiple instances of the Player object. The client needs to know which one is for "themselves" so that only that player processes input and potentially has a camera attached. The IsLocalPlayer function will return true only for the player instance that belongs to the player on the local machine, so it can be used to filter out input for non-local players.

Declaration
public bool IsLocalPlayer { get; }

IsServerOnly

True if this object only exists on the server

Declaration
public bool IsServerOnly { get; }

IsClientOnly

True if this object exists on a client that is not also acting as a server

Declaration
public bool IsClientOnly { get; }

HasAuthority

This returns true if this object is the authoritative version of the object in the distributed network application. The value on the NetworkIdentity determines how authority is determined. For most objects, authority is held by the server. For objects with set, authority is held by the client of that player.

Declaration
public bool HasAuthority { get; }

NetId

The unique network Id of this object. This is assigned at runtime by the network server and will be unique for all objects for that network session.

Declaration
public uint NetId { get; }

Server

The associated to this object.

Declaration
public NetworkServer Server { get; }

ServerObjectManager

Quick Reference to the NetworkIdentities ServerObjectManager. Present only for server/host instances.

Declaration
public ServerObjectManager ServerObjectManager { get; }

Client

The associated to this object.

Declaration
public NetworkClient Client { get; }

ClientObjectManager

Quick Reference to the NetworkIdentities ClientObjectManager. Present only for instances instances.

Declaration
public ClientObjectManager ClientObjectManager { get; }

Owner

The associated with this This is only valid for player objects on the server.

Declaration
public INetworkPlayer Owner { get; }

World

Declaration
public NetworkWorld World { get; }

NetworkTime

Returns the appropriate NetworkTime instance based on if this NetworkBehaviour is running as a Server or Client.

Declaration
public NetworkTime NetworkTime { get; }

BehaviourId

Get Id of this NetworkBehaviour, Its NetId and ComponentIndex

Declaration
public NetworkBehaviour.Id BehaviourId { get; }

SyncVarDirtyBits

Declaration
protected ulong SyncVarDirtyBits { get; }

AnySyncObjectDirty

Declaration
protected bool AnySyncObjectDirty { get; }

Identity

Returns the NetworkIdentity of this object

Declaration
public NetworkIdentity Identity { get; }

ComponentIndex

Returns the index of the component on this object

Declaration
public int ComponentIndex { get; }

Methods

GetSyncVarHookGuard(UInt64)

Declaration
protected bool GetSyncVarHookGuard(ulong dirtyBit)
Parameters
TypeNameDescription
System.UInt64dirtyBit
Returns
TypeDescription
System.Boolean

SetSyncVarHookGuard(UInt64, Boolean)

Declaration
protected void SetSyncVarHookGuard(ulong dirtyBit, bool value)
Parameters
TypeNameDescription
System.UInt64dirtyBit
System.Booleanvalue

InitSyncObject(ISyncObject)

Declaration
protected void InitSyncObject(ISyncObject syncObject)
Parameters
TypeNameDescription
Mirage.Collections.ISyncObjectsyncObject

UpdateSyncObjectShouldSync()

Call this after updating SyncSettings to update all SyncObjects

This only needs to be called manually if updating syncSettings at runtime. Mirage will automatically call this after serializing or deserializing with initialState

Declaration
public void UpdateSyncObjectShouldSync()

SyncVarEqual<T>(T, T)

Declaration
protected bool SyncVarEqual<T>(T value, T fieldValue)
Parameters
TypeNameDescription
Tvalue
TfieldValue
Returns
TypeDescription
System.Boolean

SetDirtyBit(UInt64)

Used to set the behaviour as dirty, so that a network update will be sent for the object. these are masks, not bit numbers, ie. 0x004 not 2

Declaration
public void SetDirtyBit(ulong bitMask)
Parameters
TypeNameDescription
System.UInt64bitMaskBit mask to set.

ClearDirtyBit(UInt64)

Used to clear dirty bit. Object may still be in dirty list, so will be checked in next update. but values in this mask will no longer be set until they are changed again

Declaration
public void ClearDirtyBit(ulong bitMask)
Parameters
TypeNameDescription
System.UInt64bitMaskBit mask to set.

ClearDirtyBits()

Declaration
public void ClearDirtyBits()

ClearShouldSync(Double)

Clears dirty bits and sets the next sync time

Declaration
public void ClearShouldSync(double now)
Parameters
TypeNameDescription
System.Doublenow

ShouldSync(Double)

True if this behaviour is dirty and it is time to sync

Declaration
public bool ShouldSync(double time)
Parameters
TypeNameDescription
System.Doubletime
Returns
TypeDescription
System.Boolean

TimeToSync(Double)

If it is time to sync based on last sync and

Declaration
public bool TimeToSync(double time)
Parameters
TypeNameDescription
System.Doubletime
Returns
TypeDescription
System.Boolean

AnyDirtyBits()

Are any SyncVar or SyncObjects dirty

Declaration
public bool AnyDirtyBits()
Returns
TypeDescription
System.Boolean

OnSerialize(NetworkWriter, Boolean)

Virtual function to override to send custom serialization data. The corresponding function to send serialization data is OnDeserialize().

Declaration
public virtual bool OnSerialize(NetworkWriter writer, bool initialState)
Parameters
TypeNameDescription
Mirage.Serialization.NetworkWriterwriterWriter to use to write to the stream.
System.BooleaninitialStateIf this is being called to send initial state.
Returns
TypeDescription
System.BooleanTrue if data was written.

OnDeserialize(NetworkReader, Boolean)

Virtual function to override to receive custom serialization data. The corresponding function to send serialization data is OnSerialize().

Declaration
public virtual void OnDeserialize(NetworkReader reader, bool initialState)
Parameters
TypeNameDescription
Mirage.Serialization.NetworkReaderreaderReader to read from the stream.
System.BooleaninitialStateTrue if being sent initial state.

SerializeSyncVars(NetworkWriter, Boolean)

Declaration
public virtual bool SerializeSyncVars(NetworkWriter writer, bool initialState)
Parameters
TypeNameDescription
Mirage.Serialization.NetworkWriterwriter
System.BooleaninitialState
Returns
TypeDescription
System.Boolean

DeserializeSyncVars(NetworkReader, Boolean)

Declaration
public virtual void DeserializeSyncVars(NetworkReader reader, bool initialState)
Parameters
TypeNameDescription
Mirage.Serialization.NetworkReaderreader
System.BooleaninitialState

SetDeserializeMask(UInt64, Int32)

Declaration
protected void SetDeserializeMask(ulong dirtyBit, int offset)
Parameters
TypeNameDescription
System.UInt64dirtyBit
System.Int32offset

SerializeObjects(NetworkWriter, Boolean)

Declaration
public bool SerializeObjects(NetworkWriter writer, bool initialState)
Parameters
TypeNameDescription
Mirage.Serialization.NetworkWriterwriter
System.BooleaninitialState
Returns
TypeDescription
System.Boolean

SerializeObjectsAll(NetworkWriter)

Declaration
public bool SerializeObjectsAll(NetworkWriter writer)
Parameters
TypeNameDescription
Mirage.Serialization.NetworkWriterwriter
Returns
TypeDescription
System.Boolean

SerializeObjectsDelta(NetworkWriter)

Declaration
public bool SerializeObjectsDelta(NetworkWriter writer)
Parameters
TypeNameDescription
Mirage.Serialization.NetworkWriterwriter
Returns
TypeDescription
System.Boolean

GetRpcCount()

Declaration
protected virtual int GetRpcCount()
Returns
TypeDescription
System.Int32

RegisterRpc(RemoteCallCollection)

Declaration
protected virtual void RegisterRpc(RemoteCallCollection collection)
Parameters
TypeNameDescription
Mirage.RemoteCalls.RemoteCallCollectioncollection