Skip to content

Commit

Permalink
Fix ReplacePlayerForConn isLocalPlayer (MirrorNetworking#2969)
Browse files Browse the repository at this point in the history
- Moved `ChangeOwner` down below `OnChangeOwner`
- Added `isLocalPlayer` bool to `ChangeOwnerMessage`
- Added handling for `isLocalPlayer to `ChangeOwner`
- Added call to `SendChangeOwnerMessage` to `ReplacePlayerForConnection` for when `keepAuthority` is true
- Fixes MirrorNetworking#2968
  • Loading branch information
MrGadget1024 authored Oct 21, 2021
1 parent 282c843 commit 9184d56
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions Assets/Mirror/Runtime/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public struct ChangeOwnerMessage : NetworkMessage
{
public uint netId;
public bool isOwner;
public bool isLocalPlayer;
}

public struct ObjectSpawnStartedMessage : NetworkMessage {}
Expand Down
24 changes: 18 additions & 6 deletions Assets/Mirror/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,12 +1020,6 @@ internal static void ApplySpawnPayload(NetworkIdentity identity, SpawnMessage me
}
}

internal static void ChangeOwner(NetworkIdentity identity, ChangeOwnerMessage message)
{
identity.hasAuthority = message.isOwner;
identity.NotifyAuthority();
}

// Finds Existing Object with NetId or spawns a new one using AssetId or sceneId
internal static bool FindOrSpawnObject(SpawnMessage message, out NetworkIdentity identity)
{
Expand Down Expand Up @@ -1286,6 +1280,24 @@ internal static void OnChangeOwner(ChangeOwnerMessage message)
Debug.LogError($"OnChangeOwner: Could not find object with netId {message.netId}");
}

internal static void ChangeOwner(NetworkIdentity identity, ChangeOwnerMessage message)
{
identity.hasAuthority = message.isOwner;
identity.NotifyAuthority();

identity.isLocalPlayer = message.isLocalPlayer;
if (identity.isLocalPlayer)
localPlayer = identity;
else if (localPlayer == identity)
{
// localPlayer may already be assigned to something else
// so only make it null if it's this identity.
localPlayer = null;
}

CheckForLocalPlayer(identity);
}

internal static void CheckForLocalPlayer(NetworkIdentity identity)
{
if (identity == localPlayer)
Expand Down
14 changes: 12 additions & 2 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,12 @@ public static bool ReplacePlayerForConnection(NetworkConnection conn, GameObject

Respawn(identity);

if (!keepAuthority)
if (keepAuthority)
// This needs to be sent to clear isLocalPlayer on
// client while keeping hasAuthority true
SendChangeOwnerMessage(previousPlayer, conn);
else
// This clears both isLocalPlayer and hasAuthority on client
previousPlayer.RemoveClientAuthority();

return true;
Expand Down Expand Up @@ -955,7 +960,12 @@ internal static void SendChangeOwnerMessage(NetworkIdentity identity, NetworkCon

//Debug.Log($"Server SendChangeOwnerMessage: name={identity.name} netid={identity.netId}");

conn.Send(new ChangeOwnerMessage { netId = identity.netId, isOwner = identity.connectionToClient == conn });
conn.Send(new ChangeOwnerMessage
{
netId = identity.netId,
isOwner = identity.connectionToClient == conn,
isLocalPlayer = conn.identity == identity
});
}

static void SpawnObject(GameObject obj, NetworkConnection ownerConnection)
Expand Down

0 comments on commit 9184d56

Please sign in to comment.