Skip to content

Mirror::NetworkBehaviour

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

Inherits from MonoBehaviour

Inherited by AutoHostClient, CameraController, MatchMaker, Player, PlayerAttack, PlayerCameraContoller, PlayerMovement, TurnManager, Ball, BallManager, Player, PlayerController, RandomColor, ShootingTankBehaviour, ZoneHandler, Player, MonsterMovement, PlayerMovement, Player, PhysicsCollision, PlayerController, PlayerScore, RandomColor, Reward, Spawner, PlayerController, PlayerScore, RandomColor, Reward, Spawner, Ball, Player, AddForce, Projectile, Tank, NetworkLerpRigidbody, NetworkRigidbody, NetworkRigidbody2D, NetworkTransformBase, NetworkAnimator, NetworkRoomPlayer, NetworkTransformBase, NetworkVisibility

Public Functions

Name
void SetDirtyBit(ulong dirtyBit)
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
void ClearAllDirtyBits()
This clears all the dirty bits that were set on this script by SetDirtyBits();
bool IsDirty()
virtual bool OnSerialize(NetworkWriter writer, bool initialState)
Virtual function to override to send custom serialization data. The corresponding function to send serialization data is OnDeserialize().
virtual void OnDeserialize(NetworkReader reader, bool initialState)
Virtual function to override to receive custom serialization data. The corresponding function to send serialization data is OnSerialize().
bool SerializeObjectsAll(NetworkWriter writer)
bool SerializeObjectsDelta(NetworkWriter writer)
virtual void OnStopClient()
This is invoked on clients when the server has caused this object to be destroyed.
virtual void OnStartServer()
This is invoked for NetworkBehaviour objects when they become active on the server.
virtual void OnStopServer()
Invoked on the server when the object is unspawned
virtual void OnStartClient()
Called on every NetworkBehaviour when it is activated on a client.
virtual void OnStartLocalPlayer()
Called when the local player object has been set up.
virtual void OnStartAuthority()
This is invoked on behaviours that have authority, based on context and NetworkIdentity.hasAuthority.
virtual void OnStopAuthority()
This is invoked on behaviours when authority is removed.

Protected Functions

Name
bool getSyncVarHookGuard(ulong dirtyBit)
void setSyncVarHookGuard(ulong dirtyBit, bool value)
void InitSyncObject(SyncObject syncObject)
void SendCommandInternal(Type invokeClass, string cmdName, NetworkWriter writer, int channelId, bool ignoreAuthority =false)
void SendRPCInternal(Type invokeClass, string rpcName, NetworkWriter writer, int channelId, bool excludeOwner)
void SendTargetRPCInternal(NetworkConnection conn, Type invokeClass, string rpcName, NetworkWriter writer, int channelId)
bool SyncVarGameObjectEqual(GameObject newGameObject, uint netIdField)
void SetSyncVarGameObject(GameObject newGameObject, ref GameObject gameObjectField, ulong dirtyBit, ref uint netIdField)
GameObject GetSyncVarGameObject(uint netId, ref GameObject gameObjectField)
bool SyncVarNetworkIdentityEqual(NetworkIdentity newIdentity, uint netIdField)
void SetSyncVarNetworkIdentity(NetworkIdentity newIdentity, ref NetworkIdentity identityField, ulong dirtyBit, ref uint netIdField)
NetworkIdentity GetSyncVarNetworkIdentity(uint netId, ref NetworkIdentity identityField)
bool SyncVarEqual< T >(T value, ref T fieldValue)
void SetSyncVar< T >(T value, ref T fieldValue, ulong dirtyBit)
virtual bool SerializeSyncVars(NetworkWriter writer, bool initialState)
virtual void DeserializeSyncVars(NetworkReader reader, bool initialState)

Public Properties

Name
NetworkIdentity netIdentity
Returns the NetworkIdentity of this object
int ComponentIndex
Returns the index of the component on this object

Protected Properties

Name
ulong syncVarDirtyBits

Public Attributes

