Skip to content

feat: NetworkObject Parenting #855

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 54 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
e73a720
networkobject parent tracking
0xFA11 May 23, 2021
ea6b6e7
NetworkConstants.PARENT_SYNC = 16
0xFA11 May 23, 2021
68c1812
minor nullptr fix
0xFA11 May 23, 2021
2df134d
minor cleanup
0xFA11 May 23, 2021
a175ef3
sync networkobject reparenting props
0xFA11 May 23, 2021
3c03ddb
parent sync with orphan children
0xFA11 May 23, 2021
dac2e0f
sync reparenting at runtime
0xFA11 May 23, 2021
e161d73
working poc
0xFA11 May 23, 2021
e5eaf2a
more networkobject parenting restrictions (IsListening && IsServer &&…
0xFA11 May 24, 2021
33fb6ac
minor fix
0xFA11 May 24, 2021
342495b
cleanup
0xFA11 May 24, 2021
8151633
Merge branch 'develop' into feat/reparent
0xFA11 May 24, 2021
1766b58
minor update: exception message
0xFA11 May 25, 2021
86a1532
nullptr check
0xFA11 May 25, 2021
d7a4a3c
reset networkobject.orphanchildren set on networkmanager.initialize
0xFA11 May 25, 2021
3588afe
NetworkBehaviour.OnNetworkObjectParentChanged
0xFA11 May 27, 2021
91a5358
Merge branch 'develop' into feat/reparent
0xFA11 May 31, 2021
73d6431
do not pop invalid moves to the root
0xFA11 Jun 1, 2021
778d60f
NetworkObject.TrySetParent & InvalidParentException
0xFA11 Jun 2, 2021
1025dbf
bool flag to enable/disable
0xFA11 Jun 2, 2021
e7782d7
Merge branch 'develop' into feat/reparent
0xFA11 Jun 3, 2021
cddb332
Merge branch 'develop' into feat/reparent
0xFA11 Jun 3, 2021
0c593ff
implement networkobject runtime tests
0xFA11 Jun 5, 2021
e52f623
Merge branch 'develop' into feat/reparent
0xFA11 Jun 5, 2021
e3f43cb
./standards.py --fix
0xFA11 Jun 5, 2021
ca90125
revert SampleScene changes
0xFA11 Jun 5, 2021
43b6e08
revert SampleScene.unity changes
0xFA11 Jun 5, 2021
964a1cf
delete NetworkDevDebug script
0xFA11 Jun 5, 2021
8e2863f
implement ReparentingCubeNetBhv to track new NetworkObject parent in …
0xFA11 Jun 5, 2021
26bc467
clarify namings
0xFA11 Jun 5, 2021
d524131
compare InstanceIDs instead of Transforms
0xFA11 Jun 5, 2021
617c1bb
debug PrintHierarchy
0xFA11 Jun 5, 2021
acbe66d
better log printing
0xFA11 Jun 5, 2021
e5882ee
increase frame wait time
0xFA11 Jun 5, 2021
a5941f6
more granular logs
0xFA11 Jun 5, 2021
37eeee1
add netobj id to logs
0xFA11 Jun 5, 2021
3b8e802
add more verbose logs
0xFA11 Jun 5, 2021
c602862
try with host mode
0xFA11 Jun 5, 2021
d699581
targetframerate & more logs craziness
0xFA11 Jun 5, 2021
326c7ab
let's see
0xFA11 Jun 5, 2021
db40b35
TARGETFRAMERATE?!?!11!!1
0xFA11 Jun 5, 2021
92d369c
cleanup
0xFA11 Jun 5, 2021
40b45fe
50fps
0xFA11 Jun 6, 2021
33bf226
add placeholder OnNetworkObjectParentChanged callback handler
0xFA11 Jun 7, 2021
235aea6
more comments and warning log
0xFA11 Jun 7, 2021
6df1324
hey hey Yamato
0xFA11 Jun 8, 2021
50d1cf3
move child NetworkObject to the root on parent NetworkObject destroy
0xFA11 Jun 8, 2021
309398f
Merge branch 'develop' into feat/reparent
0xFA11 Jun 8, 2021
0ea6dec
downgrade PARENT_SYNC log to Developer from Warning as we expect it t…
0xFA11 Jun 9, 2021
4c6534e
Merge branch 'develop' into feat/reparent
0xFA11 Jun 9, 2021
7217098
remove Application.targetFrameRate trick
0xFA11 Jun 9, 2021
f2876d2
Merge branch 'develop' into feat/reparent
0xFA11 Jun 10, 2021
8619915
Merge branch 'develop' into feat/reparent
0xFA11 Jun 11, 2021
467fe99
merge branch 'develop' into 'feat/reparent'
0xFA11 Jun 18, 2021
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
15 changes: 6 additions & 9 deletions com.unity.multiplayer.mlapi/Prototyping/NetworkTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using MLAPI.NetworkVariable;
using MLAPI.Transports;
using UnityEngine;
using UnityEngine.Serialization;

namespace MLAPI.Prototyping
{
Expand Down Expand Up @@ -41,24 +40,20 @@ public enum Authority
/// TODO MTT-766 once we have per var interpolation
/// Enable interpolation
/// </summary>
// ReSharper disable once NotAccessedField.Global
[FormerlySerializedAs("m_InterpolatePosition")]
[SerializeField, Tooltip("This requires AssumeSyncedSends to be true")]
public bool InterpolatePosition = true;

/// <summary>
/// TODO MTT-766 once we have per var interpolation
/// The distance before snaping to the position
/// </summary>
// ReSharper disable once NotAccessedField.Global
[SerializeField, Tooltip("The transform will snap if the distance is greater than this distance")]
public float SnapDistance = 10f;

/// <summary>
/// TODO MTT-766 once we have per var interpolation
/// Should the server interpolate
/// </summary>
// ReSharper disable once NotAccessedField.Global
[SerializeField]
public bool InterpolateServer = true;

Expand All @@ -68,23 +63,20 @@ public enum Authority
/// The setting in the NetworkTransform would be to just apply it to our netvars when available
/// The min meters to move before a send is sent
/// </summary>
// ReSharper disable once NotAccessedField.Global
[SerializeField, Tooltip("The min meters to move before a send is sent")]
public float MinMeters = 0.15f;

/// <summary>
/// TODO MTT-767 once we have this per var setting
/// The min degrees to rotate before a send is sent
/// </summary>
// ReSharper disable once NotAccessedField.Global
[SerializeField, Tooltip("The min degrees to rotate before a send is sent")]
public float MinDegrees = 1.5f;

/// <summary>
/// TODO MTT-767 once we have this per var setting
/// The min meters to scale before a send is sent
/// </summary>
// ReSharper disable once NotAccessedField.Global
[SerializeField, Tooltip("The min meters to scale before a send is sent")]
public float MinSize = 0.15f;

Expand Down Expand Up @@ -178,7 +170,7 @@ private void SetWorldScale(Vector3 globalScale)
m_Transform.localScale = new Vector3(globalScale.x / lossyScale.x, globalScale.y / lossyScale.y, globalScale.z / lossyScale.z);
}

private bool CanUpdateTransform()
public bool CanUpdateTransform()
{
return (IsClient && TransformAuthority == Authority.Client && IsOwner) || (IsServer && TransformAuthority == Authority.Server) || TransformAuthority == Authority.Shared;
}
Expand Down Expand Up @@ -221,6 +213,11 @@ void SetupVar<T>(NetworkVariable<T> v, T initialValue, ref T oldVal)
}
}

