Skip to content

Commit

Permalink
fix: don't call OnStartLocalPlayer twice (#1263)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpach committed Dec 1, 2019
1 parent 4d373c5 commit c753089
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
15 changes: 7 additions & 8 deletions Assets/Mirror/Runtime/ClientScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,6 @@ public static GameObject FindLocalObject(uint netId)
static void ApplySpawnPayload(NetworkIdentity identity, SpawnMessage msg)
{
identity.Reset();
identity.pendingLocalPlayer = msg.isLocalPlayer;
identity.assetId = msg.assetId;

if (!identity.gameObject.activeSelf)
Expand Down Expand Up @@ -667,11 +666,13 @@ internal static void OnHostClientSpawn(SpawnMessage msg)
{
if (NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity localObject) && localObject != null)
{
localObject.pendingLocalPlayer = msg.isLocalPlayer;
localObject.OnStartClient();
if (msg.isLocalPlayer)
InternalAddPlayer(localObject);

localObject.hasAuthority = msg.isOwner;
localObject.OnSetHostVisibility(true);
localObject.NotifyAuthority();
localObject.OnStartClient();
localObject.OnSetHostVisibility(true);
CheckForLocalPlayer(localObject);
}
}
Expand Down Expand Up @@ -716,7 +717,7 @@ internal static void OnSyncEventMessage(SyncEventMessage msg)

static void CheckForLocalPlayer(NetworkIdentity identity)
{
if (identity.pendingLocalPlayer)
if (identity == localPlayer)
{
if (readyConnection.identity != null && readyConnection.identity != identity)
{
Expand All @@ -726,11 +727,9 @@ 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;
identity.SetLocalPlayer();
identity.OnStartLocalPlayer();

if (LogFilter.Debug) Debug.Log("ClientScene.OnOwnerMessage - player=" + identity.name);

identity.pendingLocalPlayer = false;
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions Assets/Mirror/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ public sealed class NetworkIdentity : MonoBehaviour
/// </summary>
public bool isLocalPlayer => ClientScene.localPlayer == this;

internal bool pendingLocalPlayer { get; set; }

/// <summary>
/// This returns true if this object is the authoritative player object on the client.
/// <para>This value is determined at runtime. For most objects, authority is held by the server.</para>
Expand Down Expand Up @@ -847,10 +845,12 @@ internal void OnUpdateVars(NetworkReader reader, bool initialState)
OnDeserializeAllSafely(reader, initialState);
}

internal void SetLocalPlayer()
private static NetworkIdentity previousLocalPlayer = null;
internal void OnStartLocalPlayer()
{
hasAuthority = true;
NotifyAuthority();
if (previousLocalPlayer == this)
return;
previousLocalPlayer = this;

foreach (NetworkBehaviour comp in networkBehavioursCache)
{
Expand Down

0 comments on commit c753089

Please sign in to comment.