-
Notifications
You must be signed in to change notification settings - Fork 460
chore: [Optimize bitfield accessors] NetworkTransform #3907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
noellie-velez
wants to merge
2
commits into
develop-2.0.0
Choose a base branch
from
chore/simplify-accessors-network-transform
base: develop-2.0.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+30
−28
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1621,7 +1621,7 @@ internal bool SynchronizeScale | |
| public bool CanCommitToTransform { get; protected set; } | ||
|
|
||
| /// <summary> | ||
| /// Internally used by <see cref="NetworkTransform"/> to keep track of the <see cref="NetworkManager"/> instance assigned to this | ||
| /// Internally used by <see cref="NetworkTransform"/> to keep track of the <see cref="NetworkManager"/> instance assigned to | ||
| /// this <see cref="NetworkBehaviour"/> derived class instance. | ||
| /// </summary> | ||
| protected NetworkManager m_CachedNetworkManager; | ||
|
|
@@ -1852,6 +1852,7 @@ private bool ShouldSynchronizeHalfFloat(ulong targetClientId) | |
| if (!IsServerAuthoritative() && NetworkObject.OwnerClientId == targetClientId) | ||
| { | ||
| // In distributed authority mode we want to synchronize the half float if we are the owner. | ||
| // TODO do we have a cached NetworkManager here? Should we create one? | ||
| return (!NetworkManager.DistributedAuthorityMode && NetworkObject.IsOwnedByServer) || (NetworkManager.DistributedAuthorityMode); | ||
| } | ||
| return true; | ||
|
|
@@ -3537,7 +3538,7 @@ private void ApplyPlayerTransformState() | |
|
|
||
| /// <summary> | ||
| /// For dynamically spawned NetworkObjects, when the non-authority instance's client is already connected and | ||
| /// the SynchronizeState is still pending synchronization then we want to finalize the synchornization at this time. | ||
| /// the SynchronizeState is still pending synchronization then we want to finalize the synchronization at this time. | ||
| /// </summary> | ||
| protected internal override void InternalOnNetworkPostSpawn() | ||
| { | ||
|
|
@@ -3550,7 +3551,7 @@ protected internal override void InternalOnNetworkPostSpawn() | |
| // Then we want to: | ||
| // - Force the "IsSynchronizing" flag so the NetworkTransform has its state updated properly and runs through the initialization again. | ||
| // - Make sure the SynchronizingState is updated to the instantiated prefab's default flags/settings. | ||
| if (NetworkManager.IsServer && !NetworkManager.DistributedAuthorityMode && !IsOwner && !OnIsServerAuthoritative() && !SynchronizeState.IsSynchronizing) | ||
| if (m_CachedNetworkManager.IsServer && !m_CachedNetworkManager.DistributedAuthorityMode && !IsOwner && !OnIsServerAuthoritative() && !SynchronizeState.IsSynchronizing) | ||
| { | ||
| // Handle the first/root NetworkTransform slightly differently to have a sequenced synchronization of like authority nested NetworkTransform components | ||
| if (m_IsFirstNetworkTransform) | ||
|
|
@@ -3578,7 +3579,7 @@ protected internal override void InternalOnNetworkPostSpawn() | |
| } | ||
|
|
||
| // Standard non-authority synchronization is handled here | ||
| if (!CanCommitToTransform && NetworkManager.IsConnectedClient && SynchronizeState.IsSynchronizing) | ||
| if (!CanCommitToTransform && m_CachedNetworkManager.IsConnectedClient && SynchronizeState.IsSynchronizing) | ||
| { | ||
| NonAuthorityFinalizeSynchronization(); | ||
| } | ||
|
|
@@ -3631,14 +3632,14 @@ internal override void InternalOnNetworkPreSpawn(ref NetworkManager networkManag | |
| public override void OnNetworkSpawn() | ||
| { | ||
| m_ParentedChildren.Clear(); | ||
| m_CachedNetworkManager = NetworkManager; | ||
|
|
||
| Initialize(); | ||
|
|
||
| if (CanCommitToTransform && !SwitchTransformSpaceWhenParented) | ||
| { | ||
| SetState(GetSpaceRelativePosition(), GetSpaceRelativeRotation(), GetScale(), false); | ||
| } | ||
| base.OnNetworkSpawn(); | ||
| } | ||
|
|
||
| private void CleanUpOnDestroyOrDespawn() | ||
|
|
@@ -3651,7 +3652,7 @@ private void CleanUpOnDestroyOrDespawn() | |
| #endif | ||
| if (m_CachedNetworkObject != null) | ||
| { | ||
| NetworkManager?.NetworkTransformRegistration(m_CachedNetworkObject, forUpdate, false); | ||
| m_CachedNetworkManager.NetworkTransformRegistration(m_CachedNetworkObject, forUpdate, false); | ||
| } | ||
|
|
||
| DeregisterForTickUpdate(this); | ||
|
|
@@ -3697,7 +3698,7 @@ protected virtual void OnInitialize(ref NetworkVariable<NetworkTransformState> r | |
| /// </summary> | ||
| private void ResetInterpolatedStateToCurrentAuthoritativeState() | ||
| { | ||
| var serverTime = NetworkManager.ServerTime.Time; | ||
| var serverTime = m_CachedNetworkManager.ServerTime.Time; | ||
| #if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D | ||
| var position = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetPosition() : GetSpaceRelativePosition(); | ||
| var rotation = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetRotation() : GetSpaceRelativeRotation(); | ||
|
|
@@ -3719,7 +3720,7 @@ private void ResetInterpolatedStateToCurrentAuthoritativeState() | |
| } | ||
| private NetworkObject m_CachedNetworkObject; | ||
| /// <summary> | ||
| /// The internal initialzation method to allow for internal API adjustments | ||
| /// The internal initialization method to allow for internal API adjustments | ||
| /// </summary> | ||
| /// <param name="isOwnershipChange"></param> | ||
| private void InternalInitialization(bool isOwnershipChange = false) | ||
|
|
@@ -3731,7 +3732,7 @@ private void InternalInitialization(bool isOwnershipChange = false) | |
| m_CachedNetworkObject = NetworkObject; | ||
|
|
||
| // Determine if this is the first NetworkTransform in the associated NetworkObject's list | ||
| m_IsFirstNetworkTransform = NetworkObject.NetworkTransforms[0] == this; | ||
| m_IsFirstNetworkTransform = m_CachedNetworkObject.NetworkTransforms[0] == this; | ||
|
|
||
| if (m_CachedNetworkManager && m_CachedNetworkManager.DistributedAuthorityMode) | ||
| { | ||
|
|
@@ -3755,9 +3756,9 @@ private void InternalInitialization(bool isOwnershipChange = false) | |
| var currentPosition = GetSpaceRelativePosition(); | ||
| var currentRotation = GetSpaceRelativeRotation(); | ||
|
|
||
| if (NetworkManager.DistributedAuthorityMode) | ||
| if (m_CachedNetworkManager.DistributedAuthorityMode) | ||
| { | ||
| RegisterNetworkManagerForTickUpdate(NetworkManager); | ||
| RegisterNetworkManagerForTickUpdate(m_CachedNetworkManager); | ||
| } | ||
|
|
||
| #if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D | ||
|
|
@@ -3941,7 +3942,7 @@ internal override void InternalOnNetworkObjectParentChanged(NetworkObject parent | |
| if (LastTickSync == m_LocalAuthoritativeNetworkState.GetNetworkTick()) | ||
| { | ||
| m_InternalCurrentPosition = m_LastStateTargetPosition = GetSpaceRelativePosition(); | ||
| m_PositionInterpolator.ResetTo(m_PositionInterpolator.Parent, m_InternalCurrentPosition, NetworkManager.ServerTime.Time); | ||
| m_PositionInterpolator.ResetTo(m_PositionInterpolator.Parent, m_InternalCurrentPosition, m_CachedNetworkManager.ServerTime.Time); | ||
| if (InLocalSpace) | ||
| { | ||
| transform.localPosition = m_InternalCurrentPosition; | ||
|
|
@@ -3973,7 +3974,7 @@ internal override void InternalOnNetworkObjectParentChanged(NetworkObject parent | |
| { | ||
| m_InternalCurrentRotation = GetSpaceRelativeRotation(); | ||
| m_TargetRotation = m_InternalCurrentRotation.eulerAngles; | ||
| m_RotationInterpolator.ResetTo(m_RotationInterpolator.Parent, m_InternalCurrentRotation, NetworkManager.ServerTime.Time); | ||
| m_RotationInterpolator.ResetTo(m_RotationInterpolator.Parent, m_InternalCurrentRotation, m_CachedNetworkManager.ServerTime.Time); | ||
| if (InLocalSpace) | ||
| { | ||
| transform.localRotation = m_InternalCurrentRotation; | ||
|
|
@@ -4596,7 +4597,7 @@ internal void TransformStateUpdate() | |
| { | ||
| // TODO: Investigate where this state should be applied or just discarded. | ||
| // For now, discard the state if we assumed ownership. | ||
| // Debug.Log($"[Client-{NetworkManager.LocalClientId}] Ignoring inbound update from Client-{0} and parentUpdated:{isParentingDirective}!"); | ||
| // Debug.Log($"[Client-{m_CachedNetworkManager.LocalClientId}] Ignoring inbound update from Client-{0} and parentUpdated:{isParentingDirective}!"); | ||
| return; | ||
| } | ||
| // Store the previous/old state | ||
|
|
@@ -4653,17 +4654,17 @@ private void UpdateTransformState() | |
| { | ||
| continue; | ||
| } | ||
| if (!NetworkObject.Observers.Contains(clientId)) | ||
| if (!m_CachedNetworkObject.Observers.Contains(clientId)) | ||
| { | ||
| continue; | ||
| } | ||
| NetworkManager.MessageManager.SendMessage(ref m_OutboundMessage, networkDelivery, clientId); | ||
| m_CachedNetworkManager.MessageManager.SendMessage(ref m_OutboundMessage, networkDelivery, clientId); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // Clients (owner authoritative) send messages to the server-host | ||
| NetworkManager.MessageManager.SendMessage(ref m_OutboundMessage, networkDelivery, NetworkManager.ServerClientId); | ||
| m_CachedNetworkManager.MessageManager.SendMessage(ref m_OutboundMessage, networkDelivery, NetworkManager.ServerClientId); | ||
| } | ||
| m_LocalAuthoritativeNetworkState.LastSerializedSize = m_OutboundMessage.BytesWritten; | ||
| } | ||
|
|
@@ -4802,7 +4803,7 @@ public NetworkTransformTickRegistration(NetworkManager networkManager) | |
| internal void RegisterForTickSynchronization() | ||
| { | ||
| s_TickSynchPosition++; | ||
| m_NextTickSync = NetworkManager.ServerTime.Tick + (s_TickSynchPosition % (int)NetworkManager.NetworkConfig.TickRate); | ||
| m_NextTickSync = m_CachedNetworkManager.ServerTime.Tick + (s_TickSynchPosition % (int)NetworkManager.NetworkConfig.TickRate); | ||
| } | ||
|
|
||
| private static void RegisterNetworkManagerForTickUpdate(NetworkManager networkManager) | ||
|
|
@@ -4821,14 +4822,14 @@ private static void RegisterNetworkManagerForTickUpdate(NetworkManager networkMa | |
| /// <param name="networkTransform"></param> | ||
| private static void RegisterForTickUpdate(NetworkTransform networkTransform) | ||
| { | ||
|
|
||
| if (!networkTransform.NetworkManager.DistributedAuthorityMode && !s_NetworkTickRegistration.ContainsKey(networkTransform.NetworkManager)) | ||
| var networkManager = networkTransform.NetworkManager; | ||
| if (!networkManager.DistributedAuthorityMode && !s_NetworkTickRegistration.ContainsKey(networkManager)) | ||
| { | ||
| s_NetworkTickRegistration.Add(networkTransform.NetworkManager, new NetworkTransformTickRegistration(networkTransform.NetworkManager)); | ||
| s_NetworkTickRegistration.Add(networkManager, new NetworkTransformTickRegistration(networkManager)); | ||
| } | ||
|
|
||
| networkTransform.RegisterForTickSynchronization(); | ||
| s_NetworkTickRegistration[networkTransform.NetworkManager].NetworkTransforms.Add(networkTransform); | ||
| s_NetworkTickRegistration[networkManager].NetworkTransforms.Add(networkTransform); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -4838,16 +4839,17 @@ private static void RegisterForTickUpdate(NetworkTransform networkTransform) | |
| /// <param name="networkTransform"></param> | ||
| private static void DeregisterForTickUpdate(NetworkTransform networkTransform) | ||
| { | ||
| if (networkTransform.NetworkManager == null) | ||
| var networkManager = networkTransform.NetworkManager; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here: |
||
| if (!networkManager) | ||
| { | ||
| return; | ||
| } | ||
| if (s_NetworkTickRegistration.ContainsKey(networkTransform.NetworkManager)) | ||
| if (s_NetworkTickRegistration.ContainsKey(networkManager)) | ||
| { | ||
| s_NetworkTickRegistration[networkTransform.NetworkManager].NetworkTransforms.Remove(networkTransform); | ||
| if (!networkTransform.NetworkManager.DistributedAuthorityMode && s_NetworkTickRegistration[networkTransform.NetworkManager].NetworkTransforms.Count == 0) | ||
| s_NetworkTickRegistration[networkManager].NetworkTransforms.Remove(networkTransform); | ||
| if (!networkManager.DistributedAuthorityMode && s_NetworkTickRegistration[networkManager].NetworkTransforms.Count == 0) | ||
| { | ||
| var registrationEntry = s_NetworkTickRegistration[networkTransform.NetworkManager]; | ||
| var registrationEntry = s_NetworkTickRegistration[networkManager]; | ||
| registrationEntry.Remove(); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are in static context, does it mean we are using NetworkManager.Singleton? If that's so, shouldn't we use that one instead of networkTransform.NetworkManager?