Name
SyncMode syncMode
sync mode for OnSerialize
float syncInterval
sync interval for OnSerialize (in seconds)
bool isServer
Returns true if this object is active on an active server.
bool isClient
Returns true if running as a client and this object was spawned by a server.
bool isLocalPlayer
This returns true if this object is the one that represents the player on the local machine.
bool isServerOnly
True if this object only exists on the server
bool isClientOnly
True if this object exists on a client that is not also acting as a server
bool hasAuthority
This returns true if this object is the authoritative version of the object in the distributed network application.
uint netId
The unique network Id of this object.
NetworkConnection connectionToServer
The NetworkConnection associated with this NetworkIdentity. This is only valid for player objects on the client.
NetworkConnection connectionToClient
The NetworkConnection associated with this NetworkIdentity. This is only valid for player objects on the server.

Protected Attributes

Name
readonly List< SyncObject > syncObjects
objects that can synchronize themselves, such as synclists

Detailed Description

class Mirror::NetworkBehaviour;

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

This is a MonoBehaviour class so scripts which need to use the networking feature should inherit this class instead of MonoBehaviour. It allows you to invoke networked actions, receive various callbacks, and automatically synchronize state from server-to-client.

The NetworkBehaviour component requires a NetworkIdentity on the game object. There can be multiple NetworkBehaviours on a single game object. For an object with sub-components in a hierarchy, the NetworkIdentity must be on the root object, and NetworkBehaviour scripts must also be on the root object.

Some of the built-in components of the networking system are derived from NetworkBehaviour, including NetworkTransport, NetworkAnimator and NetworkProximityChecker.

Public Functions Documentation

function SetDirtyBit

inline void SetDirtyBit(
    ulong dirtyBit
)

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

Parameters:

  • dirtyBit Bit mask to set.

function ClearAllDirtyBits

inline void ClearAllDirtyBits()

This clears all the dirty bits that were set on this script by SetDirtyBits();

This is automatically invoked when an update is sent for this object, but can be called manually as well.

function IsDirty

inline bool IsDirty()

function OnSerialize

inline virtual bool OnSerialize(
    NetworkWriter writer,
    bool initialState
)

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

Parameters:

  • writer Writer to use to write to the stream.
  • initialState If this is being called to send initial state.

Return: True if data was written.

Reimplemented by: Mirror::NetworkTransformBase::OnSerialize, Mirror::NetworkAnimator::OnSerialize

The initialState flag is useful to differentiate between the first time an object is serialized and when incremental updates can be sent. The first time an object is sent to a client, it must include a full state snapshot, but subsequent updates can save on bandwidth by including only incremental changes. Note that SyncVar hook functions are not called when initialState is true, only for incremental updates.

If a class has SyncVars, then an implementation of this function and OnDeserialize() are added automatically to the class. So a class that has SyncVars cannot also have custom serialization functions.

The OnSerialize function should return true to indicate that an update should be sent. If it returns true, then the dirty bits for that script are set to zero, if it returns false then the dirty bits are not changed. This allows multiple changes to a script to be accumulated over time and sent when the system is ready, instead of every frame.

function OnDeserialize

inline virtual void OnDeserialize(
    NetworkReader reader,
    bool initialState
)

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

Parameters:

  • reader Reader to read from the stream.
  • initialState True if being sent initial state.

Reimplemented by: Mirror::NetworkTransformBase::OnDeserialize, Mirror::NetworkAnimator::OnDeserialize

function SerializeObjectsAll

inline bool SerializeObjectsAll(
    NetworkWriter writer
)

function SerializeObjectsDelta

inline bool SerializeObjectsDelta(
    NetworkWriter writer
)

function OnStopClient

inline virtual void OnStopClient()

This is invoked on clients when the server has caused this object to be destroyed.

Reimplemented by: Me::DerangedSenators::CopsAndRobbers::Player::OnStopClient

This can be used as a hook to invoke effects or do client specific cleanup.

function OnStartServer

inline virtual void OnStartServer()

This is invoked for NetworkBehaviour objects when they become active on the server.

