Skip to content

Commit 6fa7a43

Browse files
committed
Fix all pvp exceptions in NetworkTransform and NetworkBehaviour
1 parent 06e58e7 commit 6fa7a43

File tree

3 files changed

+60
-65
lines changed

3 files changed

+60
-65
lines changed

com.unity.netcode.gameobjects/Components/NetworkTransform.cs

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class NetworkTransform : NetworkBehaviour
6666
/// - The teleporting state update.
6767
/// - When using half float precision and the `NetworkDeltaPosition` delta exceeds the maximum delta forcing the axis in
6868
/// question to be collapsed into the core base position, this state update will be sent as reliable fragmented sequenced.
69-
///
69+
///
7070
/// In order to preserve a continual consistency of axial values when unreliable delta messaging is enabled (due to the
7171
/// possibility of dropping packets), NetworkTransform instances will send 1 axial frame synchronization update per
7272
/// second (only for the axis marked to synchronize are sent as reliable fragmented sequenced) as long as a delta state
@@ -104,7 +104,7 @@ public struct NetworkTransformState : INetworkSerializable
104104
private const int k_ReliableSequenced = 0x00080000;
105105
private const int k_UseUnreliableDeltas = 0x00100000;
106106
private const int k_UnreliableFrameSync = 0x00200000;
107-
private const int k_TrackStateId = 0x10000000; // (Internal Debugging) When set each state update will contain a state identifier
107+
private const int k_TrackStateId = 0x10000000; // (Internal Debugging) When set each state update will contain a state identifier
108108

109109
// Stores persistent and state relative flags
110110
private uint m_Bitset;
@@ -459,10 +459,11 @@ internal set
459459
}
460460

