Skip to content

Commit 3486ca9

Browse files
committed
chore: Swap ChildNetworkBehaviours to a Dictionary
1 parent e02c3e7 commit 3486ca9

7 files changed

Lines changed: 150 additions & 232 deletions

File tree

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

Lines changed: 31 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -379,41 +379,21 @@ internal void __endSendRpc(ref FastBufferWriter bufferWriter, uint rpcMethodId,
379379

380380
if (rpcParams.Send.Target == null)
381381
{
382-
switch (defaultTarget)
382+
rpcParams.Send.Target = defaultTarget switch
383383
{
384-
case SendTo.Everyone:
385-
rpcParams.Send.Target = RpcTarget.Everyone;
386-
break;
387-
case SendTo.Owner:
388-
rpcParams.Send.Target = RpcTarget.Owner;
389-
break;
390-
case SendTo.Server:
391-
rpcParams.Send.Target = RpcTarget.Server;
392-
break;
393-
case SendTo.NotServer:
394-
rpcParams.Send.Target = RpcTarget.NotServer;
395-
break;
396-
case SendTo.NotMe:
397-
rpcParams.Send.Target = RpcTarget.NotMe;
398-
break;
399-
case SendTo.NotOwner:
400-
rpcParams.Send.Target = RpcTarget.NotOwner;
401-
break;
402-
case SendTo.Me:
403-
rpcParams.Send.Target = RpcTarget.Me;
404-
break;
405-
case SendTo.ClientsAndHost:
406-
rpcParams.Send.Target = RpcTarget.ClientsAndHost;
407-
break;
408-
case SendTo.Authority:
409-
rpcParams.Send.Target = RpcTarget.Authority;
410-
break;
411-
case SendTo.NotAuthority:
412-
rpcParams.Send.Target = RpcTarget.NotAuthority;
413-
break;
414-
case SendTo.SpecifiedInParams:
415-
throw new RpcException("This method requires a runtime-specified send target.");
416-
}
384+
SendTo.Everyone => RpcTarget.Everyone,
385+
SendTo.Owner => RpcTarget.Owner,
386+
SendTo.Server => RpcTarget.Server,
387+
SendTo.NotServer => RpcTarget.NotServer,
388+
SendTo.NotMe => RpcTarget.NotMe,
389+
SendTo.NotOwner => RpcTarget.NotOwner,
390+
SendTo.Me => RpcTarget.Me,
391+
SendTo.ClientsAndHost => RpcTarget.ClientsAndHost,
392+
SendTo.Authority => RpcTarget.Authority,
393+
SendTo.NotAuthority => RpcTarget.NotAuthority,
394+
SendTo.SpecifiedInParams => throw new RpcException("This method requires a runtime-specified send target."),
395+
_ => throw new RpcException("This method requires a runtime-specified send target."),
396+
};
417397
}
418398
else if (defaultTarget != SendTo.SpecifiedInParams && !attributeParams.AllowTargetOverride)
419399
{
@@ -473,15 +453,8 @@ public NetworkManager NetworkManager
473453
#pragma warning disable IDE0001
474454
/// <summary>
475455
/// Provides access to the various <see cref="SendTo"/> targets at runtime, as well as
476-
/// runtime-bound targets like <see cref="Unity.Netcode.RpcTarget.Single"/>,
477-
/// <see cref="Unity.Netcode.RpcTarget.Group(NativeArray{ulong})"/>,
478-
/// <see cref="Unity.Netcode.RpcTarget.Group(NativeList{ulong})"/>,
479-
/// <see cref="Unity.Netcode.RpcTarget.Group(ulong[])"/>,
480-
/// <see cref="Unity.Netcode.RpcTarget.Group{T}(T)"/>, <see cref="Unity.Netcode.RpcTarget.Not(ulong)"/>,
481-
/// <see cref="Unity.Netcode.RpcTarget.Not(NativeArray{ulong})"/>,
482-
/// <see cref="Unity.Netcode.RpcTarget.Not(NativeList{ulong})"/>,
483-
/// <see cref="Unity.Netcode.RpcTarget.Not(ulong[])"/>, and
484-
/// <see cref="Unity.Netcode.RpcTarget.Not{T}(T)"/>.
456+
/// runtime-bound targets like <see cref="RpcTarget.Single"/>, <see cref="RpcTarget.Group{T}"/>, and
457+
/// <see cref="RpcTarget.Not{T}"/>.
485458
/// </summary>
486459
#pragma warning restore IDE0001
487460
public RpcTarget RpcTarget { get; private set; }
@@ -623,11 +596,6 @@ public NetworkObject NetworkObject
623596
/// </summary>
624597
public ushort NetworkBehaviourId { get; internal set; }
625598

626-
/// <summary>
627-
/// Internally caches the Id of this behaviour in a NetworkObject. Makes look-up faster
628-
/// </summary>
629-
internal ushort NetworkBehaviourIdCache = 0;
630-
631599
/// <summary>
632600
/// Returns the NetworkBehaviour with a given BehaviourId for the current NetworkObject.
633601
/// </summary>
@@ -753,7 +721,7 @@ internal virtual void InternalOnNetworkPreSpawn(ref NetworkManager networkManage
753721
/// <summary>
754722
/// Handles pre-spawn related initializations.
755723
/// Invokes any <see cref="InternalOnNetworkPreSpawn"/> subscriptions.
756-
/// Finally invokes <see cref="OnNetworkPreSpawn(ref NetworkManager)"/>.
724+
/// Finally invokes <see cref="OnNetworkPreSpawn"/>.
757725
/// </summary>
758726
internal void NetworkPreSpawn(ref NetworkManager networkManager, NetworkObject networkObject)
759727
{
@@ -1101,14 +1069,14 @@ internal void InitializeVariables()
11011069
// placed NetworkObject in an already loaded scene that has already been
11021070
// used within a network session =or= if this is a pooled NetworkObject
11031071
// that is being repurposed.
1104-
for (int i = 0; i < NetworkVariableFields.Count; i++)
1072+
foreach (var variable in NetworkVariableFields)
11051073
{
11061074
// If already initialized, then skip
1107-
if (NetworkVariableFields[i].HasBeenInitialized)
1075+
if (variable.HasBeenInitialized)
11081076
{
11091077
continue;
11101078
}
1111-
NetworkVariableFields[i].Initialize(this);
1079+
variable.Initialize(this);
11121080
}
11131081
// Exit early as we don't need to run through the rest of this initialization
11141082
// process
@@ -1136,9 +1104,8 @@ internal void InitializeVariables()
11361104
for (int i = 0; i < NetworkVariableFields.Count; i++)
11371105
{
11381106
var networkDelivery = MessageDeliveryType<NetworkVariableDeltaMessage>.DefaultDelivery;
1139-
if (!firstLevelIndex.ContainsKey(networkDelivery))
1107+
if (firstLevelIndex.TryAdd(networkDelivery, secondLevelCounter))
11401108
{
1141-
firstLevelIndex.Add(networkDelivery, secondLevelCounter);
11421109
m_DeliveryTypesForNetworkVariableGroups.Add(networkDelivery);
11431110
secondLevelCounter++;
11441111
}
@@ -1166,9 +1133,8 @@ internal void PostNetworkVariableWrite(bool forced = false)
11661133
{
11671134
// Mark every variable as no longer dirty. We just spawned the object and whatever the game code did
11681135
// during OnNetworkSpawn has been sent and needs to be cleared
1169-
for (int i = 0; i < NetworkVariableFields.Count; i++)
1136+
foreach (var networkVariable in NetworkVariableFields)
11701137
{
1171-
var networkVariable = NetworkVariableFields[i];
11721138
if (networkVariable.IsDirty())
11731139
{
11741140
if (networkVariable.CanSend())
@@ -1302,9 +1268,8 @@ internal void NetworkVariableUpdate(ulong targetClientId, bool forceSend = false
13021268
private bool CouldHaveDirtyNetworkVariables()
13031269
{
13041270
// TODO: There should be a better way by reading one dirty variable vs. 'n'
1305-
for (int i = 0; i < NetworkVariableFields.Count; i++)
1271+
foreach (var networkVariable in NetworkVariableFields)
13061272
{
1307-
var networkVariable = NetworkVariableFields[i];
13081273
if (networkVariable.IsDirty())
13091274
{
13101275
if (networkVariable.CanSend())
@@ -1330,12 +1295,12 @@ private bool CouldHaveDirtyNetworkVariables()
13301295
/// </remarks>
13311296
internal void UpdateNetworkVariableOnOwnershipChanged()
13321297
{
1333-
for (int j = 0; j < NetworkVariableFields.Count; j++)
1298+
foreach (var variable in NetworkVariableFields)
13341299
{
13351300
// Only invoke OnInitialize on NetworkVariables the owner can write to
1336-
if (NetworkVariableFields[j].CanClientWrite(OwnerClientId))
1301+
if (variable.CanClientWrite(OwnerClientId))
13371302
{
1338-
NetworkVariableFields[j].OnInitialize();
1303+
variable.OnInitialize();
13391304
}
13401305
}
13411306
}
@@ -1355,15 +1320,15 @@ internal void MarkVariablesDirty(bool dirty)
13551320
/// </summary>
13561321
internal void MarkOwnerReadDirtyAndCheckOwnerWriteIsDirty()
13571322
{
1358-
for (int j = 0; j < NetworkVariableFields.Count; j++)
1323+
foreach (var variable in NetworkVariableFields)
13591324
{
1360-
if (NetworkVariableFields[j].ReadPerm == NetworkVariableReadPermission.Owner)
1325+
if (variable.ReadPerm == NetworkVariableReadPermission.Owner)
13611326
{
1362-
NetworkVariableFields[j].SetDirty(true);
1327+
variable.SetDirty(true);
13631328
}
1364-
if (NetworkVariableFields[j].WritePerm == NetworkVariableWritePermission.Owner)
1329+
if (variable.WritePerm == NetworkVariableWritePermission.Owner)
13651330
{
1366-
NetworkVariableFields[j].OnCheckIsDirtyState();
1331+
variable.OnCheckIsDirtyState();
13671332
}
13681333
}
13691334
}

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviourUpdater.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ internal void ProcessDirtyObjectServer(NetworkObject dirtyObj, bool forceSend)
5656
if (m_NetworkManager.DistributedAuthorityMode || dirtyObj.IsNetworkVisibleTo(client.ClientId))
5757
{
5858
// Sync just the variables for just the objects this client sees
59-
for (int k = 0; k < dirtyObj.ChildNetworkBehaviours.Count; k++)
59+
foreach(var behaviour in dirtyObj.ChildNetworkBehaviours.Values)
6060
{
61-
dirtyObj.ChildNetworkBehaviours[k].NetworkVariableUpdate(client.ClientId, forceSend);
61+
behaviour.NetworkVariableUpdate(client.ClientId, forceSend);
6262
}
6363
}
6464
}
@@ -73,9 +73,9 @@ internal void ProcessDirtyObjectServer(NetworkObject dirtyObj, bool forceSend)
7373
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7474
internal void ProcessDirtyObjectClient(NetworkObject dirtyObj, bool forceSend)
7575
{
76-
for (int k = 0; k < dirtyObj.ChildNetworkBehaviours.Count; k++)
76+
foreach(var behaviour in dirtyObj.ChildNetworkBehaviours.Values)
7777
{
78-
dirtyObj.ChildNetworkBehaviours[k].NetworkVariableUpdate(NetworkManager.ServerClientId, forceSend);
78+
behaviour.NetworkVariableUpdate(NetworkManager.ServerClientId, forceSend);
7979
}
8080
}
8181

@@ -86,9 +86,8 @@ internal void ProcessDirtyObjectClient(NetworkObject dirtyObj, bool forceSend)
8686
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8787
internal void PostProcessDirtyObject(NetworkObject dirtyObj)
8888
{
89-
for (int k = 0; k < dirtyObj.ChildNetworkBehaviours.Count; k++)
89+
foreach(var behaviour in dirtyObj.ChildNetworkBehaviours.Values)
9090
{
91-
var behaviour = dirtyObj.ChildNetworkBehaviours[k];
9291
for (int i = 0; i < behaviour.NetworkVariableFields.Count; i++)
9392
{
9493
// Set to true for NetworkVariable to ignore duplication of the
@@ -114,7 +113,7 @@ internal void PostProcessDirtyObject(NetworkObject dirtyObj)
114113
[MethodImpl(MethodImplOptions.AggressiveInlining)]
115114
internal void ResetDirtyObject(NetworkObject dirtyObj, bool forceSend)
116115
{
117-
foreach (var behaviour in dirtyObj.ChildNetworkBehaviours)
116+
foreach (var behaviour in dirtyObj.ChildNetworkBehaviours.Values)
118117
{
119118
behaviour.PostNetworkVariableWrite(forceSend);
120119
}
@@ -164,9 +163,9 @@ internal void ProcessDirtyObject(NetworkObject networkObject, bool forceSend)
164163
}
165164

166165
// Pre-variable update
167-
for (int k = 0; k < networkObject.ChildNetworkBehaviours.Count; k++)
166+
foreach (var behaviour in networkObject.ChildNetworkBehaviours.Values)
168167
{
169-
networkObject.ChildNetworkBehaviours[k].PreVariableUpdate();
168+
behaviour.PreVariableUpdate();
170169
}
171170

172171
// Server sends updates to all clients where a client sends updates

0 commit comments

Comments
 (0)