Skip to content

Commit f593acc

Browse files
test
Adding the 11th test to the order of operations test.
1 parent 39fe8be commit f593acc

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

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

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ public IEnumerator OrderOfOperations()
191191

192192
ConfigureSequencesTest10(m_AuthorityGenericInstances[0], m_AuthorityGenericInstances[1]);
193193
yield return RunTestSequences(spawnWithOwnership: true);
194+
195+
ConfigureSequencesTest11(m_AuthorityGenericInstances[0], m_AuthorityGenericInstances[1]);
196+
yield return RunTestSequences(spawnWithOwnership: true);
194197
}
195198

196199
private IEnumerator RunTestSequences(bool spawnWithObservers = true, bool spawnWithOwnership = false)
@@ -631,9 +634,77 @@ private void ConfigureSequencesTest10(NetworkObject parent1, NetworkObject paren
631634
SpawnSequenceController.AddAction(parentSequence1);
632635
SpawnSequenceController.AddAction(parentRpc);
633636
}
637+
638+
/// <summary>
639+
/// Test-11: (Client-Server Only)
640+
/// Authority-> Spawn with ownership, Parent RPC with NetworkObjectReference.
641+
/// ClientOwner-> Parent (1) RPC using NetworkObjectReference
642+
/// Server-> On the parent changing --> re-parent (2)
643+
/// </summary>
644+
private void ConfigureSequencesTest11(NetworkObject parent1, NetworkObject parent2)
645+
{
646+
SpawnSequenceController.CurrentTest = "Test11 (Client-Server Only)";
647+
if (m_AuthorityNetworkManager.DistributedAuthorityMode)
648+
{
649+
SpawnSequenceController.ClientServerOnly = true;
650+
return;
651+
}
652+
653+
654+
var parentRpc = new ReferenceRpcSequence()
655+
{
656+
IsParentRPC = true,
657+
Parent = parent1,
658+
ReferenceTeleportHelperId = parent1.GetComponent<ReferenceRpcHelper>().NetworkObjectId,
659+
Stage = SpawnSequence.SpawnStage.AfterSpawn,
660+
};
661+
662+
var reparent = new ParentSequence()
663+
{
664+
TargetParent = parent2,
665+
Stage = SpawnSequence.SpawnStage.Conditional,
666+
};
667+
668+
// Authority
669+
var conditionalParent = new ConditionalParentSequence()
670+
{
671+
ConditionalSequence = reparent,
672+
ParentToWaitFor = parent1,
673+
Stage = SpawnSequence.SpawnStage.Conditional,
674+
};
675+
676+
SpawnSequenceController.AddAction(parentRpc);
677+
SpawnSequenceController.AddAction(reparent);
678+
SpawnSequenceController.AddAction(conditionalParent);
679+
}
634680
#endregion
635681

636682
#region Sequence Class Definitions
683+
684+
internal class ConditionalParentSequence : SpawnSequence
685+
{
686+
public NetworkObject ParentToWaitFor;
687+
688+
protected override bool OnShouldInvoke(SpawnStage stage)
689+
{
690+
// This could use additional properties to extend who
691+
// registers for the parenting event (i.e. in a client-server topology).
692+
if (m_NetworkObject.HasAuthority && stage == SpawnStage.PostSpawn)
693+
{
694+
m_SpawnSequenceController.OnParentChanged += OnParentChanged;
695+
}
696+
return base.OnShouldInvoke(stage) && m_NetworkObject.HasAuthority;
697+
}
698+
699+
private void OnParentChanged(NetworkObject parent)
700+
{
701+
if (ParentToWaitFor.NetworkObjectId == parent.NetworkObjectId)
702+
{
703+
ConditionReached();
704+
}
705+
}
706+
}
707+
637708
internal class NetworkShowSequence : SpawnSequence
638709
{
639710
public List<ulong> Clients = new List<ulong>();
@@ -802,11 +873,14 @@ public enum SpawnStage
802873
{
803874
Spawn,
804875
PostSpawn,
805-
AfterSpawn
876+
AfterSpawn,
877+
Conditional,
806878
};
807879

808880
public SpawnStage Stage;
809881

882+
public SpawnSequence ConditionalSequence;
883+
810884
public bool WasInvoked { get; protected set; }
811885
public bool InvokePending { get; protected set; }
812886

@@ -816,6 +890,19 @@ public enum SpawnStage
816890
protected SpawnSequenceController m_SpawnSequenceController;
817891
protected NetworkObject m_NetworkObject;
818892

893+
protected void ConditionReached()
894+
{
895+
if (ConditionalSequence != null)
896+
{
897+
WasInvoked = true;
898+
ConditionalSequence.Action(SpawnStage.Conditional, m_SpawnSequenceController);
899+
}
900+
else
901+
{
902+
Debug.LogError($"[{GetType().Name}] Condition reached but {nameof(ConditionalSequence)} is null!");
903+
}
904+
}
905+
819906
protected virtual bool OnShouldInvoke(SpawnStage stage)
820907
{
821908
if (InvokeOnlyOnClientId.HasValue && m_NetworkObject.NetworkManager.LocalClientId != InvokeOnlyOnClientId.Value)
@@ -928,6 +1015,14 @@ public static bool AllActionsInvoked()
9281015
return true;
9291016
}
9301017

1018+
public event System.Action<NetworkObject> OnParentChanged;
1019+
1020+
public override void OnNetworkObjectParentChanged(NetworkObject parentNetworkObject)
1021+
{
1022+
OnParentChanged?.Invoke(parentNetworkObject);
1023+
base.OnNetworkObjectParentChanged(parentNetworkObject);
1024+
}
1025+
9311026
private void InvokeSequencesForStage(SpawnSequence.SpawnStage spawnStage)
9321027
{
9331028
foreach (var action in s_SpawnSequencedActions)

0 commit comments

Comments
 (0)