Get PhotonView through Player object / Get all PhotonViews in current room
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?
Answers
-
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.
0 -
That's rather cumbersome... but I guess there ain't another way then.
0 -
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; }
1