Skip to content

Commit 2b3893c

Browse files
fix: clamp spawntimeout to recommended range (#3174)
* update Clamping spawntimeout * update improving parenting failed message when either the child or parent NetworkObject is not spawnd. * update Update the local SceneEventData.SceneEventType on the authority side for SceneLoadComplete. * style removing whitespaces
1 parent c69aa97 commit 2b3893c

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

com.unity.netcode.gameobjects/Runtime/Configuration/NetworkConfig.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ namespace Unity.Netcode
1313
[Serializable]
1414
public class NetworkConfig
1515
{
16+
// Clamp spawn time outs to prevent dropping messages during scene events
17+
// Note: The legacy versions of NGO defaulted to 1s which was too low. As
18+
// well, the SpawnTimeOut is now being clamped to within this recommended
19+
// range both via UI and when NetworkManager is validated.
20+
internal const float MinSpawnTimeout = 10.0f;
21+
// Clamp spawn time outs to no more than 1 hour (really that is a bit high)
22+
internal const float MaxSpawnTimeout = 3600.0f;
23+
1624
/// <summary>
1725
/// The protocol version. Different versions doesn't talk to each other.
1826
/// </summary>
@@ -132,6 +140,8 @@ public class NetworkConfig
132140
/// The amount of time a message will be held (deferred) if the destination NetworkObject needed to process the message doesn't exist yet. If the NetworkObject is not spawned within this time period, all deferred messages for that NetworkObject will be dropped.
133141
/// </summary>
134142
[Tooltip("The amount of time a message will be held (deferred) if the destination NetworkObject needed to process the message doesn't exist yet. If the NetworkObject is not spawned within this time period, all deferred messages for that NetworkObject will be dropped.")]
143+
144+
[Range(MinSpawnTimeout, MaxSpawnTimeout)]
135145
public float SpawnTimeout = 10f;
136146

137147
/// <summary>
@@ -176,6 +186,21 @@ public class NetworkConfig
176186
[Tooltip("Enable (default) if you want to profile network messages with development builds and defaults to being disabled in release builds. When disabled, network messaging profiling will be disabled in development builds.")]
177187
public bool NetworkProfilingMetrics = true;
178188

189+
/// <summary>
190+
/// Invoked by <see cref="NetworkManager"/> when it is validated.
191+
/// </summary>
192+
/// <remarks>
193+
/// Used to check for potential legacy values that have already been serialized and/or
194+
/// runtime modifications to a property outside of the recommended range.
195+
/// For each property checked below, provide a brief description of the reason.
196+
/// </remarks>
197+
internal void OnValidate()
198+
{
199+
// Legacy NGO versions defaulted this value to 1 second that has since been determiend
200+
// any range less than 10 seconds can lead to dropped messages during scene events.
201+
SpawnTimeout = Mathf.Clamp(SpawnTimeout, MinSpawnTimeout, MaxSpawnTimeout);
202+
}
203+
179204
/// <summary>
180205
/// Returns a base64 encoded version of the configuration
181206
/// </summary>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,9 @@ internal void OnValidate()
955955
return; // May occur when the component is added
956956
}
957957

958+
// Do a validation pass on NetworkConfig properties
959+
NetworkConfig.OnValidate();
960+
958961
if (GetComponentInChildren<NetworkObject>() != null)
959962
{
960963
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,12 +2007,14 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
20072007

20082008
internal bool InternalTrySetParent(NetworkObject parent, bool worldPositionStays = true)
20092009
{
2010-
if (parent != null && (IsSpawned ^ parent.IsSpawned))
2010+
if (parent != null && (IsSpawned ^ parent.IsSpawned) && NetworkManager != null && !NetworkManager.ShutdownInProgress)
20112011
{
2012-
if (NetworkManager != null && !NetworkManager.ShutdownInProgress)
2012+
if (NetworkManager.LogLevel <= LogLevel.Developer)
20132013
{
2014-
return false;
2014+
var nameOfNotSpawnedObject = IsSpawned ? $" the parent ({parent.name})" : $"the child ({name})";
2015+
NetworkLog.LogWarning($"Parenting failed because {nameOfNotSpawnedObject} is not spawned!");
20152016
}
2017+
return false;
20162018
}
20172019

20182020
m_CachedWorldPositionStays = worldPositionStays;

com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1903,10 +1903,12 @@ private void OnSessionOwnerLoadedScene(uint sceneEventId, Scene scene)
19031903
SendSceneEventData(sceneEventData.SceneEventId, NetworkManager.ConnectedClientsIds.Where(c => c != sessionOwner).ToArray());
19041904

19051905
m_IsSceneEventActive = false;
1906+
1907+
sceneEventData.SceneEventType = SceneEventType.LoadComplete;
19061908
//First, notify local server that the scene was loaded
19071909
OnSceneEvent?.Invoke(new SceneEvent()
19081910
{
1909-
SceneEventType = SceneEventType.LoadComplete,
1911+
SceneEventType = sceneEventData.SceneEventType,
19101912
LoadSceneMode = sceneEventData.LoadSceneMode,
19111913
SceneName = SceneNameFromHash(sceneEventData.SceneHash),
19121914
ClientId = NetworkManager.LocalClientId,

0 commit comments

Comments
 (0)