Skip to content

Commit

Permalink
fix: replace player (remove authority by default) (#1261)
Browse files Browse the repository at this point in the history
* fix: remove authority properly in replace player

This is an alternative to #1257

Now the user can pass a parameter to ReplacePlayer to indicate
that the old player should remain attached to the connection.

Note the user could simply pass true and then call RemoveAuthority themselves
which was the approach in #1257

Personally I would prefer #1257,  but this is at least better than
master because authority is removed the proper way.

* Update NetworkServer.cs
  • Loading branch information
paulpach authored and miwarnec committed Dec 3, 2019
1 parent 8012972 commit ad724fe
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 29 deletions.
6 changes: 0 additions & 6 deletions Assets/Mirror/Runtime/ClientScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -719,12 +719,6 @@ static void CheckForLocalPlayer(NetworkIdentity identity)
{
if (identity == localPlayer)
{
if (readyConnection.identity != null && readyConnection.identity != identity)
{
readyConnection.identity.SetNotLocalPlayer();
}
// supposed to be local player, so make it the local player!

// Set isLocalPlayer to true on this NetworkIdentity and trigger OnStartLocalPlayer in all scripts on the same GO
identity.connectionToServer = readyConnection;
identity.OnStartLocalPlayer();
Expand Down
12 changes: 0 additions & 12 deletions Assets/Mirror/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,6 @@ internal void SetClientOwner(NetworkConnection conn)
/// </summary>
public static void ResetNextNetworkId() => nextNetworkId = 1;

// used when the player object for a connection changes
internal void SetNotLocalPlayer()
{
if (NetworkServer.active && NetworkServer.localClientActive)
{
// dont change authority for objects on the host
return;
}
hasAuthority = false;
NotifyAuthority();
}

// this is used when a connection is destroyed, since the "observers" property is read-only
internal void RemoveObserverInternal(NetworkConnection conn)
{
Expand Down
23 changes: 12 additions & 11 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -711,14 +711,15 @@ public static void SendToClientOfPlayer(NetworkIdentity identity, int msgType, M
/// <param name="conn">Connection which is adding the player.</param>
/// <param name="player">Player object spawned for the player.</param>
/// <param name="assetId"></param>
/// <param name="keepAuthority">Does the previous player remain attached to this connection?</param>
/// <returns></returns>
public static bool ReplacePlayerForConnection(NetworkConnection conn, GameObject player, Guid assetId)
public static bool ReplacePlayerForConnection(NetworkConnection conn, GameObject player, Guid assetId, bool keepAuthority = false)
{
if (GetNetworkIdentity(player, out NetworkIdentity identity))
{
identity.assetId = assetId;
}
return InternalReplacePlayerForConnection(conn, player);
return InternalReplacePlayerForConnection(conn, player, keepAuthority);
}

/// <summary>
Expand All @@ -727,10 +728,11 @@ public static bool ReplacePlayerForConnection(NetworkConnection conn, GameObject
/// </summary>
/// <param name="conn">Connection which is adding the player.</param>
/// <param name="player">Player object spawned for the player.</param>
/// <param name="keepAuthority">Does the previous player remain attached to this connection?</param>
/// <returns></returns>
public static bool ReplacePlayerForConnection(NetworkConnection conn, GameObject player)
public static bool ReplacePlayerForConnection(NetworkConnection conn, GameObject player, bool keepAuthority = false)
{
return InternalReplacePlayerForConnection(conn, player);
return InternalReplacePlayerForConnection(conn, player, keepAuthority);
}

/// <summary>
Expand Down Expand Up @@ -844,7 +846,7 @@ static void FinishPlayerForConnection(NetworkIdentity identity, GameObject playe
}
}

internal static bool InternalReplacePlayerForConnection(NetworkConnection conn, GameObject player)
internal static bool InternalReplacePlayerForConnection(NetworkConnection conn, GameObject player, bool keepAuthority)
{
NetworkIdentity identity = player.GetComponent<NetworkIdentity>();
if (identity == null)
Expand All @@ -856,12 +858,7 @@ internal static bool InternalReplacePlayerForConnection(NetworkConnection conn,
//NOTE: there can be an existing player
if (LogFilter.Debug) Debug.Log("NetworkServer ReplacePlayer");

// is there already an owner that is a different object??
if (conn.identity != null)
{
conn.identity.SetNotLocalPlayer();
conn.identity.connectionToClient = null;
}
NetworkIdentity previousPlayer = conn.identity;

conn.identity = identity;

Expand All @@ -888,6 +885,10 @@ internal static bool InternalReplacePlayerForConnection(NetworkConnection conn,

FinishPlayerForConnection(identity, player);
identity.SetClientOwner(conn);

if (!keepAuthority)
previousPlayer.RemoveClientAuthority();

return true;
}

Expand Down

0 comments on commit ad724fe

Please sign in to comment.