Skip to content

fix: CleanDiffedSceneObjects was not clearing PendingSoftSyncObjects #834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 20, 2021

Conversation

NoelStephensUnity
Copy link
Collaborator

Fix is based on MTT-772:
NetworkSpawnManager.CleanDiffedSceneObjects was cleaning remaining references to destroyed in-scene placed objects (which is detected and stored within the PendingSoftSyncObjects dictionary), but it was never clearing the PendingSoftSyncObjects dictionary which would result in a "key value already exists" error under the following conditions:

  • Scene-A is a game lobby and is loaded when connecting. Several players connect and the game is started based on . Once all players are ready they transition to the scene of choice (Scene-B).
  • Scene-B is transitioned to or loaded and contains in-scene placed NetworkObjects of which some of these in-scene placed NetworkObjects are destroyed during game play in Scene-B. Then the players transition away from Scene-B to Scene-C (post game scene because the players died or the like).
  • While in Scene-C, the players decide to try the level again and are transitioned into Scene-A to before transitioning to Scene-B again.
  • Upon transitioning to Scene-B a "key already exists" exception will be thrown because the same in-scene NetworkObjects destroyed are being re-synchronized but the PendingSoftSyncObjects still has the same NetworkInstanceId as a key and its value would be null because

MTT-772:
Clear PendingSoftSyncObjects in CleanDiffedSceneObjects
improving the comments
@NoelStephensUnity NoelStephensUnity requested a review from 0xFA11 May 17, 2021 16:29
@@ -561,6 +561,9 @@ internal void CleanDiffedSceneObjects()
{
UnityEngine.Object.Destroy(networkObjectsToDestroy[i].gameObject);
}

// Make sure to clear this once done destroying all remaining NetworkObjects
PendingSoftSyncObjects.Clear();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at the code above, shouldn't we just iterate over PendingSoftSyncObjects, call UnityEngine.Object.Destroy on them and then do PendingSoftSyncObjects.Clear()?
I think we don't need networkObjectsToDestroy at all.

I'd suggest something like this (pseudo-code):

// Clean up any in-scene objects that had been destroyed
if (PendingSoftSyncObjects.Count > 0)
{
    foreach (var pair in PendingSoftSyncObjects)
    {
        UnityEngine.Object.Destroy(pair.Value.gameObject);
    }

    // Make sure to clear this once done destroying all remaining NetworkObjects
    PendingSoftSyncObjects.Clear();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that should work since it doesn't modify the actual dictionary but just makes the ghost Unity References null. Either approach works, this one being cleaner.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just applied your suggested fix @MFatihMAR

Copy link
Contributor

@TwoTenPvP TwoTenPvP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great changes 👌🏻

@NoelStephensUnity NoelStephensUnity merged commit 34b1a91 into develop May 20, 2021
@NoelStephensUnity NoelStephensUnity deleted the fix/CleanDiffedSceneObjects branch May 20, 2021 00:03
SamuelBellomo added a commit that referenced this pull request May 21, 2021
…player-object-get

* develop:
  test: manual connection approval to fully automated connection approval testing (#839)
  test: manual rpc tests to automated fixed revision (#841)
  fix: CleanDiffedSceneObjects was not clearing PendingSoftSyncObjects  (#834)
  feat: NetworkTransform now uses NetworkVariables instead of RPCs (#826)
  fix: gracefully handle exceptions in an RPC invoke (#846)
  fix: Fixing utp license file for legal (#843)
  ci: enable standards check on UTP too (#837)
  refactor: NetworkBehaviour.NetworkObject no longer throws an exception (#838)
  fix: revert #830 (#840)
  test: converting the manual rpc tests over to an automated unit test (#830)

# Conflicts:
#	com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/MultiInstanceHelpers.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants