Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Changed

- Improve handling of destroyed NetworkBehaviours. (#3953)
- Hardened error handling and recovery during `NetworkObject` spawn. (#3941)
- Replaced Debug usage by NetcodeLog on `NetworkSpawnManager` and `NetworkObject`. (#3933)
- Improved performance of `NetworkBehaviour`. (#3915)
Expand Down
97 changes: 31 additions & 66 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,41 +379,21 @@ internal void __endSendRpc(ref FastBufferWriter bufferWriter, uint rpcMethodId,

if (rpcParams.Send.Target == null)
{
switch (defaultTarget)
rpcParams.Send.Target = defaultTarget switch
{
case SendTo.Everyone:
rpcParams.Send.Target = RpcTarget.Everyone;
break;
case SendTo.Owner:
rpcParams.Send.Target = RpcTarget.Owner;
break;
case SendTo.Server:
rpcParams.Send.Target = RpcTarget.Server;
break;
case SendTo.NotServer:
rpcParams.Send.Target = RpcTarget.NotServer;
break;
case SendTo.NotMe:
rpcParams.Send.Target = RpcTarget.NotMe;
break;
case SendTo.NotOwner:
rpcParams.Send.Target = RpcTarget.NotOwner;
break;
case SendTo.Me:
rpcParams.Send.Target = RpcTarget.Me;
break;
case SendTo.ClientsAndHost:
rpcParams.Send.Target = RpcTarget.ClientsAndHost;
break;
case SendTo.Authority:
rpcParams.Send.Target = RpcTarget.Authority;
break;
case SendTo.NotAuthority:
rpcParams.Send.Target = RpcTarget.NotAuthority;
break;
case SendTo.SpecifiedInParams:
throw new RpcException("This method requires a runtime-specified send target.");
}
SendTo.Everyone => RpcTarget.Everyone,
SendTo.Owner => RpcTarget.Owner,
SendTo.Server => RpcTarget.Server,
SendTo.NotServer => RpcTarget.NotServer,
SendTo.NotMe => RpcTarget.NotMe,
SendTo.NotOwner => RpcTarget.NotOwner,
SendTo.Me => RpcTarget.Me,
SendTo.ClientsAndHost => RpcTarget.ClientsAndHost,
SendTo.Authority => RpcTarget.Authority,
SendTo.NotAuthority => RpcTarget.NotAuthority,
SendTo.SpecifiedInParams => throw new RpcException("This method requires a runtime-specified send target."),
_ => throw new RpcException("This method requires a runtime-specified send target."),
};
}
else if (defaultTarget != SendTo.SpecifiedInParams && !attributeParams.AllowTargetOverride)
{
Expand Down Expand Up @@ -473,15 +453,8 @@ public NetworkManager NetworkManager
#pragma warning disable IDE0001
/// <summary>
/// Provides access to the various <see cref="SendTo"/> targets at runtime, as well as
/// runtime-bound targets like <see cref="Unity.Netcode.RpcTarget.Single"/>,
/// <see cref="Unity.Netcode.RpcTarget.Group(NativeArray{ulong})"/>,
/// <see cref="Unity.Netcode.RpcTarget.Group(NativeList{ulong})"/>,
/// <see cref="Unity.Netcode.RpcTarget.Group(ulong[])"/>,
/// <see cref="Unity.Netcode.RpcTarget.Group{T}(T)"/>, <see cref="Unity.Netcode.RpcTarget.Not(ulong)"/>,
/// <see cref="Unity.Netcode.RpcTarget.Not(NativeArray{ulong})"/>,
/// <see cref="Unity.Netcode.RpcTarget.Not(NativeList{ulong})"/>,
/// <see cref="Unity.Netcode.RpcTarget.Not(ulong[])"/>, and
/// <see cref="Unity.Netcode.RpcTarget.Not{T}(T)"/>.
/// runtime-bound targets like <see cref="RpcTarget.Single"/>, <see cref="RpcTarget.Group{T}"/>, and
/// <see cref="RpcTarget.Not{T}"/>.
/// </summary>
#pragma warning restore IDE0001
public RpcTarget RpcTarget { get; private set; }
Expand Down Expand Up @@ -623,11 +596,6 @@ public NetworkObject NetworkObject
/// </summary>
public ushort NetworkBehaviourId { get; internal set; }

/// <summary>
/// Internally caches the Id of this behaviour in a NetworkObject. Makes look-up faster
/// </summary>
internal ushort NetworkBehaviourIdCache = 0;

/// <summary>
/// Returns the NetworkBehaviour with a given BehaviourId for the current NetworkObject.
/// </summary>
Expand Down Expand Up @@ -789,7 +757,7 @@ internal virtual void InternalOnNetworkPreSpawn(ref NetworkManager networkManage
/// <summary>
/// Handles pre-spawn related initializations.
/// Invokes any <see cref="InternalOnNetworkPreSpawn"/> subscriptions.
/// Finally invokes <see cref="OnNetworkPreSpawn(ref NetworkManager)"/>.
/// Finally invokes <see cref="OnNetworkPreSpawn"/>.
/// </summary>
internal void NetworkPreSpawn(ref NetworkManager networkManager, NetworkObject networkObject)
{
Expand Down Expand Up @@ -1137,14 +1105,14 @@ internal void InitializeVariables()
// placed NetworkObject in an already loaded scene that has already been
// used within a network session =or= if this is a pooled NetworkObject
// that is being repurposed.
for (int i = 0; i < NetworkVariableFields.Count; i++)
foreach (var variable in NetworkVariableFields)
{
// If already initialized, then skip
if (NetworkVariableFields[i].HasBeenInitialized)
if (variable.HasBeenInitialized)
{
continue;
}
NetworkVariableFields[i].Initialize(this);
variable.Initialize(this);
}
// Exit early as we don't need to run through the rest of this initialization
// process
Expand Down Expand Up @@ -1172,9 +1140,8 @@ internal void InitializeVariables()
for (int i = 0; i < NetworkVariableFields.Count; i++)
{
var networkDelivery = MessageDeliveryType<NetworkVariableDeltaMessage>.DefaultDelivery;
if (!firstLevelIndex.ContainsKey(networkDelivery))
if (firstLevelIndex.TryAdd(networkDelivery, secondLevelCounter))
{
firstLevelIndex.Add(networkDelivery, secondLevelCounter);
m_DeliveryTypesForNetworkVariableGroups.Add(networkDelivery);
secondLevelCounter++;
}
Expand Down Expand Up @@ -1202,9 +1169,8 @@ internal void PostNetworkVariableWrite(bool forced = false)
{
// Mark every variable as no longer dirty. We just spawned the object and whatever the game code did
// during OnNetworkSpawn has been sent and needs to be cleared
for (int i = 0; i < NetworkVariableFields.Count; i++)
foreach (var networkVariable in NetworkVariableFields)
{
var networkVariable = NetworkVariableFields[i];
if (networkVariable.IsDirty())
{
if (networkVariable.CanSend())
Expand Down Expand Up @@ -1338,9 +1304,8 @@ internal void NetworkVariableUpdate(ulong targetClientId, bool forceSend = false
private bool CouldHaveDirtyNetworkVariables()
{
// TODO: There should be a better way by reading one dirty variable vs. 'n'
for (int i = 0; i < NetworkVariableFields.Count; i++)
foreach (var networkVariable in NetworkVariableFields)
{
var networkVariable = NetworkVariableFields[i];
if (networkVariable.IsDirty())
{
if (networkVariable.CanSend())
Expand All @@ -1366,12 +1331,12 @@ private bool CouldHaveDirtyNetworkVariables()
/// </remarks>
internal void UpdateNetworkVariableOnOwnershipChanged()
{
for (int j = 0; j < NetworkVariableFields.Count; j++)
foreach (var variable in NetworkVariableFields)
{
// Only invoke OnInitialize on NetworkVariables the owner can write to
if (NetworkVariableFields[j].CanClientWrite(OwnerClientId))
if (variable.CanClientWrite(OwnerClientId))
{
NetworkVariableFields[j].OnInitialize();
variable.OnInitialize();
}
}
}
Expand All @@ -1391,15 +1356,15 @@ internal void MarkVariablesDirty(bool dirty)
/// </summary>
internal void MarkOwnerReadDirtyAndCheckOwnerWriteIsDirty()
{
for (int j = 0; j < NetworkVariableFields.Count; j++)
foreach (var variable in NetworkVariableFields)
{
if (NetworkVariableFields[j].ReadPerm == NetworkVariableReadPermission.Owner)
if (variable.ReadPerm == NetworkVariableReadPermission.Owner)
{
NetworkVariableFields[j].SetDirty(true);
variable.SetDirty(true);
}
if (NetworkVariableFields[j].WritePerm == NetworkVariableWritePermission.Owner)
if (variable.WritePerm == NetworkVariableWritePermission.Owner)
{
NetworkVariableFields[j].OnCheckIsDirtyState();
variable.OnCheckIsDirtyState();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ internal void ProcessDirtyObjectServer(NetworkObject dirtyObj, bool forceSend)
if (m_NetworkManager.DistributedAuthorityMode || dirtyObj.IsNetworkVisibleTo(client.ClientId))
{
// Sync just the variables for just the objects this client sees
for (int k = 0; k < dirtyObj.ChildNetworkBehaviours.Count; k++)
foreach (var behaviour in dirtyObj.ChildNetworkBehaviours.Values)
{
dirtyObj.ChildNetworkBehaviours[k].NetworkVariableUpdate(client.ClientId, forceSend);
behaviour.NetworkVariableUpdate(client.ClientId, forceSend);
}
}
}
Expand All @@ -73,9 +73,9 @@ internal void ProcessDirtyObjectServer(NetworkObject dirtyObj, bool forceSend)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void ProcessDirtyObjectClient(NetworkObject dirtyObj, bool forceSend)
{
for (int k = 0; k < dirtyObj.ChildNetworkBehaviours.Count; k++)
foreach (var behaviour in dirtyObj.ChildNetworkBehaviours.Values)
{
dirtyObj.ChildNetworkBehaviours[k].NetworkVariableUpdate(NetworkManager.ServerClientId, forceSend);
behaviour.NetworkVariableUpdate(NetworkManager.ServerClientId, forceSend);
}
}

Expand All @@ -86,9 +86,8 @@ internal void ProcessDirtyObjectClient(NetworkObject dirtyObj, bool forceSend)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void PostProcessDirtyObject(NetworkObject dirtyObj)
{
for (int k = 0; k < dirtyObj.ChildNetworkBehaviours.Count; k++)
foreach (var behaviour in dirtyObj.ChildNetworkBehaviours.Values)
{
var behaviour = dirtyObj.ChildNetworkBehaviours[k];
for (int i = 0; i < behaviour.NetworkVariableFields.Count; i++)
{
// Set to true for NetworkVariable to ignore duplication of the
Expand All @@ -114,7 +113,7 @@ internal void PostProcessDirtyObject(NetworkObject dirtyObj)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void ResetDirtyObject(NetworkObject dirtyObj, bool forceSend)
{
foreach (var behaviour in dirtyObj.ChildNetworkBehaviours)
foreach (var behaviour in dirtyObj.ChildNetworkBehaviours.Values)
{
behaviour.PostNetworkVariableWrite(forceSend);
}
Expand Down Expand Up @@ -164,9 +163,9 @@ internal void ProcessDirtyObject(NetworkObject networkObject, bool forceSend)
}

// Pre-variable update
for (int k = 0; k < networkObject.ChildNetworkBehaviours.Count; k++)
foreach (var behaviour in networkObject.ChildNetworkBehaviours.Values)
{
networkObject.ChildNetworkBehaviours[k].PreVariableUpdate();
behaviour.PreVariableUpdate();
}

// Server sends updates to all clients where a client sends updates
Expand Down
Loading