Skip to content

chore: EnableNetworkVariable removal #1179

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 4 commits into from
Sep 14, 2021
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 @@ -26,7 +26,6 @@ public class NetworkManagerEditor : UnityEditor.Editor
private SerializedProperty m_MaxObjectUpdatesPerTickProperty;
private SerializedProperty m_ClientConnectionBufferTimeoutProperty;
private SerializedProperty m_ConnectionApprovalProperty;
private SerializedProperty m_EnableNetworkVariableProperty;
private SerializedProperty m_EnsureNetworkVariableLengthSafetyProperty;
private SerializedProperty m_ForceSamePrefabsProperty;
private SerializedProperty m_EnableSceneManagementProperty;
Expand Down Expand Up @@ -94,7 +93,6 @@ private void Initialize()
m_TickRateProperty = m_NetworkConfigProperty.FindPropertyRelative("TickRate");
m_ClientConnectionBufferTimeoutProperty = m_NetworkConfigProperty.FindPropertyRelative("ClientConnectionBufferTimeout");
m_ConnectionApprovalProperty = m_NetworkConfigProperty.FindPropertyRelative("ConnectionApproval");
m_EnableNetworkVariableProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableNetworkVariable");
m_EnsureNetworkVariableLengthSafetyProperty = m_NetworkConfigProperty.FindPropertyRelative("EnsureNetworkVariableLengthSafety");
m_ForceSamePrefabsProperty = m_NetworkConfigProperty.FindPropertyRelative("ForceSamePrefabs");
m_EnableSceneManagementProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableSceneManagement");
Expand Down Expand Up @@ -122,7 +120,6 @@ private void CheckNullProperties()
m_TickRateProperty = m_NetworkConfigProperty.FindPropertyRelative("TickRate");
m_ClientConnectionBufferTimeoutProperty = m_NetworkConfigProperty.FindPropertyRelative("ClientConnectionBufferTimeout");
m_ConnectionApprovalProperty = m_NetworkConfigProperty.FindPropertyRelative("ConnectionApproval");
m_EnableNetworkVariableProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableNetworkVariable");
m_EnsureNetworkVariableLengthSafetyProperty = m_NetworkConfigProperty.FindPropertyRelative("EnsureNetworkVariableLengthSafety");
m_ForceSamePrefabsProperty = m_NetworkConfigProperty.FindPropertyRelative("ForceSamePrefabs");
m_EnableSceneManagementProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableSceneManagement");
Expand Down Expand Up @@ -260,12 +257,8 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(m_TickRateProperty);

EditorGUILayout.LabelField("Performance", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(m_EnableNetworkVariableProperty);

using (new EditorGUI.DisabledScope(!m_NetworkManager.NetworkConfig.EnableNetworkVariable))
{
EditorGUILayout.PropertyField(m_EnsureNetworkVariableLengthSafetyProperty);
}
EditorGUILayout.PropertyField(m_EnsureNetworkVariableLengthSafetyProperty);

EditorGUILayout.LabelField("Connection", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(m_ConnectionApprovalProperty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ public class NetworkConfig
[Tooltip("The amount of seconds between re-syncs of NetworkTime, if enabled")]
public int TimeResyncInterval = 30;

/// <summary>
/// Whether or not to enable the NetworkVariable system. This system runs in the Update loop and will degrade performance, but it can be a huge convenience.
/// Only turn it off if you have no need for the NetworkVariable system.
/// </summary>
[Tooltip("Whether or not to enable the NetworkVariable system")]
public bool EnableNetworkVariable = true;

/// <summary>
/// Whether or not to ensure that NetworkVariables can be read even if a client accidentally writes where its not allowed to. This costs some CPU and bandwidth.
/// </summary>
Expand Down Expand Up @@ -180,7 +173,6 @@ public string ToBase64()
writer.WriteBool(EnableSceneManagement);
writer.WriteBool(RecycleNetworkIds);
writer.WriteSinglePacked(NetworkIdRecycleDelay);
writer.WriteBool(EnableNetworkVariable);
writer.WriteBool(EnableNetworkLogs);
buffer.PadBuffer();

Expand Down Expand Up @@ -211,7 +203,6 @@ public void FromBase64(string base64)
config.EnableSceneManagement = reader.ReadBool();
config.RecycleNetworkIds = reader.ReadBool();
config.NetworkIdRecycleDelay = reader.ReadSinglePacked();
config.EnableNetworkVariable = reader.ReadBool();
config.EnableNetworkLogs = reader.ReadBool();
}

Expand Down Expand Up @@ -246,7 +237,6 @@ public ulong GetConfig(bool cache = true)
}
}
writer.WriteBool(ConnectionApproval);
writer.WriteBool(EnableNetworkVariable);
writer.WriteBool(ForceSamePrefabs);
writer.WriteBool(EnableSceneManagement);
writer.WriteBool(EnsureNetworkVariableLengthSafety);
Expand Down
12 changes: 3 additions & 9 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,11 +1040,8 @@ private void OnNetworkPreUpdate()
/// </summary>
private void OnNetworkManagerTick()
{
if (NetworkConfig.EnableNetworkVariable)
{
// Do NetworkVariable updates
BehaviourUpdater.NetworkBehaviourUpdate(this);
}
// Do NetworkVariable updates
BehaviourUpdater.NetworkBehaviourUpdate(this);

int timeSyncFrequencyTicks = (int)(k_TimeSyncFrequency * NetworkConfig.TickRate);
if (IsServer && NetworkTickSystem.ServerTime.Tick % timeSyncFrequencyTicks == 0)
Expand Down Expand Up @@ -1523,10 +1520,7 @@ internal void ApprovedPlayerSpawn(ulong clientId, uint playerPrefabHash)

nonNullContext.NetworkWriter.WriteBool(false); //No payload data

if (NetworkConfig.EnableNetworkVariable)
{
ConnectedClients[clientId].PlayerObject.WriteNetworkVariableData(nonNullContext.NetworkWriter.GetStream(), clientPair.Key);
}
ConnectedClients[clientId].PlayerObject.WriteNetworkVariableData(nonNullContext.NetworkWriter.GetStream(), clientPair.Key);
}
}
}
Expand Down
56 changes: 27 additions & 29 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -968,44 +968,42 @@ internal void SerializeSceneObject(NetworkWriter writer, ulong targetClientId)
WriteNetworkParenting(writer, isReparented, latestParent);
}