461461
/// <summary>
462-
/// Returns whether this state update was a frame synchronization when
463-
/// UseUnreliableDeltas is enabled. When set, the entire transform will
462+
/// Returns whether this state update was a frame synchronization when
463+
/// UseUnreliableDeltas is enabled. When set, the entire transform will
464464
/// be or has been synchronized.
465465
/// </summary>
466+
/// <returns><see cref="true"/> if this state update was a frame synchronization; otherwise, <see cref="false"/>.</returns>
466467
public bool IsUnreliableFrameSync()
467468
{
468469
return UnreliableFrameSync;
@@ -475,6 +476,7 @@ public bool IsUnreliableFrameSync()
475476
/// <remarks>
476477
/// Unreliable delivery will only be used if <see cref="UseUnreliableDeltas"/> is set.
477478
/// </remarks>
479+
/// <returns><see cref="true"/> if this state was sent with reliable delivery; otherwise, <see cref="false"/>.</returns>
478480
public bool IsReliableStateUpdate()
479481
{
480482
return ReliableSequenced;
@@ -586,7 +588,7 @@ public Quaternion GetRotation()
586588
/// <remarks>
587589
/// When there is no change in an updated state's position then there are no values to return.
588590
/// Checking for <see cref="HasPositionChange"/> is one way to detect this.
589-
/// When used with half precision it returns the half precision delta position state update
591+
/// When used with half precision it returns the half precision delta position state update
590592
/// which will not be the full position.
591593
/// To get a NettworkTransform's full position, use <see cref="GetSpaceRelativePosition(bool)"/> and
592594
/// pass true as the parameter.
@@ -661,6 +663,8 @@ public int GetNetworkTick()
661663
/// <summary>
662664
/// Serializes this <see cref="NetworkTransformState"/>
663665
/// </summary>
666+
/// <typeparam name="T">The type of the serializer.</typeparam>
667+
/// <param name="serializer">The serializer used for reading or writing the state.</param>
664668
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
665669
{
666670
// Used to calculate the LastSerializedSize value
@@ -897,7 +901,7 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
897901
if (HasScaleChange)
898902
{
899903
// If we are teleporting (which includes synchronizing) and the associated NetworkObject has a parent
900-
// then we want to serialize the LossyScale since NetworkObject spawn order is not guaranteed
904+
// then we want to serialize the LossyScale since NetworkObject spawn order is not guaranteed
901905
if (IsTeleportingNextFrame && IsParented)
902906
{
903907
serializer.SerializeValue(ref LossyScale);
@@ -1419,9 +1423,8 @@ private bool ShouldSynchronizeHalfFloat(ulong targetClientId)
14191423
/// <remarks>
14201424
/// If a derived class overrides this, then make sure to invoke this base method!
14211425
/// </remarks>
1422-
/// <typeparam name="T"></typeparam>
1423-
/// <param name="serializer"></param>
1424-
/// <param name="targetClientId">the clientId being synchronized (both reading and writing)</param>
1426+
/// <typeparam name="T">The type of the serializer.</typeparam>
1427+
/// <param name="serializer">The serializer used for reading or writing the state.</param>
14251428
protected override void OnSynchronize<T>(ref BufferSerializer<T> serializer)
14261429
{
14271430
m_CachedNetworkManager = NetworkManager;
@@ -1575,11 +1578,11 @@ private void TryCommitTransform(ref Transform transformToCommit, bool synchroniz
15751578
Debug.LogException(ex);
15761579
}
15771580

1578-
// The below is part of assuring we only send a frame synch, when sending unreliable deltas, if
1581+
// The below is part of assuring we only send a frame synch, when sending unreliable deltas, if
15791582
// we have already sent at least one unreliable delta state update. At this point in the callstack,
15801583
// a delta state update has just been sent in the above UpdateTransformState() call and as long as
15811584
// we didn't send a frame synch and we are not synchronizing then we know at least one unreliable
1582-
// delta has been sent. Under this scenario, we should start checking for this instance's alloted
1585+
// delta has been sent. Under this scenario, we should start checking for this instance's alloted
15831586
// frame synch "tick slot". Once we send a frame synch, if no other deltas occur after that
15841587
// (i.e. the object is at rest) then we will stop sending frame synch's until the object begins
15851588
// moving, rotating, or scaling again.
@@ -1862,7 +1865,7 @@ private bool ApplyTransformToNetworkStateWithInfo(ref NetworkTransformState netw
18621865

18631866
networkState.NetworkDeltaPosition = m_HalfPositionState;
18641867

1865-
// If ownership offset is greater or we are doing an axial synchronization then synchronize the base position
1868+
// If ownership offset is greater or we are doing an axial synchronization then synchronize the base position
18661869
if ((m_HalfFloatTargetTickOwnership > m_CachedNetworkManager.ServerTime.Tick || isAxisSync) && !networkState.IsTeleportingNextFrame)
18671870
{
18681871
networkState.SynchronizeBaseHalfFloat = true;
@@ -2056,6 +2059,10 @@ private bool ApplyTransformToNetworkStateWithInfo(ref NetworkTransformState netw
20562059
return isDirty;
20572060
}
20582061

2062+
/// <summary>
2063+
/// Invoked whenever the transform has been updated.
2064+
/// This method can be overridden to handle any custom logic that needs to occur after the transform has been updated.
2065+
/// </summary>
20592066
protected virtual void OnTransformUpdated()
20602067
{
20612068

@@ -2602,6 +2609,10 @@ protected virtual void OnNetworkTransformStateUpdated(ref NetworkTransformState
26022609

26032610
}
26042611

2612+
/// <summary>
2613+
/// Invoked just before the transform state is updated.
2614+
/// This method can be overridden to handle any custom logic that needs to occur before the transform state is updated.
2615+
/// </summary>
26052616
protected virtual void OnBeforeUpdateTransformState()
26062617
{
26072618

@@ -2816,6 +2827,12 @@ public override void OnGainedOwnership()
28162827
base.OnGainedOwnership();
28172828
}
28182829

2830+
/// <summary>
2831+
/// Invoked when the ownership of the <see cref="NetworkObject"/> changes.
2832+
/// This method handles reinitialization when the local client gains or loses ownership of the <see cref="NetworkObject"/>.
2833+
/// </summary>
2834+
/// <param name="previous">The client ID of the previous owner.</param>
2835+
/// <param name="current">The client ID of the new owner.</param>
28192836
protected override void OnOwnershipChanged(ulong previous, ulong current)
28202837
{
28212838
// If we were the previous owner or the newly assigned owner then reinitialize
@@ -2842,7 +2859,7 @@ protected virtual void OnInitialize(ref NetworkTransformState replicatedState)
28422859
/// This method is only invoked by the owner
28432860
/// Use: OnInitialize(ref NetworkTransformState replicatedState) to be notified on all instances
28442861
/// </summary>
2845-
/// <param name="replicatedState"></param>
2862+
/// <param name="replicatedState">The current <see cref="NetworkVariable{NetworkTransformState}"/> after initializing.</param>
28462863
protected virtual void OnInitialize(ref NetworkVariable<NetworkTransformState> replicatedState)
28472864
{
28482865

@@ -2851,9 +2868,9 @@ protected virtual void OnInitialize(ref NetworkVariable<NetworkTransformState> r
28512868
private int m_HalfFloatTargetTickOwnership;
28522869

28532870
/// <summary>
2854-
/// The internal initialzation method to allow for internal API adjustments
2871+
/// The internal initialization method to allow for internal API adjustments
28552872
/// </summary>
2856-
/// <param name="isOwnershipChange"></param>
2873+
/// <param name="isOwnershipChange">Indicates whether the initialization is due to an ownership change.</param>
28572874
private void InternalInitialization(bool isOwnershipChange = false)
28582875
{
28592876
if (!IsSpawned)
@@ -2956,11 +2973,11 @@ public override void OnNetworkObjectParentChanged(NetworkObject parentNetworkObj
29562973
/// The parameters are broken up into pos / rot / scale on purpose so that the caller can perturb
29572974
/// just the desired one(s)
29582975
/// </summary>
2959-
/// <param name="posIn"></param> new position to move to. Can be null
2960-
/// <param name="rotIn"></param> new rotation to rotate to. Can be null
2976+
/// <param name="posIn">new position to move to. Can be null.</param>
2977+
/// <param name="rotIn">new rotation to rotate to. Can be null.</param>
29612978
/// <param name="scaleIn">new scale to scale to. Can be null</param>
29622979
/// <param name="teleportDisabled">When true (the default) the <see cref="NetworkObject"/> will not be teleported and, if enabled, will interpolate. When false the <see cref="NetworkObject"/> will teleport/apply the parameters provided immediately.</param>
2963-
/// <exception cref="Exception"></exception>
2980+
/// <exception cref="Exception">Thrown when the function is called on non-spawned object or, when it's called without proper authority</exception>
29642981
public void SetState(Vector3? posIn = null, Quaternion? rotIn = null, Vector3? scaleIn = null, bool teleportDisabled = true)
29652982
{
29662983
if (!IsSpawned)
@@ -3023,7 +3040,7 @@ private void SetStateInternal(Vector3 pos, Quaternion rot, Vector3 scale, bool s
30233040

30243041
var transformToCommit = transform;
30253042

3026-
// Explicit set states are cumulative during a fractional tick period of time (i.e. each SetState invocation will
3043+
// Explicit set states are cumulative during a fractional tick period of time (i.e. each SetState invocation will
30273044
// update the axial deltas to whatever changes are applied). As such, we need to preserve the dirty and explicit
30283045
// state flags.
30293046
var stateWasDirty = m_LocalAuthoritativeNetworkState.IsDirty;
@@ -3111,7 +3128,9 @@ private void UpdateInterpolation()
31113128
}
31123129
}
31133130

3114-
/// <inheritdoc/>
3131+
/// <summary>
3132+
/// This method is called once per frame.
3133+
/// </summary>
31153134
/// <remarks>
31163135
/// If you override this method, be sure that:
31173136
/// - Non-authority always invokes this base class method.
@@ -3134,10 +3153,10 @@ protected virtual void Update()
31343153
/// <summary>
31353154
/// Teleport the transform to the given values without interpolating
31363155
/// </summary>
3137-
/// <param name="newPosition"></param> new position to move to.
3138-
/// <param name="newRotation"></param> new rotation to rotate to.
3156+
/// <param name="newPosition">new position to move to.</param>
3157+
/// <param name="newRotation">new rotation to rotate to.</param>
31393158
/// <param name="newScale">new scale to scale to.</param>
3140-
/// <exception cref="Exception"></exception>
3159+
/// <exception cref="Exception">Thrown if teleporting is attempted on a non-authoritative side.</exception>
31413160
public void Teleport(Vector3 newPosition, Quaternion newRotation, Vector3 newScale)
31423161
{
31433162
if (!CanCommitToTransform)

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -492,15 +492,15 @@ internal bool IsBehaviourEditable()
492492
m_NetworkObject.NetworkManager.IsServer;
493493
}
494494

495-
/// TODO: this needs an overhaul. It's expensive, it's ja little naive in how it looks for networkObject in
496-
/// its parent and worst, it creates a puzzle if you are a NetworkBehaviour wanting to see if you're live or not
497-
/// (e.g. editor code). All you want to do is find out if NetworkManager is null, but to do that you
498-
/// need NetworkObject, but if you try and grab NetworkObject and NetworkManager isn't up you'll get
499-
/// the warning below. This is why IsBehaviourEditable had to be created. Matt was going to re-do
500-
/// how NetworkObject works but it was close to the release and too risky to change
501495
/// <summary>
502496
/// Gets the NetworkObject that owns this NetworkBehaviour instance
503497
/// </summary>
498+
// TODO: this needs an overhaul. It's expensive, it's ja little naive in how it looks for networkObject in
499+
// its parent and worst, it creates a puzzle if you are a NetworkBehaviour wanting to see if you're live or not
500+
// (e.g. editor code). All you want to do is find out if NetworkManager is null, but to do that you
501+
// need NetworkObject, but if you try and grab NetworkObject and NetworkManager isn't up you'll get
502+
// the warning below. This is why IsBehaviourEditable had to be created. Matt was going to re-do
503+
// how NetworkObject works but it was close to the release and too risky to change
504504
public NetworkObject NetworkObject
505505
{
506506
get
@@ -620,11 +620,11 @@ internal void UpdateNetworkProperties()
620620
/// A reference to <see cref="NetworkManager"/> is passed in as a parameter to determine the context of execution (IsServer/IsClient)
621621
/// </summary>
622622
/// <remarks>
623-
/// <param name="networkManager">a ref to the <see cref="NetworkManager"/> since this is not yet set on the <see cref="NetworkBehaviour"/></param>
624623
/// The <see cref="NetworkBehaviour"/> will not have anything assigned to it at this point in time.
625624
/// Settings like ownership, NetworkBehaviourId, NetworkManager, and most other spawn related properties will not be set.
626625
/// This can be used to handle things like initializing/instantiating a NetworkVariable or the like.
627626
/// </remarks>
627+
/// <param name="networkManager">a ref to the <see cref="NetworkManager"/> since this is not yet set on the <see cref="NetworkBehaviour"/></param>
628628
protected virtual void OnNetworkPreSpawn(ref NetworkManager networkManager) { }
629629

630630
/// <summary>
@@ -654,11 +654,11 @@ protected virtual void OnNetworkPostSpawn() { }
654654
protected virtual void OnNetworkSessionSynchronized() { }
655655

656656
/// <summary>
657-
/// [Client & Server Side]
657+
/// [Client and Server Side]
658658
/// When a scene is loaded an in-scene placed NetworkObjects are all spawned, this method is invoked on all of the newly spawned in-scene placed NetworkObjects.
659659
/// </summary>
660660
/// <remarks>
661-
/// This can be used to handle post scene loaded actions for in-scene placed NetworkObjcts where you might need to access a different NetworkObject and/or NetworkBehaviour not local to the current NetworkObject context.
661+
/// This can be used to handle post scene loaded actions for in-scene placed NetworkObjects where you might need to access a different NetworkObject and/or NetworkBehaviour not local to the current NetworkObject context.
662662
/// </remarks>
663663
protected virtual void OnInSceneObjectsSpawned() { }
664664

@@ -772,7 +772,7 @@ public virtual void OnGainedOwnership() { }
772772
internal void InternalOnGainedOwnership()
773773
{
774774
UpdateNetworkProperties();
775-
// New owners need to assure any NetworkVariables they have write permissions
775+
// New owners need to assure any NetworkVariables they have write permissions
776776
// to are updated so the previous and original values are aligned with the
777777
// current value (primarily for collections).
778778
if (OwnerClientId == NetworkManager.LocalClientId)
@@ -1220,10 +1220,10 @@ internal void SetNetworkVariableData(FastBufferReader reader, ulong clientId)
12201220
}
12211221

12221222
/// <summary>
1223-
/// Gets the local instance of a object with a given NetworkId
1223+
/// Gets the local instance of a <see cref="NetworkObject"/> with a given NetworkId.
12241224
/// </summary>
1225-
/// <param name="networkId"></param>
1226-
/// <returns></returns>
1225+
/// <param name="networkId">The NetworkId of the <see cref="NetworkObject"/> to retrieve.</param>
1226+
/// <returns>Returns the <see cref="NetworkObject"/> with the specified NetworkId, or null if not found.</returns>
12271227
protected NetworkObject GetNetworkObject(ulong networkId)
12281228
{
12291229
return NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(networkId, out NetworkObject networkObject) ? networkObject : null;
@@ -1246,12 +1246,16 @@ protected NetworkObject GetNetworkObject(ulong networkId)
12461246
/// Either BufferSerializerReader or BufferSerializerWriter, depending whether the serializer
12471247
/// is in read mode or write mode.
12481248
/// </typeparam>
1249-
/// <param name="targetClientId">the relative client identifier being synchronized</param>
12501249
protected virtual void OnSynchronize<T>(ref BufferSerializer<T> serializer) where T : IReaderWriter
12511250
{
12521251

12531252
}
12541253

1254+
/// <summary>
1255+
/// Invoked when the network anticipates a change in the state of the object.
1256+
/// This method can be used to handle any logic that needs to occur when the network predicts a state change.
1257+
/// </summary>
1258+
/// <param name="lastRoundTripTime">The last round trip time in seconds.</param>
12551259
public virtual void OnReanticipate(double lastRoundTripTime)
12561260
{
12571261

0 commit comments

Comments
 (0)