Reimplemented by: Mirror::Examples::NetworkRoom::RandomColor::OnStartServer, Mirror::Examples::MultipleAdditiveScenes::RandomColor::OnStartServer, Mirror::Examples::Additive::RandomColor::OnStartServer, Mirror::Examples::NetworkRoom::Spawner::OnStartServer, Mirror::Examples::Pong::Ball::OnStartServer, Mirror::Examples::MultipleAdditiveScenes::Spawner::OnStartServer, Mirror::Cloud::Examples::Pong::Ball::OnStartServer, Mirror::Examples::Tanks::Projectile::OnStartServer, Mirror::Cloud::Examples::Pong::BallManager::OnStartServer, Mirror::Examples::Benchmark::MonsterMovement::OnStartServer, Mirror::Examples::Basic::Player::OnStartServer, Mirror::NetworkProximityChecker::OnStartServer, Mirror::NetworkSceneChecker::OnStartServer, Mirror::NetworkMatchChecker::OnStartServer

This could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created.

This will be called for objects on a "host" as well as for object on a dedicated server.

function OnStopServer

inline virtual void OnStopServer()

Invoked on the server when the object is unspawned

Reimplemented by: Mirror::Cloud::Examples::Pong::BallManager::OnStopServer, Mirror::NetworkProximityChecker::OnStopServer, Me::DerangedSenators::CopsAndRobbers::Player::OnStopServer

Useful for saving object data in persistant storage

function OnStartClient

inline virtual void OnStartClient()

Called on every NetworkBehaviour when it is activated on a client.

Reimplemented by: Mirror::Examples::NetworkRoom::NetworkRoomPlayerExt::OnStartClient, Me::DerangedSenators::CopsAndRobbers::Player::OnStartClient, Mirror::Examples::Basic::Player::OnStartClient

Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client.

function OnStartLocalPlayer

inline virtual void OnStartLocalPlayer()

Called when the local player object has been set up.

Reimplemented by: Me::DerangedSenators::CopsAndRobbers::CameraController::OnStartLocalPlayer, Mirror::Examples::NetworkRoom::PlayerController::OnStartLocalPlayer, Mirror::Examples::Additive::PlayerController::OnStartLocalPlayer, Mirror::Examples::MultipleAdditiveScenes::PlayerController::OnStartLocalPlayer, Mirror::Examples::Basic::Player::OnStartLocalPlayer

This happens after OnStartClient(), as it is triggered by an ownership message from the server. This is an appropriate place to activate components or functionality that should only be active for the local player, such as cameras and input.

function OnStartAuthority

inline virtual void OnStartAuthority()

This is invoked on behaviours that have authority, based on context and NetworkIdentity.hasAuthority.

This is called after OnStartServer and before OnStartClient.

When AssignClientAuthority is called on the server, this will be called on the client that owns the object. When an object is spawned with [NetworkServer.Spawn] with a NetworkConnection parameter included, this will be called on the client that owns the object.

function OnStopAuthority

inline virtual void OnStopAuthority()

This is invoked on behaviours when authority is removed.

When NetworkIdentity.RemoveClientAuthority is called on the server, this will be called on the client that owns the object.

Protected Functions Documentation

function getSyncVarHookGuard

inline bool getSyncVarHookGuard(
    ulong dirtyBit
)

function setSyncVarHookGuard

inline void setSyncVarHookGuard(
    ulong dirtyBit,
    bool value
)

function InitSyncObject

inline void InitSyncObject(
    SyncObject syncObject
)

function SendCommandInternal

inline void SendCommandInternal(
    Type invokeClass,
    string cmdName,
    NetworkWriter writer,
    int channelId,
    bool ignoreAuthority =false
)

function SendRPCInternal

inline void SendRPCInternal(
    Type invokeClass,
    string rpcName,
    NetworkWriter writer,
    int channelId,
    bool excludeOwner
)

function SendTargetRPCInternal

inline void SendTargetRPCInternal(
    NetworkConnection conn,
    Type invokeClass,
    string rpcName,
    NetworkWriter writer,
    int channelId
)

