Mirror Networking
Mirror.NetworkBehaviour Class Reference

Base class for networked components. More...

Inheritance diagram for Mirror.NetworkBehaviour:

Classes

struct  NetworkBehaviourSyncVar
 

Public Member Functions

void SetSyncVarDirtyBit (ulong dirtyBit)
 Set as dirty so that it's synced to clients again. More...
 
bool IsDirty ()
 
void ClearAllDirtyBits ()
 Clears all the dirty bits that were set by SetDirtyBits() More...
 
void GeneratedSyncVarSetter< T > (T value, ref T field, ulong dirtyBit, Action< T, T > OnChanged)
 
void GeneratedSyncVarSetter_GameObject (GameObject value, ref GameObject field, ulong dirtyBit, Action< GameObject, GameObject > OnChanged, ref uint netIdField)
 
void GeneratedSyncVarSetter_NetworkIdentity (NetworkIdentity value, ref NetworkIdentity field, ulong dirtyBit, Action< NetworkIdentity, NetworkIdentity > OnChanged, ref uint netIdField)
 
void GeneratedSyncVarSetter_NetworkBehaviour< T > (T value, ref T field, ulong dirtyBit, Action< T, T > OnChanged, ref NetworkBehaviourSyncVar netIdField)
 
void GeneratedSyncVarDeserialize< T > (ref T field, Action< T, T > OnChanged, T value)
 
void GeneratedSyncVarDeserialize_GameObject (ref GameObject field, Action< GameObject, GameObject > OnChanged, NetworkReader reader, ref uint netIdField)
 
void GeneratedSyncVarDeserialize_NetworkIdentity (ref NetworkIdentity field, Action< NetworkIdentity, NetworkIdentity > OnChanged, NetworkReader reader, ref uint netIdField)
 
void GeneratedSyncVarDeserialize_NetworkBehaviour< T > (ref T field, Action< T, T > OnChanged, NetworkReader reader, ref NetworkBehaviourSyncVar netIdField)
 
virtual bool OnSerialize (NetworkWriter writer, bool initialState)
 Override to do custom serialization (instead of SyncVars/SyncLists). Use OnDeserialize too. More...
 
virtual void OnDeserialize (NetworkReader reader, bool initialState)
 Override to do custom deserialization (instead of SyncVars/SyncLists). Use OnSerialize too. More...
 
bool SerializeObjectsAll (NetworkWriter writer)
 
bool SerializeObjectsDelta (NetworkWriter writer)
 
virtual void OnStartServer ()
 Like Start(), but only called on server and host. More...
 
virtual void OnStopServer ()
 Stop event, only called on server and host. More...
 
virtual void OnStartClient ()
 Like Start(), but only called on client and host. More...
 
virtual void OnStopClient ()
 Stop event, only called on client and host. More...
 
virtual void OnStartLocalPlayer ()
 Like Start(), but only called on client and host for the local player object. More...
 
virtual void OnStopLocalPlayer ()
 Stop event, but only called on client and host for the local player object. More...
 
virtual void OnStartAuthority ()
 Like Start(), but only called for objects the client has authority over. More...
 
virtual void OnStopAuthority ()
 Stop event, only called for objects the client has authority over. More...
 

Static Public Member Functions

static bool SyncVarGameObjectEqual (GameObject newGameObject, uint netIdField)
 
static bool SyncVarNetworkIdentityEqual (NetworkIdentity newIdentity, uint netIdField)
 

Public Attributes

SyncMode syncMode = SyncMode.Observers
 sync mode for OnSerialize More...
 
float syncInterval = 0.1f
 sync interval for OnSerialize (in seconds) More...
 

Protected Member Functions

bool GetSyncVarHookGuard (ulong dirtyBit)
 
void SetSyncVarHookGuard (ulong dirtyBit, bool value)
 
void InitSyncObject (SyncObject syncObject)
 
void SendCommandInternal (string functionFullName, NetworkWriter writer, int channelId, bool requiresAuthority=true)
 
void SendRPCInternal (string functionFullName, NetworkWriter writer, int channelId, bool includeOwner)
 
void SendTargetRPCInternal (NetworkConnection conn, string functionFullName, NetworkWriter writer, int channelId)
 
void SetSyncVarGameObject (GameObject newGameObject, ref GameObject gameObjectField, ulong dirtyBit, ref uint netIdField)
 
GameObject GetSyncVarGameObject (uint netId, ref GameObject gameObjectField)
 
void SetSyncVarNetworkIdentity (NetworkIdentity newIdentity, ref NetworkIdentity identityField, ulong dirtyBit, ref uint netIdField)
 
NetworkIdentity GetSyncVarNetworkIdentity (uint netId, ref NetworkIdentity identityField)
 
void SetSyncVarNetworkBehaviour< T > (T newBehaviour, ref T behaviourField, ulong dirtyBit, ref NetworkBehaviourSyncVar syncField)
 
GetSyncVarNetworkBehaviour< T > (NetworkBehaviourSyncVar syncNetBehaviour, ref T behaviourField)
 
void SetSyncVar< T > (T value, ref T fieldValue, ulong dirtyBit)
 
virtual bool SerializeSyncVars (NetworkWriter writer, bool initialState)
 
