Accessing a variable across all players.
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on Realtime.
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).
Accessing a variable across all players.
Drex
2014-07-21 02:45:09
Hey all,
So I'm hoping someone will be able to help me out with this. I'm using the Photon Unity Network and C#, I am setting up an online mutliplayer FPS game. It's going well so far but I hit a snag.
Basically, I want to have enemy players have no highlight until they go in my crosshair, then they highlight red. This part is already working. The part I'm struggling with is, I want all teammate players to highlight with a blue color at all times.
I have a teamID variable set up on each player that spawns in, 1 being the first team and 2 being the second team with 0 being no team/FFA. Is there a way for me to check every player's teamID and compare them to my own teamID? I would only want do this every time a new player joins the game or when a player switches teams. So it shouldn't be too resource intensive to just check all players every now and then.
Any ideas on this? Does Photon have a built in command I'm not aware of that can do this simply?
Thanks!
Comments
The next PUN update will have a Pickup and Teams Demo which shows how to store such a player-value and how to access it easily. In short: Use PhotonPlayer.SetCustomProperty to store the value per player and then access the PhotonView.owner to get access to it for any player in game.
Thanks for the help Tobias. I've got the part working where I can see what team each player is on. That is working fine. The problem I'm having is assigning a color to each of those player's gameobjects specifically. Is there a way to access the gameobject of the specific player in the player list?
Here is the code I have right now:
foreach(PhotonPlayer player in PhotonNetwork.playerList)
{
teamID = GetComponent<teamMember> ().teamID;
if(player.customProperties["teamID"] != null)
{
if(player.customProperties["teamID"].ToString() == teamID.ToString())
{
print ("ally");
}
else
{
print ("enemy!");;
}
}
}
This code works and prints if the player that just spawned is an ally or an enemy. Now how would I go about assigning a color to that particular player? Basically I want to run this line of code on that specific player's gameobject.
(that player's gameobject).material.SetColor("_RimColor", new Color(0, 0, 1, 1)
There is no built-in way to access a GameObject per player. I will add a "TagObject" value to the PhotonPlayer class, so you could attach it then but it's still manual. Make your code remember the main object of each player. The prefab could have a script that registers itself somewhere in Awake or something similar.
When the properties change, you can go through the whole list of such game objects and update their color or just check which color/team changed and update a single one.
Back to top