function SyncVarGameObjectEqual

inline bool SyncVarGameObjectEqual(
    GameObject newGameObject,
    uint netIdField
)

function SetSyncVarGameObject

inline void SetSyncVarGameObject(
    GameObject newGameObject,
    ref GameObject gameObjectField,
    ulong dirtyBit,
    ref uint netIdField
)

function GetSyncVarGameObject

inline GameObject GetSyncVarGameObject(
    uint netId,
    ref GameObject gameObjectField
)

function SyncVarNetworkIdentityEqual

inline bool SyncVarNetworkIdentityEqual(
    NetworkIdentity newIdentity,
    uint netIdField
)

function SetSyncVarNetworkIdentity

inline void SetSyncVarNetworkIdentity(
    NetworkIdentity newIdentity,
    ref NetworkIdentity identityField,
    ulong dirtyBit,
    ref uint netIdField
)

function GetSyncVarNetworkIdentity

inline NetworkIdentity GetSyncVarNetworkIdentity(
    uint netId,
    ref NetworkIdentity identityField
)

function SyncVarEqual< T >

inline bool SyncVarEqual< T >(
    T value,
    ref T fieldValue
)

function SetSyncVar< T >

inline void SetSyncVar< T >(
    T value,
    ref T fieldValue,
    ulong dirtyBit
)

function SerializeSyncVars

inline virtual bool SerializeSyncVars(
    NetworkWriter writer,
    bool initialState
)

function DeserializeSyncVars

inline virtual void DeserializeSyncVars(
    NetworkReader reader,
    bool initialState
)

Public Property Documentation

property netIdentity

NetworkIdentity netIdentity;

Returns the NetworkIdentity of this object

property ComponentIndex

int ComponentIndex;

Returns the index of the component on this object

Protected Property Documentation

property syncVarDirtyBits

ulong syncVarDirtyBits;

Public Attributes Documentation

variable syncMode

SyncMode syncMode = SyncMode.Observers;

sync mode for OnSerialize

variable syncInterval

float syncInterval = 0.1f;

sync interval for OnSerialize (in seconds)

variable isServer

bool isServer => netIdentity.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.

variable isClient

bool isClient => netIdentity.isClient;

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

variable isLocalPlayer

bool isLocalPlayer => netIdentity.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.

variable isServerOnly

bool isServerOnly => [isServer](/Documentation/Cops%20And%20Robbers/Classes/classMirror_1_1NetworkBehaviour/#variable-isserver) && ![isClient](/Documentation/Cops%20And%20Robbers/Classes/classMirror_1_1NetworkBehaviour/#variable-isclient);

True if this object only exists on the server

variable isClientOnly

bool isClientOnly => [isClient](/Documentation/Cops%20And%20Robbers/Classes/classMirror_1_1NetworkBehaviour/#variable-isclient) && ![isServer](/Documentation/Cops%20And%20Robbers/Classes/classMirror_1_1NetworkBehaviour/#variable-isserver);

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

variable hasAuthority

bool hasAuthority => netIdentity.hasAuthority;

This returns true if this object is the authoritative version of the object in the distributed network application.

The NetworkIdentity.hasAuthority value on the NetworkIdentity determines how authority is determined. For most objects, authority is held by the server. For objects with NetworkIdentity.hasAuthority set, authority is held by the client of that player.

variable netId

uint netId => netIdentity.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.

variable connectionToServer

NetworkConnection connectionToServer => netIdentity.connectionToServer;

The NetworkConnection associated with this NetworkIdentity. This is only valid for player objects on the client.

variable connectionToClient

NetworkConnection connectionToClient => netIdentity.connectionToClient;

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

Protected Attributes Documentation

variable syncObjects

readonly List< SyncObject > syncObjects = new List<[SyncObject](/Documentation/Cops%20And%20Robbers/Classes/interfaceMirror_1_1SyncObject/)>();

objects that can synchronize themselves, such as synclists


Updated on 22 January 2021 at 07:53:47 UTC