virtual void DeserializeSyncVars (NetworkReader reader, bool initialState)
 

Static Protected Member Functions

static bool SyncVarNetworkBehaviourEqual< T > (T newBehaviour, NetworkBehaviourSyncVar syncField)
 
static bool SyncVarEqual< T > (T value, ref T fieldValue)
 

Protected Attributes

readonly List< SyncObjectsyncObjects = new List<SyncObject>()
 

Properties

bool isServer [get]
 True if this object is on the server and has been spawned. More...
 
bool isClient [get]
 True if this object is on the client and has been spawned by the server. More...
 
bool isLocalPlayer [get]
 True if this object is the the client's own local player. More...
 
bool isServerOnly [get]
 True if this object is on the server-only, not host. More...
 
bool isClientOnly [get]
 True if this object is on the client-only, not host. More...
 
bool hasAuthority [get]
 True on client if that component has been assigned to the client. E.g. player, pets, henchmen. More...
 
uint netId [get]
 The unique network Id of this object (unique at runtime). More...
 
NetworkConnection connectionToServer [get]
 Client's network connection to the server. This is only valid for player objects on the client. More...
 
NetworkConnectionToClient connectionToClient [get]
 Server's network connection to the client. This is only valid for player objects on the server. More...
 
NetworkIdentity netIdentity [get, set]
 Returns the NetworkIdentity of this object More...
 
int ComponentIndex [get, set]
 Returns the index of the component on this object More...
 
ulong syncVarDirtyBits [get]
 

Detailed Description

Base class for networked components.

Definition at line 15 of file NetworkBehaviour.cs.

Member Function Documentation

◆ ClearAllDirtyBits()

void Mirror.NetworkBehaviour.ClearAllDirtyBits ( )

Clears all the dirty bits that were set by SetDirtyBits()

Definition at line 137 of file NetworkBehaviour.cs.

138 {
139 lastSyncTime = NetworkTime.localTime;
140 syncVarDirtyBits = 0L;
141 syncObjectDirtyBits = 0L;
142
143 // clear all unsynchronized changes in syncobjects
144 // (Linq allocates, use for instead)
145 for (int i = 0; i < syncObjects.Count; ++i)
146 {
147 syncObjects[i].ClearChanges();
148 }
149 }

◆ DeserializeSyncVars()

virtual void Mirror.NetworkBehaviour.DeserializeSyncVars ( NetworkReader  reader,
bool  initialState 
)
protectedvirtual

Definition at line 985 of file NetworkBehaviour.cs.

986 {
987 // SyncVars are read here in subclass
988
989 // if initialState
990 // read all SyncVars
991 // else
992 // read syncVarDirtyBits
993 // read dirty SyncVars
994 }

◆ GeneratedSyncVarDeserialize< T >()

void Mirror.NetworkBehaviour.GeneratedSyncVarDeserialize< T > ( ref T  field,
Action< T, T >  OnChanged,
value 
)

Definition at line 584 of file NetworkBehaviour.cs.

585 {
586 T previous = field;
587 field = value;
588
589 // any hook? then call if changed.
590 if (OnChanged != null && !SyncVarEqual(previous, ref field))
591 {
592 OnChanged(previous, field);
593 }
594 }

◆ GeneratedSyncVarDeserialize_GameObject()

void Mirror.NetworkBehaviour.GeneratedSyncVarDeserialize_GameObject ( ref GameObject  field,
Action< GameObject, GameObject >  OnChanged,
NetworkReader  reader,
ref uint  netIdField 
)

Definition at line 642 of file NetworkBehaviour.cs.

643 {
644 uint previousNetId = netIdField;
645 GameObject previousGameObject = field;
646 netIdField = reader.ReadUInt();
647
648 // get the new GameObject now that netId field is set
649 field = GetSyncVarGameObject(netIdField, ref field);
650
651 // any hook? then call if changed.
652 if (OnChanged != null && !SyncVarEqual(previousNetId, ref netIdField))
653 {
654 OnChanged(previousGameObject, field);
655 }
656 }

◆ GeneratedSyncVarDeserialize_NetworkBehaviour< T >()

void Mirror.NetworkBehaviour.GeneratedSyncVarDeserialize_NetworkBehaviour< T > ( ref T  field,
Action< T, T >  OnChanged,
NetworkReader  reader,
ref NetworkBehaviourSyncVar  netIdField 
)
Type Constraints
T :NetworkBehaviour 

Definition at line 769 of file NetworkBehaviour.cs.

770 : NetworkBehaviour
771 {
772 NetworkBehaviourSyncVar previousNetId = netIdField;
773 T previousBehaviour = field;
774 netIdField = reader.ReadNetworkBehaviourSyncVar();
775
776 // get the new NetworkBehaviour now that netId field is set
777 field = GetSyncVarNetworkBehaviour(netIdField, ref field);
778
779 // any hook? then call if changed.
780 if (OnChanged != null && !SyncVarEqual(previousNetId, ref netIdField))
781 {
782 OnChanged(previousBehaviour, field);
783 }
784 }

◆ GeneratedSyncVarDeserialize_NetworkIdentity()

void Mirror.NetworkBehaviour.GeneratedSyncVarDeserialize_NetworkIdentity ( ref NetworkIdentity  field,
Action< NetworkIdentity, NetworkIdentity OnChanged,
NetworkReader  reader,
ref uint  netIdField 
)

