Skip to content

fix: clamp spawntimeout to recommended range #3174

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

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ namespace Unity.Netcode
[Serializable]
public class NetworkConfig
{
// Clamp spawn time outs to prevent dropping messages during scene events
// Note: The legacy versions of NGO defaulted to 1s which was too low. As
// well, the SpawnTimeOut is now being clamped to within this recommended
// range both via UI and when NetworkManager is validated.
internal const float MinSpawnTimeout = 10.0f;
// Clamp spawn time outs to no more than 1 hour (really that is a bit high)
internal const float MaxSpawnTimeout = 3600.0f;

/// <summary>
/// The protocol version. Different versions doesn't talk to each other.
/// </summary>
Expand Down Expand Up @@ -132,6 +140,8 @@ public class NetworkConfig
/// 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.
/// </summary>
[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.")]

[Range(MinSpawnTimeout, MaxSpawnTimeout)]
public float SpawnTimeout = 10f;

/// <summary>
Expand Down Expand Up @@ -176,6 +186,21 @@ public class NetworkConfig
[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.")]
public bool NetworkProfilingMetrics = true;

/// <summary>
/// Invoked by <see cref="NetworkManager"/> when it is validated.
/// </summary>
/// <remarks>
/// Used to check for potential legacy values that have already been serialized and/or
/// runtime modifications to a property outside of the recommended range.
/// For each property checked below, provide a brief description of the reason.
/// </remarks>
internal void OnValidate()
{
// Legacy NGO versions defaulted this value to 1 second that has since been determiend
// any range less than 10 seconds can lead to dropped messages during scene events.
SpawnTimeout = Mathf.Clamp(SpawnTimeout, MinSpawnTimeout, MaxSpawnTimeout);
}

/// <summary>
/// Returns a base64 encoded version of the configuration
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,9 @@ internal void OnValidate()
return; // May occur when the component is added
}

// Do a validation pass on NetworkConfig properties
NetworkConfig.OnValidate();

if (GetComponentInChildren<NetworkObject>() != null)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
Expand Down
8 changes: 5 additions & 3 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2007,12 +2007,14 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)

internal bool InternalTrySetParent(NetworkObject parent, bool worldPositionStays = true)
{
if (parent != null && (IsSpawned ^ parent.IsSpawned))
if (parent != null && (IsSpawned ^ parent.IsSpawned) && NetworkManager != null && !NetworkManager.ShutdownInProgress)
{
if (NetworkManager != null && !NetworkManager.ShutdownInProgress)
if (NetworkManager.LogLevel <= LogLevel.Developer)
{
return false;
var nameOfNotSpawnedObject = IsSpawned ? $" the parent ({parent.name})" : $"the child ({name})";
NetworkLog.LogWarning($"Parenting failed because {nameOfNotSpawnedObject} is not spawned!");
}
return false;
}

m_CachedWorldPositionStays = worldPositionStays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1903,10 +1903,12 @@ private void OnSessionOwnerLoadedScene(uint sceneEventId, Scene scene)
SendSceneEventData(sceneEventData.SceneEventId, NetworkManager.ConnectedClientsIds.Where(c => c != sessionOwner).ToArray());

m_IsSceneEventActive = false;

sceneEventData.SceneEventType = SceneEventType.LoadComplete;
//First, notify local server that the scene was loaded
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = SceneEventType.LoadComplete,
SceneEventType = sceneEventData.SceneEventType,
LoadSceneMode = sceneEventData.LoadSceneMode,
SceneName = SceneNameFromHash(sceneEventData.SceneHash),
ClientId = NetworkManager.LocalClientId,
Expand Down
Loading