From 9acde20b0a4237936fc028747551204208ac9677 Mon Sep 17 00:00:00 2001 From: Paul Pacheco Date: Fri, 29 Nov 2019 14:34:07 -0600 Subject: [PATCH] fix: isLocalPlayer works in host mode onStartServer (#1253) See #1250 Before: ``` Void Awake() hasAuthority=False isClient=False isLocalPlayer=False Void OnStartServer() hasAuthority=False isClient=False isLocalPlayer=False Void OnStartClient() hasAuthority=False isClient=True isLocalPlayer=False Void OnStartAuthority() hasAuthority=True isClient=True isLocalPlayer=True Void OnStartLocalPlayer() hasAuthority=True isClient=True isLocalPlayer=True Void Start() hasAuthority=True isClient=True isLocalPlayer=True ``` after ``` Void Awake() hasAuthority=False isClient=False isLocalPlayer=False Void OnStartServer() hasAuthority=False isClient=False isLocalPlayer=True Void OnStartClient() hasAuthority=False isClient=True isLocalPlayer=True Void OnStartAuthority() hasAuthority=True isClient=True isLocalPlayer=True Void OnStartLocalPlayer() hasAuthority=True isClient=True isLocalPlayer=True Void Start() hasAuthority=True isClient=True isLocalPlayer=True ``` Note it is not possible to fix Awake because it gets called before we have any chance of setting any property --- Assets/Mirror/Runtime/ClientScene.cs | 3 ++- Assets/Mirror/Runtime/NetworkIdentity.cs | 6 +----- Assets/Mirror/Runtime/NetworkServer.cs | 3 +++ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Assets/Mirror/Runtime/ClientScene.cs b/Assets/Mirror/Runtime/ClientScene.cs index f081417d4..fcefab062 100644 --- a/Assets/Mirror/Runtime/ClientScene.cs +++ b/Assets/Mirror/Runtime/ClientScene.cs @@ -723,10 +723,11 @@ static void CheckForLocalPlayer(NetworkIdentity identity) // Set isLocalPlayer to true on this NetworkIdentity and trigger OnStartLocalPlayer in all scripts on the same GO identity.connectionToServer = readyConnection; + InternalAddPlayer(identity); + identity.SetLocalPlayer(); if (LogFilter.Debug) Debug.Log("ClientScene.OnOwnerMessage - player=" + identity.name); - InternalAddPlayer(identity); identity.pendingLocalPlayer = false; } diff --git a/Assets/Mirror/Runtime/NetworkIdentity.cs b/Assets/Mirror/Runtime/NetworkIdentity.cs index ec087aac6..c2f594191 100644 --- a/Assets/Mirror/Runtime/NetworkIdentity.cs +++ b/Assets/Mirror/Runtime/NetworkIdentity.cs @@ -73,7 +73,7 @@ public bool isServer /// This returns true if this object is the one that represents the player on the local machine. /// This is set when the server has spawned an object for this particular client. /// - public bool isLocalPlayer { get; private set; } + public bool isLocalPlayer => ClientScene.localPlayer == this; internal bool pendingLocalPlayer { get; set; } @@ -207,8 +207,6 @@ internal void SetClientOwner(NetworkConnection conn) // used when the player object for a connection changes internal void SetNotLocalPlayer() { - isLocalPlayer = false; - if (NetworkServer.active && NetworkServer.localClientActive) { // dont change authority for objects on the host @@ -871,7 +869,6 @@ internal void OnUpdateVars(NetworkReader reader, bool initialState) internal void SetLocalPlayer() { - isLocalPlayer = true; hasAuthority = true; NotifyAuthority(); @@ -1147,7 +1144,6 @@ internal void Reset() m_IsServer = false; netId = 0; - isLocalPlayer = false; connectionToServer = null; connectionToClient = null; networkBehavioursCache = null; diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index 17cdee8f9..16fb2264b 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -819,7 +819,10 @@ public static bool AddPlayerForConnection(NetworkConnection conn, GameObject pla // special case, we are in host mode, set hasAuthority to true so that all overrides see it if (conn is ULocalConnectionToClient) + { identity.hasAuthority = true; + ClientScene.InternalAddPlayer(identity); + } // set ready if not set yet SetClientReady(conn);