Definition at line 705 of file NetworkBehaviour.cs.

706 {
707 uint previousNetId = netIdField;
708 NetworkIdentity previousIdentity = field;
709 netIdField = reader.ReadUInt();
710
711 // get the new NetworkIdentity now that netId field is set
712 field = GetSyncVarNetworkIdentity(netIdField, ref field);
713
714 // any hook? then call if changed.
715 if (OnChanged != null && !SyncVarEqual(previousNetId, ref netIdField))
716 {
717 OnChanged(previousIdentity, field);
718 }
719 }

◆ GeneratedSyncVarSetter< T >()

void Mirror.NetworkBehaviour.GeneratedSyncVarSetter< T > ( value,
ref T  field,
ulong  dirtyBit,
Action< T, T >  OnChanged 
)

Definition at line 345 of file NetworkBehaviour.cs.

346 {
347 if (!SyncVarEqual(value, ref field))
348 {
349 T oldValue = field;
350 SetSyncVar(value, ref field, dirtyBit);
351
352 // call hook (if any)
353 if (OnChanged != null)
354 {
355 // in host mode, setting a SyncVar calls the hook directly.
356 // in client-only mode, OnDeserialize would call it.
357 // we use hook guard to protect against deadlock where hook
358 // changes syncvar, calling hook again.
359 if (NetworkServer.localClientActive && !GetSyncVarHookGuard(dirtyBit))
360 {
361 SetSyncVarHookGuard(dirtyBit, true);
362 OnChanged(oldValue, value);
363 SetSyncVarHookGuard(dirtyBit, false);
364 }
365 }
366 }
367 }

◆ GeneratedSyncVarSetter_GameObject()

void Mirror.NetworkBehaviour.GeneratedSyncVarSetter_GameObject ( GameObject  value,
ref GameObject  field,
ulong  dirtyBit,
Action< GameObject, GameObject >  OnChanged,
ref uint  netIdField 
)

Definition at line 372 of file NetworkBehaviour.cs.

373 {
374 if (!SyncVarGameObjectEqual(value, netIdField))
375 {
376 GameObject oldValue = field;
377 SetSyncVarGameObject(value, ref field, dirtyBit, ref netIdField);
378
379 // call hook (if any)
380 if (OnChanged != null)
381 {
382 // in host mode, setting a SyncVar calls the hook directly.
383 // in client-only mode, OnDeserialize would call it.
384 // we use hook guard to protect against deadlock where hook
385 // changes syncvar, calling hook again.
386 if (NetworkServer.localClientActive && !GetSyncVarHookGuard(dirtyBit))
387 {
388 SetSyncVarHookGuard(dirtyBit, true);
389 OnChanged(oldValue, value);
390 SetSyncVarHookGuard(dirtyBit, false);
391 }
392 }
393 }
394 }

◆ GeneratedSyncVarSetter_NetworkBehaviour< T >()

void Mirror.NetworkBehaviour.GeneratedSyncVarSetter_NetworkBehaviour< T > ( value,
ref T  field,
ulong  dirtyBit,
Action< T, T >  OnChanged,
ref NetworkBehaviourSyncVar  netIdField 
)
Type Constraints
T :NetworkBehaviour 

Definition at line 426 of file NetworkBehaviour.cs.

427 : NetworkBehaviour
428 {
429 if (!SyncVarNetworkBehaviourEqual(value, netIdField))
430 {
431 T oldValue = field;
432 SetSyncVarNetworkBehaviour(value, ref field, dirtyBit, ref netIdField);
433
434 // call hook (if any)
435 if (OnChanged != null)
436 {
437 // in host mode, setting a SyncVar calls the hook directly.
438 // in client-only mode, OnDeserialize would call it.
439 // we use hook guard to protect against deadlock where hook
440 // changes syncvar, calling hook again.
441 if (NetworkServer.localClientActive && !GetSyncVarHookGuard(dirtyBit))
442 {
443 SetSyncVarHookGuard(dirtyBit, true);
444 OnChanged(oldValue, value);
445 SetSyncVarHookGuard(dirtyBit, false);
446 }
447 }
448 }
449 }

◆ GeneratedSyncVarSetter_NetworkIdentity()

void Mirror.NetworkBehaviour.GeneratedSyncVarSetter_NetworkIdentity ( NetworkIdentity  value,
ref NetworkIdentity  field,
ulong  dirtyBit,
Action< NetworkIdentity, NetworkIdentity OnChanged,
ref uint  netIdField 
)

Definition at line 399 of file NetworkBehaviour.cs.

400 {
401 if (!SyncVarNetworkIdentityEqual(value, netIdField))
402 {
403 NetworkIdentity oldValue = field;
404 SetSyncVarNetworkIdentity(value, ref field, dirtyBit, ref netIdField);
405
406 // call hook (if any)
407 if (OnChanged != null)
408 {
409 // in host mode, setting a SyncVar calls the hook directly.
410 // in client-only mode, OnDeserialize would call it.
411 // we use hook guard to protect against deadlock where hook
412 // changes syncvar, calling hook again.
413 if (NetworkServer.localClientActive && !GetSyncVarHookGuard(dirtyBit))
414 {
415 SetSyncVarHookGuard(dirtyBit, true);
416 OnChanged(oldValue, value);
417 SetSyncVarHookGuard(dirtyBit, false);
418 }
419 }
420 }
421 }

