fix: #2635 NetworkIdentity.OnDestroy only clears NetworkClient.localPlayer if WE are STILL the local player.

fixes data race in rooms scene (explanation see comments)
This commit is contained in:
vis2k 2021-03-13 18:29:20 +08:00
parent 2cd4403429
commit 457b21d907

View File

@ -528,7 +528,28 @@ void OnDestroy()
if (isLocalPlayer)
{
NetworkClient.ClearLocalPlayer();
// previously there was a bug where isLocalPlayer was
// false in OnDestroy because it was dynamically defined as:
// isLocalPlayer => NetworkClient.localPlayer == this
// we fixed it by setting isLocalPlayer manually and never
// resetting it.
//
// BUT now we need to be aware of a possible data race like in
// our rooms example:
// => GamePlayer is in world
// => player returns to room
// => GamePlayer is destroyed
// => NetworkClient.localPlayer is set to RoomPlayer
// => GamePlayer.OnDestroy is called 1 frame later
// => GamePlayer.OnDestroy 'isLocalPlayer' is true, so here we
// are trying to clear NetworkClient.localPlayer
// => which would overwrite the new RoomPlayer local player
//
// FIXED by simply only clearing if NetworkClient.localPlayer
// still points to US!
// => see also: https://github.com/vis2k/Mirror/issues/2635
if (NetworkClient.localPlayer == this)
NetworkClient.ClearLocalPlayer();
}
}