DemoAsteroids is coming up with dictionary error message

Hello,

So i am trying to pick apart the Asteroids demo and it all looks good, like how it utilizes many of the API and built in methods, however when I run it with my app ID, and run it with 2 executes, 1 will create the room the other will join the room... when the one that joins the room leaves, the 1 that created the room gets the following error message:

keyNotFoundException: The given key was not present in the dictionary.

System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <695d1cc93cca45069c528c15c9fdd749>:0)

GameConfig.OnPlayerLeftRoom (Photon.Realtime.Player otherPlayer) (at Assets/Scripts/GameConfig.cs:218)

Photon.Realtime.InRoomCallbacksContainer.OnPlayerLeftRoom (Photon.Realtime.Player otherPlayer) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:4152)

Photon.Realtime.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:3165)


I narrow down POSSIBLY that the second player, I see it updating or adding to the dictionary variable: private Dictionary<int, GameObject> playerListEntries;

which has count of 2, however when I use Debug.Log to display at the primary or the one hosting the room, it only shows 1 or doesnt know or realize the second player added to the dictionary, I check and thought maybe something is wrong with maybe method:

  public override void OnPlayerEnteredRoom(Player newPlayer)

  {

    GameObject entry = Instantiate(playerListItemPrefab);

    entry.transform.SetParent(playerListContent.transform);

    entry.GetComponent<PlayerListItem>().Initialize(newPlayer.ActorNumber, newPlayer.NickName);


    playerListEntries.Add(newPlayer.ActorNumber, entry);


    Debug.Log("playerlist entry new player added?: " + playerListEntries.Count);

    startGameButton.interactable = CheckPlayersReady();

  }


but not certain... again the Debug.Log is added just to keep track of player count... any thoughts or maybe a simple change or fix to this issue or maybe I am missing something from the demo that comes with PhotonUnityNetworking the DemoAsteroids?

Answers

  • So I did create a MAYBE fix or work around (probably work around, since the method:

      public override void OnPlayerLeftRoom(Player otherPlayer)

      {

        if (playerListEntries.ContainsKey(otherPlayer.ActorNumber))

        {

          Destroy(playerListEntries[otherPlayer.ActorNumber].gameObject);

          playerListEntries.Remove(otherPlayer.ActorNumber);

        }

      }


    so what i did above is add an IF statement to check if they Key exists Before removing it... and error went away... though not sure this is best or recommended, so far it seems to work...

  • OnPlayerEnteredRoom is Called when a remote player entered the room (see it's reference doc). So the local player will not get added to the playerListEntries, I guess.