Null reference exception on PhotonPlayer.

Options
The following code gives me a null reference exception on PhotonPlayer netPlayer after Shared_RemovePlayer has been called. I think the problem is that since I am getting the netPlayer after he has disconnected and has been "cleared". How do I get around this?

[code2=csharp]public List<int> players = new List<int>();

// Called on the local client when you have joined a room.
public void OnJoinedRoom(){
photonView.RPC("Shared_AddPlayer", PhotonTargets.AllBuffered, PhotonNetwork.player);
}

// Called on all clients when a client has disconnected.
public void OnPhotonPlayerDisconnected(PhotonPlayer netPlayer){
photonView.RPC("Shared_RemovePlayer", PhotonTargets.AllBuffered, netPlayer);
}

// Adds a new player to all existing clients player lists.
// Called on the master client as well as all individual clients.
[RPC]
public void Shared_AddPlayer(PhotonPlayer netPlayer){
players.Add(netPlayer.ID);
Debug.Log("Recieved player " + netPlayer.ID + ".");
}

// Removes a player on all existing clients player lists.
// Called on the master client as well as all individual clients.
[RPC]
public void Shared_RemovePlayer(PhotonPlayer netPlayer){
players.Remove(netPlayer.ID);
Debug.Log("Removed player " + netPlayer.ID + ".");
}[/code2]

Comments

  • I've seemed to figure it out, but I'm not sure if this is an efficient way of doing it. Can a photon expert give me some pointers and tips on how to improve this:
    [code2=csharp]public List<PlayerData> players = new List<PlayerData>();

    // Called when you have connected to a room.
    public void OnJoinedRoom(){
    // You have connected to a room. Now you need to add yourself to the servers player list.
    photonView.RPC("Shared_AddPlayer", PhotonTargets.AllBuffered, PhotonNetwork.player);
    }

    // Called on all clients when a client has disconnected.
    public void OnPhotonPlayerDisconnected(PhotonPlayer netPlayer){
    if(PhotonNetwork.player.isMasterClient){
    photonView.RPC("Shared_RemovePlayer", PhotonTargets.AllBuffered, netPlayer);
    }
    }

    // Adds a new player to all existing clients player lists.
    // Called on the master client as well as all individual clients.
    [RPC]
    public void Shared_AddPlayer(PhotonPlayer netPlayer){
    players.Add(new PlayerData(netPlayer));
    Debug.Log("Recieved player " + netPlayer.ID + ".");
    }

    // Removes a player on all existing clients player lists.
    // Called on the master client as well as all individual clients.
    [RPC]
    public void Shared_RemovePlayer(PhotonPlayer netPlayer){
    for(int i = 0; i < players.Count; i++){
    if(players.netPlayer.ID == netPlayer.ID){
    players.RemoveAt(i);
    Debug.Log("Removed player " + netPlayer.ID + ".");
    }
    }
    }

    public void OnGUI(){
    if(PhotonNetwork.connectionStateDetailed == PeerState.Joined){
    GUI.Label(new Rect(5, 5, 128, 128), players.Count + "/" + 16);
    }
    }[/code2]

    I would greatly appreciate it.
  • bump
  • Tobias
    Options
    Actually, you don't have to sync joins or leaves. This is done by PUN and Photon. There callbacks OnPhotonPlayerConnected and OnPhotonPlayerDisconnected are called on all clients that remain in the game where someone left. Instead of calling an RPC, just call a method locally.
  • Well, OnPhotonPlayerConnected wouldn't work because the first person joining the game wouldn't receive that call. I think I tested that. I am trying to add the player to everyones "players" list when they join the game. I have to use RPCs so that its buffered and people who join later still get the players that were on earlier.
  • Tobias
    Options
    You can't add yourself to anyone else's player list when you're first.
    The players list is managed by our framework. Use player.SetProperties to set custom values and they get synchronized no matter if you're first or last.
  • Well that's useful.

    Thanks :)