Skip to content

Commit

Permalink
feat: adding public OwnedObjects list and RemoveAllOwnedObject helper…
Browse files Browse the repository at this point in the history
… method to NetworkPlayer
  • Loading branch information
James-Frowen committed Mar 23, 2024
1 parent 7d3eb22 commit 5a611ba
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Assets/Mirage/Runtime/INetworkPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,28 @@ public interface IVisibilityTracker
public interface IObjectOwner
{
event Action<NetworkIdentity> OnIdentityChanged;
/// <summary>
/// The main object owned by this player, normally the player's character
/// </summary>
NetworkIdentity Identity { get; set; }
bool HasCharacter { get; }
void RemoveOwnedObject(NetworkIdentity networkIdentity);

/// <summary>
/// All the objects owned by the player
/// </summary>
IReadOnlyCollection<NetworkIdentity> OwnedObjects { get; }
void AddOwnedObject(NetworkIdentity networkIdentity);
void RemoveOwnedObject(NetworkIdentity networkIdentity);
/// <summary>
/// Removes all owned objects. This is useful to call when player disconnects to avoid objects being destroyed
/// </summary>
/// <param name="sendAuthorityChangeEvent">Should message be send to owner client? If player is disconnecting you should set this false</param>
void RemoveAllOwnedObject(bool sendAuthorityChangeEvent);
/// <summary>
/// Destroys or unspawns all owned objects.
/// This is called when the player is disconnects.
/// It will be called after <see cref="NetworkServer.Disconnected"/>, so Disconnected can be used to remove any owned objects from the list before they are destroyed.
/// </summary>
void DestroyOwnedObjects();
}

Expand Down
39 changes: 39 additions & 0 deletions Assets/Mirage/Runtime/NetworkPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public void SetAuthentication(PlayerAuthentication authentication, bool allowRep
/// </summary>
public IReadOnlyCollection<NetworkIdentity> VisList => _visList;

public IReadOnlyCollection<NetworkIdentity> OwnedObjects => _ownedObjects;

/// <summary>
/// Disconnects the player.
Expand Down Expand Up @@ -289,6 +290,44 @@ public void RemoveOwnedObject(NetworkIdentity identity)
}
}

public void RemoveAllOwnedObject(bool sendAuthorityChangeEvent)
{
if (logger.LogEnabled()) logger.Log($"Removing all Player[{Address}] OwnedObjects");

// create a copy because the list might be modified when destroying
var ownedObjects = new HashSet<NetworkIdentity>(_ownedObjects);
var mainIdentity = Identity;

foreach (var netIdentity in ownedObjects)
{
// remove main object last
if (netIdentity == mainIdentity)
continue;

if (netIdentity == null)
continue;

// code from Identity.RemoveClientAuthority, but without the safety checks, we dont need them here
netIdentity.SetOwner(null);

if (sendAuthorityChangeEvent && netIdentity.ServerObjectManager != null)
Send(new RemoveAuthorityMessage { NetId = netIdentity.NetId });
}

if (mainIdentity != null)
{
// code from ServerObjectManager.RemoveCharacter, but without the safety checks, we dont need them here
mainIdentity.SetOwner(null);

if (sendAuthorityChangeEvent && mainIdentity.ServerObjectManager != null)
Send(new RemoveCharacterMessage { KeepAuthority = false });

}

// clear the hashset because we destroyed them all
_ownedObjects.Clear();
}

/// <summary>
/// Destroy all objects owned by this player
/// <para>NOTE: only destroyed objects that are currently spawned</para>
Expand Down

0 comments on commit 5a611ba

Please sign in to comment.