From 9f7c30c1ac3712a0dff182d96e7c87f58b245160 Mon Sep 17 00:00:00 2001 From: vis2k Date: Fri, 7 Feb 2020 10:14:54 +0100 Subject: [PATCH] fix #1475: isClient is true in OnDestroy on host/client mode again. Reverts commit d00c95bb55eedceb4f0811de54604c960c9352fe (#1483) * fix #1475: isClient is true in OnDestroy on host/client mode again. Reverts commit d00c95bb55eedceb4f0811de54604c960c9352fe * add comment * ClientScene.InternalAddPlayer doesn't need to set isClient anymore --- Assets/Mirror/Runtime/ClientScene.cs | 5 +++++ Assets/Mirror/Runtime/NetworkIdentity.cs | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/ClientScene.cs b/Assets/Mirror/Runtime/ClientScene.cs index 9ac86eb80..985eed215 100644 --- a/Assets/Mirror/Runtime/ClientScene.cs +++ b/Assets/Mirror/Runtime/ClientScene.cs @@ -73,6 +73,11 @@ internal static void InternalAddPlayer(NetworkIdentity identity) // NOTE: It can be "normal" when changing scenes for the player to be destroyed and recreated. // But, the player structures are not cleaned up, we'll just replace the old player localPlayer = identity; + + // NOTE: we DONT need to set isClient=true here, because OnStartClient + // is called before OnStartLocalPlayer, hence it's already set. + // localPlayer.isClient = true; + if (readyConnection != null) { readyConnection.identity = identity; diff --git a/Assets/Mirror/Runtime/NetworkIdentity.cs b/Assets/Mirror/Runtime/NetworkIdentity.cs index 5ac73a1d7..9cafa3720 100644 --- a/Assets/Mirror/Runtime/NetworkIdentity.cs +++ b/Assets/Mirror/Runtime/NetworkIdentity.cs @@ -57,7 +57,13 @@ public sealed class NetworkIdentity : MonoBehaviour /// /// Returns true if running as a client and this object was spawned by a server. /// - public bool isClient => NetworkClient.active && netId != 0 && !serverOnly; + // + // IMPORTANT: checking NetworkClient.active means that isClient is false in OnDestroy: + // public bool isClient => NetworkClient.active && netId != 0 && !serverOnly; + // but we need it in OnDestroy, e.g. when saving skillbars on quit. this + // works fine if we keep the UNET way of setting isClient manually. + // => fixes https://github.com/vis2k/Mirror/issues/1475 + public bool isClient { get; internal set; } /// /// Returns true if NetworkServer.active and server is not stopped. @@ -509,6 +515,13 @@ internal void OnStartServer() // because we already set m_isServer=true above) spawned[netId] = this; + // in host mode we set isClient true before calling OnStartServer, + // otherwise isClient is false in OnStartServer. + if (NetworkClient.active) + { + isClient = true; + } + foreach (NetworkBehaviour comp in NetworkBehaviours) { try @@ -529,6 +542,8 @@ internal void OnStartClient() return; clientStarted = true; + isClient = true; + if (LogFilter.Debug) Debug.Log("OnStartClient " + gameObject + " netId:" + netId); foreach (NetworkBehaviour comp in NetworkBehaviours) { @@ -1135,6 +1150,7 @@ internal void Reset() return; clientStarted = false; + isClient = false; reset = false; netId = 0;