◆ GetSyncVarGameObject()

GameObject Mirror.NetworkBehaviour.GetSyncVarGameObject ( uint  netId,
ref GameObject  gameObjectField 
)
protected

Definition at line 504 of file NetworkBehaviour.cs.

505 {
506 // server always uses the field
507 if (isServer)
508 {
509 return gameObjectField;
510 }
511
512 // client always looks up based on netId because objects might get in and out of range
513 // over and over again, which shouldn't null them forever
514 if (NetworkClient.spawned.TryGetValue(netId, out NetworkIdentity identity) && identity != null)
515 return gameObjectField = identity.gameObject;
516 return null;
517 }
bool isServer
True if this object is on the server and has been spawned.
uint netId
The unique network Id of this object (unique at runtime).

◆ GetSyncVarNetworkBehaviour< T >()

T Mirror.NetworkBehaviour.GetSyncVarNetworkBehaviour< T > ( NetworkBehaviourSyncVar  syncNetBehaviour,
ref T  behaviourField 
)
protected
Type Constraints
T :NetworkBehaviour 

Definition at line 875 of file NetworkBehaviour.cs.

875 : NetworkBehaviour
876 {
877 // server always uses the field
878 if (isServer)
879 {
880 return behaviourField;
881 }
882
883 // client always looks up based on netId because objects might get in and out of range
884 // over and over again, which shouldn't null them forever
885 if (!NetworkClient.spawned.TryGetValue(syncNetBehaviour.netId, out NetworkIdentity identity))
886 {
887 return null;
888 }
889
890 behaviourField = identity.NetworkBehaviours[syncNetBehaviour.componentIndex] as T;
891 return behaviourField;
892 }

◆ GetSyncVarNetworkIdentity()

NetworkIdentity Mirror.NetworkBehaviour.GetSyncVarNetworkIdentity ( uint  netId,
ref NetworkIdentity  identityField 
)
protected

Definition at line 812 of file NetworkBehaviour.cs.

813 {
814 // server always uses the field
815 if (isServer)
816 {
817 return identityField;
818 }
819
820 // client always looks up based on netId because objects might get in and out of range
821 // over and over again, which shouldn't null them forever
822 NetworkClient.spawned.TryGetValue(netId, out identityField);
823 return identityField;
824 }

◆ InitSyncObject()

void Mirror.NetworkBehaviour.InitSyncObject ( SyncObject  syncObject)
protected

Definition at line 154 of file NetworkBehaviour.cs.

155 {
156 if (syncObject == null)
157 {
158 Debug.LogError("Uninitialized SyncObject. Manually call the constructor on your SyncList, SyncSet, SyncDictionary or SyncField<T>");
159 return;
160 }
161
162 // add it, remember the index in list (if Count=0, index=0 etc.)
163 int index = syncObjects.Count;
164 syncObjects.Add(syncObject);
165
166 // OnDirty needs to set nth bit in our dirty mask
167 ulong nthBit = 1UL << index;
168 syncObject.OnDirty = () => syncObjectDirtyBits |= nthBit;
169
170 // only record changes while we have observers.
171 // prevents ever growing .changes lists:
172 // if a monster has no observers but we keep modifing a SyncObject,
173 // then the changes would never be flushed and keep growing,
174 // because OnSerialize isn't called without observers.
175 syncObject.IsRecording = () => netIdentity.observers?.Count > 0;
176 }
NetworkIdentity netIdentity
Returns the NetworkIdentity of this object
Dictionary< int, NetworkConnectionToClient > observers
The set of network connections (players) that can see this object.

◆ IsDirty()

bool Mirror.NetworkBehaviour.IsDirty ( )

Definition at line 124 of file NetworkBehaviour.cs.

125 {
126 if (NetworkTime.localTime - lastSyncTime >= syncInterval)
127 {
128 // OR both bitmasks. != 0 if either was dirty.
129 return (syncVarDirtyBits | syncObjectDirtyBits) != 0UL;
130 }
131 return false;
132 }
float syncInterval
sync interval for OnSerialize (in seconds)

◆ OnDeserialize()

virtual void Mirror.NetworkBehaviour.OnDeserialize ( NetworkReader  reader,
bool  initialState 
)
virtual

Override to do custom deserialization (instead of SyncVars/SyncLists). Use OnSerialize too.

Definition at line 956 of file NetworkBehaviour.cs.

957 {
958 if (initialState)
959 {
960 DeSerializeObjectsAll(reader);
961 }
962 else
963 {
964 DeSerializeObjectsDelta(reader);
965 }
966
967 DeserializeSyncVars(reader, initialState);
968 }

◆ OnSerialize()

virtual bool Mirror.NetworkBehaviour.OnSerialize ( NetworkWriter  writer,
bool  initialState 
)
virtual

Override to do custom serialization (instead of SyncVars/SyncLists). Use OnDeserialize too.

Definition at line 946 of file NetworkBehaviour.cs.