// Write whether we are including network variable data
writer.WriteBool(NetworkManager.NetworkConfig.EnableNetworkVariable);
// NetworkVariable section

//If we are including NetworkVariable data
if (NetworkManager.NetworkConfig.EnableNetworkVariable)
{
var buffer = writer.GetStream() as NetworkBuffer;
// todo: remove this WriteBool and the matching read
writer.WriteBool(true);

// Write placeholder size, NOT as a packed value, initially as zero (i.e. we do not know how much NetworkVariable data will be written yet)
writer.WriteUInt32(0);
var buffer = writer.GetStream() as NetworkBuffer;

// Mark our current position before we potentially write any NetworkVariable data
var positionBeforeNetworkVariableData = buffer.Position;
// Write placeholder size, NOT as a packed value, initially as zero (i.e. we do not know how much NetworkVariable data will be written yet)
writer.WriteUInt32(0);

// Write network variable data
WriteNetworkVariableData(buffer, targetClientId);
// Mark our current position before we potentially write any NetworkVariable data
var positionBeforeNetworkVariableData = buffer.Position;

// If our current buffer position is greater than our positionBeforeNetworkVariableData then we wrote NetworkVariable data
// Part 1: This will include the total NetworkVariable data size, if there was NetworkVariable data written, to the stream
// in order to be able to skip past this entry on the deserialization side in the event this NetworkObject fails to be
// constructed (See Part 2 below in the DeserializeSceneObject method)
if (buffer.Position > positionBeforeNetworkVariableData)
{
// Store our current stream buffer position
var endOfNetworkVariableData = buffer.Position;
// Write network variable data
WriteNetworkVariableData(buffer, targetClientId);

// Calculate the total NetworkVariable data size written
var networkVariableDataSize = endOfNetworkVariableData - positionBeforeNetworkVariableData;
// If our current buffer position is greater than our positionBeforeNetworkVariableData then we wrote NetworkVariable data
// Part 1: This will include the total NetworkVariable data size, if there was NetworkVariable data written, to the stream
// in order to be able to skip past this entry on the deserialization side in the event this NetworkObject fails to be
// constructed (See Part 2 below in the DeserializeSceneObject method)
if (buffer.Position > positionBeforeNetworkVariableData)
{
// Store our current stream buffer position
var endOfNetworkVariableData = buffer.Position;

// Move the stream position back to just before we wrote our size (we include the unpacked UInt32 data size placeholder)
buffer.Position = positionBeforeNetworkVariableData - sizeof(uint);
// Calculate the total NetworkVariable data size written
var networkVariableDataSize = endOfNetworkVariableData - positionBeforeNetworkVariableData;

// Now write the actual data size written into our unpacked UInt32 placeholder position
writer.WriteUInt32((uint)(networkVariableDataSize));
// Move the stream position back to just before we wrote our size (we include the unpacked UInt32 data size placeholder)
buffer.Position = positionBeforeNetworkVariableData - sizeof(uint);

// Finally, revert the buffer position back to the end of the network variable data written
buffer.Position = endOfNetworkVariableData;
}
// Now write the actual data size written into our unpacked UInt32 placeholder position
writer.WriteUInt32((uint)(networkVariableDataSize));

// Finally, revert the buffer position back to the end of the network variable data written
buffer.Position = endOfNetworkVariableData;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,6 @@ public void HandleTimeSync(ulong clientId, Stream stream)

public void HandleNetworkVariableDelta(ulong clientId, Stream stream)
{
if (!NetworkManager.NetworkConfig.EnableNetworkVariable)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogWarning($"Network variable delta received but {nameof(NetworkConfig.EnableNetworkVariable)} is false");
}

return;
}

using var reader = PooledNetworkReader.Get(stream);
ulong networkObjectId = reader.ReadUInt64Packed();
ushort networkBehaviourIndex = reader.ReadUInt16Packed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ internal void SpawnNetworkObjectLocally(NetworkObject networkObject, ulong netwo
throw new SpawnStateException("Object is already spawned");
}

if (readNetworkVariable && NetworkManager.NetworkConfig.EnableNetworkVariable)
if (readNetworkVariable)
{
networkObject.SetNetworkVariableData(dataStream);
}
Expand Down Expand Up @@ -451,10 +451,7 @@ internal void WriteSpawnCallForObject(PooledNetworkWriter writer, ulong clientId
var (isReparented, latestParent) = networkObject.GetNetworkParenting();
NetworkObject.WriteNetworkParenting(writer, isReparented, latestParent);
}
if (NetworkManager.NetworkConfig.EnableNetworkVariable)
{
networkObject.WriteNetworkVariableData(writer.GetStream(), clientId);
}
networkObject.WriteNetworkVariableData(writer.GetStream(), clientId);
}

internal void DespawnObject(NetworkObject networkObject, bool destroyObject = false)
Expand Down