fix #1475: isClient is true in OnDestroy on host/client mode again. Reverts commit d00c95bb55 (#1483)

* fix #1475: isClient is true in OnDestroy on host/client mode again. Reverts commit d00c95bb55

* add comment

* ClientScene.InternalAddPlayer doesn't need to set isClient anymore
This commit is contained in:
vis2k 2020-02-07 10:14:54 +01:00 committed by GitHub
parent 82a4af9a62
commit 9f7c30c1ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -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;

View File

@ -57,7 +57,13 @@ public sealed class NetworkIdentity : MonoBehaviour
/// <summary>
/// Returns true if running as a client and this object was spawned by a server.
/// </summary>
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; }
/// <summary>
/// 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;