947 {
948 // if initialState: write all SyncVars.
949 // otherwise write dirtyBits+dirty SyncVars
950 bool objectWritten = initialState ? SerializeObjectsAll(writer) : SerializeObjectsDelta(writer);
951 bool syncVarWritten = SerializeSyncVars(writer, initialState);
952 return objectWritten || syncVarWritten;
953 }

◆ OnStartAuthority()

virtual void Mirror.NetworkBehaviour.OnStartAuthority ( )
virtual

Like Start(), but only called for objects the client has authority over.

Definition at line 1077 of file NetworkBehaviour.cs.

1077{}

◆ OnStartClient()

virtual void Mirror.NetworkBehaviour.OnStartClient ( )
virtual

Like Start(), but only called on client and host.

Definition at line 1065 of file NetworkBehaviour.cs.

1065{}

◆ OnStartLocalPlayer()

virtual void Mirror.NetworkBehaviour.OnStartLocalPlayer ( )
virtual

Like Start(), but only called on client and host for the local player object.

Definition at line 1071 of file NetworkBehaviour.cs.

1071{}

◆ OnStartServer()

virtual void Mirror.NetworkBehaviour.OnStartServer ( )
virtual

Like Start(), but only called on server and host.

Definition at line 1059 of file NetworkBehaviour.cs.

1059{}

◆ OnStopAuthority()

virtual void Mirror.NetworkBehaviour.OnStopAuthority ( )
virtual

Stop event, only called for objects the client has authority over.

Definition at line 1080 of file NetworkBehaviour.cs.

1080{}

◆ OnStopClient()

virtual void Mirror.NetworkBehaviour.OnStopClient ( )
virtual

Stop event, only called on client and host.

Definition at line 1068 of file NetworkBehaviour.cs.

1068{}

◆ OnStopLocalPlayer()

virtual void Mirror.NetworkBehaviour.OnStopLocalPlayer ( )
virtual

Stop event, but only called on client and host for the local player object.

Definition at line 1074 of file NetworkBehaviour.cs.

1074{}

◆ OnStopServer()

virtual void Mirror.NetworkBehaviour.OnStopServer ( )
virtual

Stop event, only called on server and host.

Definition at line 1062 of file NetworkBehaviour.cs.

1062{}

◆ SendCommandInternal()

void Mirror.NetworkBehaviour.SendCommandInternal ( string  functionFullName,
NetworkWriter  writer,
int  channelId,
bool  requiresAuthority = true 
)
protected

Definition at line 179 of file NetworkBehaviour.cs.

180 {
181 // this was in Weaver before
182 // NOTE: we could remove this later to allow calling Cmds on Server
183 // to avoid Wrapper functions. a lot of people requested this.
184 if (!NetworkClient.active)
185 {
186 Debug.LogError($"Command Function {functionFullName} called on {name} without an active client.", gameObject);
187 return;
188 }
189
190 // previously we used NetworkClient.readyConnection.
191 // now we check .ready separately.
192 if (!NetworkClient.ready)
193 {
194 // Unreliable Cmds from NetworkTransform may be generated,
195 // or client may have been set NotReady intentionally, so
196 // only warn if on the reliable channel.
197 if (channelId == Channels.Reliable)
198 Debug.LogWarning($"Command Function {functionFullName} called on {name} while NetworkClient is not ready.\nThis may be ignored if client intentionally set NotReady.", gameObject);
199 return;
200 }
201
202 // local players can always send commands, regardless of authority, other objects must have authority.
203 if (!(!requiresAuthority || isLocalPlayer || hasAuthority))
204 {
205 Debug.LogWarning($"Command Function {functionFullName} called on {name} without authority.", gameObject);
206 return;
207 }
208
209 // IMPORTANT: can't use .connectionToServer here because calling
210 // a command on other objects is allowed if requireAuthority is
211 // false. other objects don't have a .connectionToServer.
212 // => so we always need to use NetworkClient.connection instead.
213 // => see also: https://github.com/vis2k/Mirror/issues/2629
214 if (NetworkClient.connection == null)
215 {
216 Debug.LogError($"Command Function {functionFullName} called on {name} with no client running.", gameObject);
217 return;
218 }
219
220 // construct the message
221 CommandMessage message = new CommandMessage
222 {
223 netId = netId,
224 componentIndex = (byte)ComponentIndex,
225 // type+func so Inventory.RpcUse != Equipment.RpcUse
226 functionHash = (ushort)functionFullName.GetStableHashCode(),
227 // segment to avoid reader allocations
228 payload = writer.ToArraySegment()
229 };
230
231 // IMPORTANT: can't use .connectionToServer here because calling
232 // a command on other objects is allowed if requireAuthority is
233 // false. other objects don't have a .connectionToServer.
234 // => so we always need to use NetworkClient.connection instead.
235 // => see also: https://github.com/vis2k/Mirror/issues/2629
236 NetworkClient.connection.Send(message, channelId);
237 }
bool isLocalPlayer
True if this object is the the client's own local player.
int ComponentIndex
Returns the index of the component on this object
bool hasAuthority
True on client if that component has been assigned to the client. E.g. player, pets,...

◆ SendRPCInternal()

void Mirror.NetworkBehaviour.SendRPCInternal ( string  functionFullName,
NetworkWriter  writer,
int  channelId,
bool  includeOwner 
)
protected

Definition at line 240 of file NetworkBehaviour.cs.

