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
This commit is contained in:
Paul Pacheco 2019-11-29 14:34:07 -06:00 committed by GitHub
parent d00c95bb55
commit 9acde20b0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 6 deletions

View File

@ -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 // Set isLocalPlayer to true on this NetworkIdentity and trigger OnStartLocalPlayer in all scripts on the same GO
identity.connectionToServer = readyConnection; identity.connectionToServer = readyConnection;
InternalAddPlayer(identity);
identity.SetLocalPlayer(); identity.SetLocalPlayer();
if (LogFilter.Debug) Debug.Log("ClientScene.OnOwnerMessage - player=" + identity.name); if (LogFilter.Debug) Debug.Log("ClientScene.OnOwnerMessage - player=" + identity.name);
InternalAddPlayer(identity);
identity.pendingLocalPlayer = false; identity.pendingLocalPlayer = false;
} }

View File

@ -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 returns true if this object is the one that represents the player on the local machine.
/// <para>This is set when the server has spawned an object for this particular client.</para> /// <para>This is set when the server has spawned an object for this particular client.</para>
/// </summary> /// </summary>
public bool isLocalPlayer { get; private set; } public bool isLocalPlayer => ClientScene.localPlayer == this;
internal bool pendingLocalPlayer { get; set; } internal bool pendingLocalPlayer { get; set; }
@ -207,8 +207,6 @@ internal void SetClientOwner(NetworkConnection conn)
// used when the player object for a connection changes // used when the player object for a connection changes
internal void SetNotLocalPlayer() internal void SetNotLocalPlayer()
{ {
isLocalPlayer = false;
if (NetworkServer.active && NetworkServer.localClientActive) if (NetworkServer.active && NetworkServer.localClientActive)
{ {
// dont change authority for objects on the host // dont change authority for objects on the host
@ -871,7 +869,6 @@ internal void OnUpdateVars(NetworkReader reader, bool initialState)
internal void SetLocalPlayer() internal void SetLocalPlayer()
{ {
isLocalPlayer = true;
hasAuthority = true; hasAuthority = true;
NotifyAuthority(); NotifyAuthority();
@ -1147,7 +1144,6 @@ internal void Reset()
m_IsServer = false; m_IsServer = false;
netId = 0; netId = 0;
isLocalPlayer = false;
connectionToServer = null; connectionToServer = null;
connectionToClient = null; connectionToClient = null;
networkBehavioursCache = null; networkBehavioursCache = null;

View File

@ -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 // special case, we are in host mode, set hasAuthority to true so that all overrides see it
if (conn is ULocalConnectionToClient) if (conn is ULocalConnectionToClient)
{
identity.hasAuthority = true; identity.hasAuthority = true;
ClientScene.InternalAddPlayer(identity);
}
// set ready if not set yet // set ready if not set yet
SetClientReady(conn); SetClientReady(conn);