Skip to content

Commit

Permalink
feat: adding OwnerChanged to networkIdentity (#1077)
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed May 12, 2022
1 parent aae30b3 commit 7c94ab7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Assets/Mirage/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,16 @@ public INetworkPlayer Owner

internal set
{
// do nothing if value is the same
if (_owner == value)
return;

if (_owner != null)
_owner.RemoveOwnedObject(this);

_owner = value;
_owner?.AddOwnedObject(this);
_onOwnerChanged.Invoke(_owner);
}
}

Expand Down Expand Up @@ -351,6 +356,7 @@ internal set
[SerializeField] AddLateEvent _onStartClient = new AddLateEvent();
[SerializeField] AddLateEvent _onStartLocalPlayer = new AddLateEvent();
[SerializeField] BoolAddLateEvent _onAuthorityChanged = new BoolAddLateEvent();
[SerializeField] NetworkPlayerAddLateEvent _onOwnerChanged = new NetworkPlayerAddLateEvent();
[SerializeField] AddLateEvent _onStopClient = new AddLateEvent();
[SerializeField] AddLateEvent _onStopServer = new AddLateEvent();

Expand Down Expand Up @@ -394,6 +400,13 @@ internal set
/// </summary>
public IAddLateEvent<bool> OnAuthorityChanged => _onAuthorityChanged;

/// <summary>
/// This is invoked on behaviours that have an owner assigned.
/// <para>This even is only called on server</para>
/// <para>See <see cref="OnAuthorityChanged"/> for more comments on owner and authority</para>
/// </summary>
public IAddLateEvent<INetworkPlayer> OnOwnerChanged => _onOwnerChanged;

/// <summary>
/// This is invoked on clients when the server has caused this object to be destroyed.
/// <para>This can be used as a hook to invoke effects or do client specific cleanup.</para>
Expand Down
41 changes: 41 additions & 0 deletions Assets/Tests/Runtime/ClientServer/NetworkIdentityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,46 @@ public IEnumerator OnAuthorityChanged_Client()
Assert.That(hasAuthCalls.Count, Is.EqualTo(1));
Assert.That(hasAuthCalls.Dequeue(), Is.False);
}

[Test]
public void OnOwnerChanged_Server()
{
var hasAuthCalls = new Queue<INetworkPlayer>();
serverIdentity2.OnOwnerChanged.AddListener(newOwner =>
{
hasAuthCalls.Enqueue(newOwner);
});

serverIdentity2.AssignClientAuthority(serverPlayer);

Assert.That(hasAuthCalls.Count, Is.EqualTo(1));
Assert.That(hasAuthCalls.Dequeue(), Is.EqualTo(serverPlayer));

serverIdentity2.RemoveClientAuthority();

Assert.That(hasAuthCalls.Count, Is.EqualTo(1));
Assert.That(hasAuthCalls.Dequeue(), Is.Null);
}

[UnityTest]
[Description("OnOwnerChanged should not be called on client side")]
public IEnumerator OnOwnerChanged_Client()
{
var hasAuthCalls = new Queue<INetworkPlayer>();
clientIdentity2.OnOwnerChanged.AddListener(newOwner =>
{
hasAuthCalls.Enqueue(newOwner);
});

serverIdentity2.AssignClientAuthority(serverPlayer);
yield return new WaitForSeconds(0.1f);

Assert.That(hasAuthCalls.Count, Is.EqualTo(0));

serverIdentity2.RemoveClientAuthority();
yield return new WaitForSeconds(0.1f);

Assert.That(hasAuthCalls.Count, Is.EqualTo(0));
}
}
}

0 comments on commit 7c94ab7

Please sign in to comment.