241 {
242 // this was in Weaver before
243 if (!NetworkServer.active)
244 {
245 Debug.LogError($"RPC Function {functionFullName} called on Client.", gameObject);
246 return;
247 }
248
249 // This cannot use NetworkServer.active, as that is not specific to this object.
250 if (!isServer)
251 {
252 Debug.LogWarning($"ClientRpc {functionFullName} called on un-spawned object: {name}", gameObject);
253 return;
254 }
255
256 // construct the message
257 RpcMessage message = new RpcMessage
258 {
259 netId = netId,
260 componentIndex = (byte)ComponentIndex,
261 // type+func so Inventory.RpcUse != Equipment.RpcUse
262 functionHash = (ushort)functionFullName.GetStableHashCode(),
263 // segment to avoid reader allocations
264 payload = writer.ToArraySegment()
265 };
266
267 NetworkServer.SendToReadyObservers(netIdentity, message, includeOwner, channelId);
268 }

◆ SendTargetRPCInternal()

void Mirror.NetworkBehaviour.SendTargetRPCInternal ( NetworkConnection  conn,
string  functionFullName,
NetworkWriter  writer,
int  channelId 
)
protected

Definition at line 271 of file NetworkBehaviour.cs.

272 {
273 if (!NetworkServer.active)
274 {
275 Debug.LogError($"TargetRPC {functionFullName} called on {name} when server not active", gameObject);
276 return;
277 }
278
279 if (!isServer)
280 {
281 Debug.LogWarning($"TargetRpc {functionFullName} called on {name} but that object has not been spawned or has been unspawned", gameObject);
282 return;
283 }
284
285 // connection parameter is optional. assign if null.
286 if (conn is null)
287 {
288 conn = connectionToClient;
289 }
290
291 // if still null
292 if (conn is null)
293 {
294 Debug.LogError($"TargetRPC {functionFullName} was given a null connection, make sure the object {name} has an owner or you pass in the target connection", gameObject);
295 return;
296 }
297
298 if (!(conn is NetworkConnectionToClient))
299 {
300 Debug.LogError($"TargetRPC {functionFullName} called on {name} requires a NetworkConnectionToClient but was given {conn.GetType().Name}", gameObject);
301 return;
302 }
303
304 // construct the message
305 RpcMessage message = new RpcMessage
306 {
307 netId = netId,
308 componentIndex = (byte)ComponentIndex,
309 // type+func so Inventory.RpcUse != Equipment.RpcUse
310 functionHash = (ushort)functionFullName.GetStableHashCode(),
311 // segment to avoid reader allocations
312 payload = writer.ToArraySegment()
313 };
314
315 conn.Send(message, channelId);
316 }
NetworkConnectionToClient connectionToClient
Server's network connection to the client. This is only valid for player objects on the server.

◆ SerializeObjectsAll()

bool Mirror.NetworkBehaviour.SerializeObjectsAll ( NetworkWriter  writer)

Definition at line 996 of file NetworkBehaviour.cs.

997 {
998 bool dirty = false;
999 for (int i = 0; i < syncObjects.Count; i++)
1000 {
1001 SyncObject syncObject = syncObjects[i];
1002 syncObject.OnSerializeAll(writer);
1003 dirty = true;
1004 }
1005 return dirty;
1006 }

◆ SerializeObjectsDelta()

bool Mirror.NetworkBehaviour.SerializeObjectsDelta ( NetworkWriter  writer)

Definition at line 1008 of file NetworkBehaviour.cs.

1009 {
1010 bool dirty = false;
1011 // write the mask
1012 writer.WriteULong(syncObjectDirtyBits);
1013 // serializable objects, such as synclists
1014 for (int i = 0; i < syncObjects.Count; i++)
1015 {
1016 // check dirty mask at nth bit
1017 SyncObject syncObject = syncObjects[i];
1018 if ((syncObjectDirtyBits & (1UL << i)) != 0)
1019 {
1020 syncObject.OnSerializeDelta(writer);
1021 dirty = true;
1022 }
1023 }
1024 return dirty;
1025 }

◆ SerializeSyncVars()

virtual bool Mirror.NetworkBehaviour.SerializeSyncVars ( NetworkWriter  writer,
bool  initialState 
)
protectedvirtual

Definition at line 971 of file NetworkBehaviour.cs.

972 {
973 return false;
974
975 // SyncVar are written here in subclass
976
977 // if initialState
978 // write all SyncVars
979 // else
980 // write syncVarDirtyBits
981 // write dirty SyncVars
982 }

◆ SetSyncVar< T >()

void Mirror.NetworkBehaviour.SetSyncVar< T > ( value,
ref T  fieldValue,
ulong  dirtyBit 
)
protected

Definition at line 933 of file NetworkBehaviour.cs.

934 {
935 //Debug.Log($"SetSyncVar {GetType().Name} bit:{dirtyBit} fieldValue:{value}");
936 SetSyncVarDirtyBit(dirtyBit);
937 fieldValue = value;
938 }
void SetSyncVarDirtyBit(ulong dirtyBit)
Set as dirty so that it's synced to clients again.

◆ SetSyncVarDirtyBit()

void Mirror.NetworkBehaviour.SetSyncVarDirtyBit ( ulong  dirtyBit)

