Skip to content

Commit a968834

Browse files
Jesse Olmermattwalsh-unity
andauthored
fix: Hide spammy & debug draw NetworkTransform behind define [MTT-1262] (#1286)
* chore: Hide spammy & debug draw NetworkTransform behind define - Moves debug draw & local-write warnings in NT behind a debug define - Fixes warning message format - Fixes teleport exception message Future changes not addressed here: - Debug draw should be via gizmos, not the current implementation - Spammy log messages shouldn't be so spammy, and shouldn't be so expensive to detect * fix: Test log verification workaround. We have a test which verifies log output. Updated the test to match same parameters (that is, define guard and developer log level guard). These tests need to be properly refactored post-1.0 * test: guard regex using in test file with NGO_TRANSFORM_DEBUG Co-authored-by: Matt Walsh <[email protected]>
1 parent b36d31a commit a968834

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,6 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
298298
private int m_LastSentTick;
299299
private NetworkTransformState m_LastSentState;
300300

301-
private const string k_NoAuthorityMessage = "A local change to {dirtyField} without authority detected, reverting back to latest interpolated network state!";
302-
303301

304302
/// <summary>
305303
/// Tries updating the server authoritative transform, only if allowed.
@@ -852,25 +850,28 @@ protected virtual void Update()
852850

853851
if (!CanCommitToTransform)
854852
{
853+
#if NGO_TRANSFORM_DEBUG
855854
if (m_CachedNetworkManager.LogLevel == LogLevel.Developer)
856855
{
856+
// TODO: This should be a component gizmo - not some debug draw based on log level
857857
var interpolatedPosition = new Vector3(m_PositionXInterpolator.GetInterpolatedValue(), m_PositionYInterpolator.GetInterpolatedValue(), m_PositionZInterpolator.GetInterpolatedValue());
858858
Debug.DrawLine(interpolatedPosition, interpolatedPosition + Vector3.up, Color.magenta, k_DebugDrawLineTime, false);
859-
}
860859

861-
// try to update previously consumed NetworkState
862-
// if we have any changes, that means made some updates locally
863-
// we apply the latest ReplNetworkState again to revert our changes
864-
var oldStateDirtyInfo = ApplyTransformToNetworkStateWithInfo(ref m_PrevNetworkState, 0, m_Transform);
865-
866-
// there is a bug in this code, as we the message is dumped out under odd circumstances
867-
if (oldStateDirtyInfo.isPositionDirty || oldStateDirtyInfo.isScaleDirty || (oldStateDirtyInfo.isRotationDirty && SyncRotAngleX && SyncRotAngleY && SyncRotAngleZ))
868-
{
869-
// ignoring rotation dirty since quaternions will mess with euler angles, making this impossible to determine if the change to a single axis comes
870-
// from an unauthorized transform change or euler to quaternion conversion artifacts.
871-
var dirtyField = oldStateDirtyInfo.isPositionDirty ? "position" : oldStateDirtyInfo.isRotationDirty ? "rotation" : "scale";
872-
Debug.LogWarning(dirtyField + k_NoAuthorityMessage, this);
860+
// try to update previously consumed NetworkState
861+
// if we have any changes, that means made some updates locally
862+
// we apply the latest ReplNetworkState again to revert our changes
863+
var oldStateDirtyInfo = ApplyTransformToNetworkStateWithInfo(ref m_PrevNetworkState, 0, m_Transform);
864+
865+
// there are several bugs in this code, as we the message is dumped out under odd circumstances
866+
if (oldStateDirtyInfo.isPositionDirty || oldStateDirtyInfo.isScaleDirty || (oldStateDirtyInfo.isRotationDirty && SyncRotAngleX && SyncRotAngleY && SyncRotAngleZ))
867+
{
868+
// ignoring rotation dirty since quaternions will mess with euler angles, making this impossible to determine if the change to a single axis comes
869+
// from an unauthorized transform change or euler to quaternion conversion artifacts.
870+
var dirtyField = oldStateDirtyInfo.isPositionDirty ? "position" : oldStateDirtyInfo.isRotationDirty ? "rotation" : "scale";
871+
Debug.LogWarning($"A local change to {dirtyField} without authority detected, reverting back to latest interpolated network state!", this);
872+
}
873873
}
874+
#endif
874875

875876
// Apply updated interpolated value
876877
ApplyInterpolatedNetworkStateToTransform(m_ReplicatedNetworkState.Value, m_Transform);
@@ -887,7 +888,7 @@ public void Teleport(Vector3 newPosition, Quaternion newRotation, Vector3 newSca
887888
{
888889
if (!CanCommitToTransform)
889890
{
890-
throw new Exception("Teleport not allowed, " + k_NoAuthorityMessage);
891+
throw new Exception("Teleport not allowed");
891892
}
892893

893894
var newRotationEuler = newRotation.eulerAngles;

com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections;
3+
#if NGO_TRANSFORM_DEBUG
34
using System.Text.RegularExpressions;
5+
#endif
46
using Unity.Netcode.Components;
57
using NUnit.Framework;
68
// using Unity.Netcode.Samples;
@@ -45,6 +47,13 @@ public override IEnumerator Setup()
4547
}
4648
});
4749

50+
#if NGO_TRANSFORM_DEBUG
51+
// Log assert for writing without authority is a developer log...
52+
// TODO: This is why monolithic test base classes and test helpers are an anti-pattern - this is part of an individual test case setup but is separated from the code verifying it!
53+
m_ServerNetworkManager.LogLevel = LogLevel.Developer;
54+
m_ClientNetworkManagers[0].LogLevel = LogLevel.Developer;
55+
#endif
56+
4857
// This is the *SERVER VERSION* of the *CLIENT PLAYER*
4958
var serverClientPlayerResult = new MultiInstanceHelpers.CoroutineResultWrapper<NetworkObject>();
5059
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.GetNetworkObjectByRepresentation(x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId, m_ServerNetworkManager, serverClientPlayerResult));
@@ -167,7 +176,10 @@ public IEnumerator TestCantChangeTransformFromOtherSideAuthority([Values] bool t
167176
yield return null; // one frame
168177

169178
Assert.AreEqual(Vector3.zero, otherSideNetworkTransform.transform.position, "got authority error, but other side still moved!");
179+
#if NGO_TRANSFORM_DEBUG
180+
// TODO: This should be a separate test - verify 1 behavior per test
170181
LogAssert.Expect(LogType.Warning, new Regex(".*without authority detected.*"));
182+
#endif
171183
}
172184

173185
/*

0 commit comments

Comments
 (0)