public override void OnNetworkObjectParentChanged(NetworkObject parentNetworkObject)
{
// TODO: handle parent NetworkObject change and potentially optimize pos/rot/scale replication?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

private NetworkVariable<T>.OnValueChangedDelegate GetOnValueChangedDelegate<T>(Action<T> assignCurrent)
{
return (old, current) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal static class NetworkConstants
internal const byte TIME_SYNC = 11;
internal const byte NETWORK_VARIABLE_DELTA = 12;
internal const byte ALL_CLIENTS_LOADED_SCENE = 14;
internal const byte PARENT_SYNC = 16;
internal const byte UNNAMED_MESSAGE = 20;
internal const byte DESTROY_OBJECTS = 21;
internal const byte NAMED_MESSAGE = 22;
Expand Down Expand Up @@ -45,7 +46,7 @@ internal static class NetworkConstants
"",
"ALL_CLIENTS_SWITCH_SCENE_COMPLETED",
"",
"", // 16
"PARENT_SYNC", // 16
"",
"",
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ public virtual void OnGainedOwnership() { }
/// </summary>
public virtual void OnLostOwnership() { }

/// <summary>
/// Gets called when the parent NetworkObject of this NetworkBehaviour's NetworkObject has changed
/// </summary>
public virtual void OnNetworkObjectParentChanged(NetworkObject parentNetworkObject) { }

private bool m_VarInit = false;

private readonly List<HashSet<int>> m_ChannelMappedNetworkVariableIndexes = new List<HashSet<int>>();
Expand Down
34 changes: 29 additions & 5 deletions com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,11 @@ private void OnValidate()
}
#endif

private void Init(bool server)
private void Initialize(bool server)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
{
NetworkLog.LogInfo(nameof(Init));
NetworkLog.LogInfo(nameof(Initialize));
}

LocalClientId = 0;
Expand All @@ -337,6 +337,7 @@ private void Init(bool server)
PendingClients.Clear();
ConnectedClients.Clear();
ConnectedClientsList.Clear();
NetworkObject.OrphanChildren.Clear();

// Create spawn manager instance
SpawnManager = new NetworkSpawnManager(this);
Expand Down Expand Up @@ -541,7 +542,7 @@ public SocketTasks StartServer()
}
}

Init(true);
Initialize(true);

var socketTasks = NetworkConfig.NetworkTransport.StartServer();

Expand Down Expand Up @@ -576,7 +577,7 @@ public SocketTasks StartClient()
return SocketTask.Fault.AsTasks();
}

Init(false);
Initialize(false);

var socketTasks = NetworkConfig.NetworkTransport.StartClient();

Expand Down Expand Up @@ -702,7 +703,7 @@ public SocketTasks StartHost()
}
}

Init(true);
Initialize(true);

var socketTasks = NetworkConfig.NetworkTransport.StartServer();

Expand Down Expand Up @@ -1330,6 +1331,29 @@ internal void HandleIncomingData(ulong clientId, NetworkChannel networkChannel,
}
}

break;
}
case NetworkConstants.PARENT_SYNC:
{
if (IsClient)
{
using (var reader = PooledNetworkReader.Get(messageStream))
{
var networkObjectId = reader.ReadUInt64Packed();
var (isReparented, latestParent) = NetworkObject.ReadNetworkParenting(reader);
if (SpawnManager.SpawnedObjects.ContainsKey(networkObjectId))
{
var networkObject = SpawnManager.SpawnedObjects[networkObjectId];
networkObject.SetNetworkParenting(isReparented, latestParent);
networkObject.ApplyNetworkParenting();
}
else if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
{
NetworkLog.LogWarning($"Read {nameof(NetworkConstants.PARENT_SYNC)} for {nameof(NetworkObject)} #{networkObjectId} but could not find it in the {nameof(SpawnManager.SpawnedObjects)}");
}
}
}

break;
}
default:
Expand Down
Loading