Set as dirty so that it's synced to clients again.

Definition at line 118 of file NetworkBehaviour.cs.

119 {
120 syncVarDirtyBits |= dirtyBit;
121 }

◆ SetSyncVarGameObject()

void Mirror.NetworkBehaviour.SetSyncVarGameObject ( GameObject  newGameObject,
ref GameObject  gameObjectField,
ulong  dirtyBit,
ref uint  netIdField 
)
protected

Definition at line 476 of file NetworkBehaviour.cs.

477 {
478 if (GetSyncVarHookGuard(dirtyBit))
479 return;
480
481 uint newNetId = 0;
482 if (newGameObject != null)
483 {
484 NetworkIdentity identity = newGameObject.GetComponent<NetworkIdentity>();
485 if (identity != null)
486 {
487 newNetId = identity.netId;
488 if (newNetId == 0)
489 {
490 Debug.LogWarning($"SetSyncVarGameObject GameObject {newGameObject} has a zero netId. Maybe it is not spawned yet?");
491 }
492 }
493 }
494
495 //Debug.Log($"SetSyncVar GameObject {GetType().Name} bit:{dirtyBit} netfieldId:{netIdField} -> {newNetId}");
496 SetSyncVarDirtyBit(dirtyBit);
497 // assign new one on the server, and in case we ever need it on client too
498 gameObjectField = newGameObject;
499 netIdField = newNetId;
500 }

◆ SetSyncVarHookGuard()

void Mirror.NetworkBehaviour.SetSyncVarHookGuard ( ulong  dirtyBit,
bool  value 
)
protected

Definition at line 106 of file NetworkBehaviour.cs.

107 {
108 // set the bit
109 if (value)
110 syncVarHookGuard |= dirtyBit;
111 // clear the bit
112 else
113 syncVarHookGuard &= ~dirtyBit;
114 }

◆ SetSyncVarNetworkBehaviour< T >()

void Mirror.NetworkBehaviour.SetSyncVarNetworkBehaviour< T > ( newBehaviour,
ref T  behaviourField,
ulong  dirtyBit,
ref NetworkBehaviourSyncVar  syncField 
)
protected
Type Constraints
T :NetworkBehaviour 

Definition at line 846 of file NetworkBehaviour.cs.

846 : NetworkBehaviour
847 {
848 if (GetSyncVarHookGuard(dirtyBit))
849 return;
850
851 uint newNetId = 0;
852 int componentIndex = 0;
853 if (newBehaviour != null)
854 {
855 newNetId = newBehaviour.netId;
856 componentIndex = newBehaviour.ComponentIndex;
857 if (newNetId == 0)
858 {
859 Debug.LogWarning($"{nameof(SetSyncVarNetworkBehaviour)} NetworkIdentity {newBehaviour} has a zero netId. Maybe it is not spawned yet?");
860 }
861 }
862
863 syncField = new NetworkBehaviourSyncVar(newNetId, componentIndex);
864
865 SetSyncVarDirtyBit(dirtyBit);
866
867 // assign new one on the server, and in case we ever need it on client too
868 behaviourField = newBehaviour;
869
870 // Debug.Log($"SetSyncVarNetworkBehaviour NetworkIdentity {GetType().Name} bit [{dirtyBit}] netIdField:{oldField}->{syncField}");
871 }

◆ SetSyncVarNetworkIdentity()

void Mirror.NetworkBehaviour.SetSyncVarNetworkIdentity ( NetworkIdentity  newIdentity,
ref NetworkIdentity  identityField,
ulong  dirtyBit,
ref uint  netIdField 
)
protected

Definition at line 788 of file NetworkBehaviour.cs.

789 {
790 if (GetSyncVarHookGuard(dirtyBit))
791 return;
792
793 uint newNetId = 0;
794 if (newIdentity != null)
795 {
796 newNetId = newIdentity.netId;
797 if (newNetId == 0)
798 {
799 Debug.LogWarning($"SetSyncVarNetworkIdentity NetworkIdentity {newIdentity} has a zero netId. Maybe it is not spawned yet?");
800 }
801 }
802
803 //Debug.Log($"SetSyncVarNetworkIdentity NetworkIdentity {GetType().Name} bit:{dirtyBit} netIdField:{netIdField} -> {newNetId}");
804 SetSyncVarDirtyBit(dirtyBit);
805 netIdField = newNetId;
806 // assign new one on the server, and in case we ever need it on client too
807 identityField = newIdentity;
808 }

◆ SyncVarEqual< T >()

static bool Mirror.NetworkBehaviour.SyncVarEqual< T > ( value,
ref T  fieldValue 
)
staticprotected

Definition at line 923 of file NetworkBehaviour.cs.

924 {
925 // newly initialized or changed value?
926 // value.Equals(fieldValue) allocates without 'where T : IEquatable'
927 // seems like we use EqualityComparer to avoid allocations,
928 // because not all SyncVars<T> are IEquatable
929 return EqualityComparer<T>.Default.Equals(value, fieldValue);
930 }

◆ SyncVarGameObjectEqual()

static bool Mirror.NetworkBehaviour.SyncVarGameObjectEqual ( GameObject  newGameObject,
uint  netIdField 
)
static

Definition at line 455 of file NetworkBehaviour.cs.

