Get PhotonView through Player object / Get all PhotonViews in current room

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Get PhotonView through Player object / Get all PhotonViews in current room

StrikeAgainst
2022-01-13 12:50:14

I'm having a case where I need to acess each player's photon-instantiated gameobject in a room in short intervals. I figured I could do this via

PhotonNetwork.CurrentRoom.Players    

and then grab each player's individual PhotonView Component, until I figured that the Player object doesn't seem to support any functionality to do so. Am I missing something or is this just impossible?

Alternatively I'd also take some functionality to get all PhotonViews in the current room, but apart from

FindObjectsOfType<PhotonView>()    

which could kill my performance, there seems to be none as well. Does someone have an idea how to do so in a reasonably performant way?

Comments

DhoodPro
2022-01-14 01:40:29

I would propose creating a list to hold the players in the room then adding to the list as a player enters through an RPC. Then run your intervals locally (preferably for performance), or through a stream, by just searching through the list of players.

StrikeAgainst
2022-01-20 01:27:39

That's rather cumbersome... but I guess there ain't another way then.

JohnTube
2022-01-21 06:05:13

Hi @StrikeAgainst,

The idea is that PhotonView ViewID is generated from the Player's ActorNr.

More details here.

public List<PhotonView> GetAllPhotonViewsPerPlayer(Player player)    
    {    
      if (player != null)    
      {    
        List<PhotonView> list = new List<PhotonView>();    
        int actorNr = player.ActorNumber;    
        for(int viewId = actorNr * PhotonNetwork.MAX_VIEW_IDS + 1; viewId < (actorNr + 1) * PhotonNetwork.MAX_VIEW_IDS; viewId++)    
        {    
          PhotonView photonView = PhotonView.Find(viewId);    
          if (photonView)    
          {    
            list.Add(photonView);    
          }    
        }    
        return list;    
      }    
      return null;    
    }    
Back to top