456 {
457 uint newNetId = 0;
458 if (newGameObject != null)
459 {
460 NetworkIdentity identity = newGameObject.GetComponent<NetworkIdentity>();
461 if (identity != null)
462 {
463 newNetId = identity.netId;
464 if (newNetId == 0)
465 {
466 Debug.LogWarning($"SetSyncVarGameObject GameObject {newGameObject} has a zero netId. Maybe it is not spawned yet?");
467 }
468 }
469 }
470
471 return newNetId == netIdField;
472 }

◆ SyncVarNetworkBehaviourEqual< T >()

static bool Mirror.NetworkBehaviour.SyncVarNetworkBehaviourEqual< T > ( newBehaviour,
NetworkBehaviourSyncVar  syncField 
)
staticprotected
Type Constraints
T :NetworkBehaviour 

Definition at line 826 of file NetworkBehaviour.cs.

826 : NetworkBehaviour
827 {
828 uint newNetId = 0;
829 int newComponentIndex = 0;
830 if (newBehaviour != null)
831 {
832 newNetId = newBehaviour.netId;
833 newComponentIndex = newBehaviour.ComponentIndex;
834 if (newNetId == 0)
835 {
836 Debug.LogWarning($"SetSyncVarNetworkIdentity NetworkIdentity {newBehaviour} has a zero netId. Maybe it is not spawned yet?");
837 }
838 }
839
840 // netId changed?
841 return syncField.Equals(newNetId, newComponentIndex);
842 }

◆ SyncVarNetworkIdentityEqual()

static bool Mirror.NetworkBehaviour.SyncVarNetworkIdentityEqual ( NetworkIdentity  newIdentity,
uint  netIdField 
)
static

Definition at line 523 of file NetworkBehaviour.cs.

524 {
525 uint newNetId = 0;
526 if (newIdentity != null)
527 {
528 newNetId = newIdentity.netId;
529 if (newNetId == 0)
530 {
531 Debug.LogWarning($"SetSyncVarNetworkIdentity NetworkIdentity {newIdentity} has a zero netId. Maybe it is not spawned yet?");
532 }
533 }
534
535 // netId changed?
536 return newNetId == netIdField;
537 }

Member Data Documentation

◆ syncInterval

float Mirror.NetworkBehaviour.syncInterval = 0.1f

sync interval for OnSerialize (in seconds)

Definition at line 27 of file NetworkBehaviour.cs.

◆ syncMode

SyncMode Mirror.NetworkBehaviour.syncMode = SyncMode.Observers

sync mode for OnSerialize

Definition at line 20 of file NetworkBehaviour.cs.

◆ syncObjects

readonly List<SyncObject> Mirror.NetworkBehaviour.syncObjects = new List<SyncObject>()
protected

Definition at line 61 of file NetworkBehaviour.cs.

Property Documentation

◆ ComponentIndex

int Mirror.NetworkBehaviour.ComponentIndex
getset

Returns the index of the component on this object

Definition at line 74 of file NetworkBehaviour.cs.

74{ get; internal set; }

◆ connectionToClient

NetworkConnectionToClient Mirror.NetworkBehaviour.connectionToClient
get

Server's network connection to the client. This is only valid for player objects on the server.

Definition at line 58 of file NetworkBehaviour.cs.

◆ connectionToServer

NetworkConnection Mirror.NetworkBehaviour.connectionToServer
get

Client's network connection to the server. This is only valid for player objects on the client.

Definition at line 55 of file NetworkBehaviour.cs.

◆ hasAuthority

bool Mirror.NetworkBehaviour.hasAuthority
get

True on client if that component has been assigned to the client. E.g. player, pets, henchmen.

Definition at line 48 of file NetworkBehaviour.cs.

◆ isClient

bool Mirror.NetworkBehaviour.isClient
get

True if this object is on the client and has been spawned by the server.

Definition at line 36 of file NetworkBehaviour.cs.

◆ isClientOnly

bool Mirror.NetworkBehaviour.isClientOnly
get

True if this object is on the client-only, not host.

Definition at line 45 of file NetworkBehaviour.cs.

◆ isLocalPlayer

bool Mirror.NetworkBehaviour.isLocalPlayer
get

True if this object is the the client's own local player.

Definition at line 39 of file NetworkBehaviour.cs.

◆ isServer

bool Mirror.NetworkBehaviour.isServer
get

True if this object is on the server and has been spawned.

Definition at line 33 of file NetworkBehaviour.cs.

◆ isServerOnly

bool Mirror.NetworkBehaviour.isServerOnly
get

True if this object is on the server-only, not host.

Definition at line 42 of file NetworkBehaviour.cs.

◆ netId

uint Mirror.NetworkBehaviour.netId
get

The unique network Id of this object (unique at runtime).

Definition at line 51 of file NetworkBehaviour.cs.

◆ netIdentity

NetworkIdentity Mirror.NetworkBehaviour.netIdentity
getset

Returns the NetworkIdentity of this object

Definition at line 71 of file NetworkBehaviour.cs.

71{ get; internal set; }

◆ syncVarDirtyBits

ulong Mirror.NetworkBehaviour.syncVarDirtyBits
getprotected

Definition at line 88 of file NetworkBehaviour.cs.

88{ get; private set; }