Check and Assign Team

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.

Check and Assign Team

k3921
2018-08-12 05:58:08

I want to make a like a clash royale like room joining, which is before actually loading the battle scene, every player join in the room and assign into the different team(like 2VS2 mode), and even the masterclient of the room leaved, the room will have a new, lowest ping client to be replaced become a new masterclient for keeping the room alive. After everything(join room, room full(reach maximum player), team assign with all player within the same room(punteam assign),masterclient load battle scene(autosync=true),photonnetwork.loadlevel)is done, player will move to battle scene, with this according which team player in, player will be instantiate in different position.
This is my thought, and can somebody help me figure this code to operate this system?
Thank

Comments

k3921
2018-08-12 06:15:20

Now currently I have some codes here
<code class="{
private TextMeshProUGUI m_text;
[SerializeField]
private Transform[] spawnPointsBlue;
[SerializeField]
private Transform[] spawnPointsRed;

private float PlayerInRoomCount;

private RoomOptions options;

// Use this for initialization  
void Start () {  
    PhotonNetwork.ConnectUsingSettings("test");  
    PhotonNetwork.automaticallySyncScene = true;  
    options = new RoomOptions();  
    options.PlayerTtl = 60000; //60s  
    options.EmptyRoomTtl = 60000; //60s  
    options.MaxPlayers = 8;  
}  
public virtual void OnConnectedToMaster()  
{  
    //PhotonNetwork.JoinOrCreateRoom("new", null, null);  
    Debug.Log("in Master");  
}  
public void JoinRoom()  
{  
    PhotonNetwork.JoinOrCreateRoom("new", null, null);  
}  
public virtual void OnJoinedRoom()  
{  
    int PlayersCount = PhotonNetwork.room.PlayerCount;  
    if (PhotonNetwork.isMasterClient)  
    {  
        if (PlayersCount == 2)  
        {  
            options.IsOpen = false;  
            options.IsVisible = false;  
            PhotonNetwork.LoadLevel(3);  
        }  
    }  
    GameObject Player = Resources.Load<GameObject>("knight");  
    if (PhotonNetwork.player.GetTeam() == PunTeams.Team.blue)  
    {  
        PhotonNetwork.Instantiate(Player.name, Vector3.zero, Quaternion.identity, 0);  
    }  
    else if(PhotonNetwork.player.GetTeam() == PunTeams.Team.blue)  
    {  
        PhotonNetwork.Instantiate(Player.name, Vector3.zero, Quaternion.identity, 0);  
    }  
}  
public virtual void OnPhotonJoinRoomFailed(object[] codeAndMsg)  
{  
    Debug.LogWarning("There do not have any room/Cannot join room");  
    PhotonNetwork.LeaveRoom();  
}  
public virtual void OnPhotonCreateRoomFailed(object[] codeAndMsg)  
{  
    Debug.LogWarning("Create Room Fail");  
    PhotonNetwork.LeaveRoom();  
}  
// Update is called once per frame  
void Update () {  
    m_text = GameObject.Find("Detail").GetComponent<TextMeshProUGUI>();  
    m_text.text = PhotonNetwork.connectionStateDetailed.ToString();  
}

//Multiplayer Team Logic  
public void TeamJoinLogic()  
{  
    PlayerInRoomCount = Mathf.Round(PhotonNetwork.room.PlayerCount / 2.0f);

    for (int i = 0; i < PhotonNetwork.room.PlayerCount; i++)  
    {  
        var team = PunTeams.Team.blue;  
        if (i >= PlayerInRoomCount)  
        {  
            team = PunTeams.Team.red;  
        }  
        PhotonNetwork.playerList[i].SetTeam(team);  
    }  
}  

}">
I know there is not match the goal, I still not know how to match the goal I want, first, is that the
mean this client player?Second, how to know which team.count this player currently are(eg:blue team 3rd player)(to instantiate player in specific position of spawnpointblue or red)?Also, is that need to reset this punteam if there have any player leave the room before start battle?If so, how to achieve that?

If the code have have any error please tell me how to solve it, thank you all!

k3921
2018-08-12 06:16:54

the code is not looking good
here is the code
{
private TextMeshProUGUI m_text;
[SerializeField]
private Transform[] spawnPointsBlue;
[SerializeField]
private Transform[] spawnPointsRed;

private float PlayerInRoomCount;

private RoomOptions options;

// Use this for initialization  
void Start () {  
    PhotonNetwork.ConnectUsingSettings("test");  
    PhotonNetwork.automaticallySyncScene = true;  
    options = new RoomOptions();  
    options.PlayerTtl = 60000; //60s  
    options.EmptyRoomTtl = 60000; //60s  
    options.MaxPlayers = 8;  
}  
public virtual void OnConnectedToMaster()  
{  
    //PhotonNetwork.JoinOrCreateRoom("new", null, null);  
    Debug.Log("in Master");  
}  
public void JoinRoom()  
{  
    PhotonNetwork.JoinOrCreateRoom("new", null, null);  
}  
public virtual void OnJoinedRoom()  
{  
    int PlayersCount = PhotonNetwork.room.PlayerCount;  
    if (PhotonNetwork.isMasterClient)  
    {  
        if (PlayersCount == 2)  
        {  
            options.IsOpen = false;  
            options.IsVisible = false;  
            PhotonNetwork.LoadLevel(3);  
        }  
    }  
    GameObject Player = Resources.Load<GameObject>("knight");  
    if (PhotonNetwork.player.GetTeam() == PunTeams.Team.blue)  
    {  
        PhotonNetwork.Instantiate(Player.name, Vector3.zero, Quaternion.identity, 0);  
    }  
    else if(PhotonNetwork.player.GetTeam() == PunTeams.Team.blue)  
    {  
        PhotonNetwork.Instantiate(Player.name, Vector3.zero, Quaternion.identity, 0);  
    }  
}  
public virtual void OnPhotonJoinRoomFailed(object[] codeAndMsg)  
{  
    Debug.LogWarning("There do not have any room/Cannot join room");  
    PhotonNetwork.LeaveRoom();  
}  
public virtual void OnPhotonCreateRoomFailed(object[] codeAndMsg)  
{  
    Debug.LogWarning("Create Room Fail");  
    PhotonNetwork.LeaveRoom();  
}  
// Update is called once per frame  
void Update () {  
    m_text = GameObject.Find("Detail").GetComponent<TextMeshProUGUI>();  
    m_text.text = PhotonNetwork.connectionStateDetailed.ToString();  
}

//Multiplayer Team Logic  
public void TeamJoinLogic()  
{  
    PlayerInRoomCount = Mathf.Round(PhotonNetwork.room.PlayerCount / 2.0f);

    for (int i = 0; i < PhotonNetwork.room.PlayerCount; i++)  
    {  
        var team = PunTeams.Team.blue;  
        if (i >= PlayerInRoomCount)  
        {  
            team = PunTeams.Team.red;  
        }  
        PhotonNetwork.playerList[i].SetTeam(team);  
    }  
}  

}

k3921
2018-08-12 06:18:23

Please help me figure all function I need,Thank you again

[Deleted User]
2018-08-14 09:34:04

Hi @k3921,

if you want to use PunTeams you can use PhotonNetwork.player.SetTeam(PunTeams.Team.blue);. In this example the local player will join the blue team, you can do this accordingly for the red team. To get the number of players who have joined a certain team, you can use int blueTeamMembers = PunTeams.PlayersPerTeam[PunTeams.Team.blue].Count; for example. In this case you get the number of players who have joined the blue team, you can do this for the red team accordingly. Please note, that you have to attach the PunTeams component to a GameObject in the scene, it won't work otherwise.

If you want to start the game when a certain number of players is reached, you can use the void OnPhotonPlayerConnected(PhotonPlayer newPlayer) callback. It gets called whenever a new client has joined the room. As a MasterClient you can check inside this callback, if the certain number of players is reached in order to start the game. Another way here would be, to create a simple game lobby, where each client can toggle if he is ready. The MasterClient can observe the clients' ready state and start the game according to this. This could be done by either using the Custom Player Properties or by raising certain events. If you want to follow this approach, you can search on the forum to find ways to do this (in this thread for example).

k3921
2018-08-17 10:12:15

@Christian_Simon
thank you for reply, now I am using punteam script and which work well, I want to know is that have any method to get which number of team count player at,eg: red team 3rd team member.

About sync loading, I have using the method you suggest,

int Max = options.MaxPlayers;
if(PhotonNetwork.playerList.Length == Max && PhotonNetwork.isMasterClient)
{
PhotonNetwork.LoadLevel("Scene3");
}

Which work fine. But I cannot see Player who load the scene previously(other client not masterclient).And will cause any other problem which will not have when I load the scene separately.

Received OnSerialization for view ID 1001.We have no such PhotonView! Ignored this if you're leaving a room.State: Joined

NullReferenceException....

And Main problem is cannot see the masterclient gameobject in other client window.
Which do not have any problem when I load the scene separately.
Why would this happen?

Also, I want to try to use RPC, but RPC require photonview which I link in playercharacter, I can not access it before loading the scene.

I want to sync player to same scene without cause any other problem, please let me know why I can have zero error when I load scene separate but have a lot of error in sync loading, thank you

k3921
2018-08-17 11:03:14

@Christian_Simon
I found another problem is I cannot use PhotonNetwork.Instantiate player character, in any other callback except Awake(). Is this mean to do that? Because I want to know if my put the spawn logic in start callback check the situation whether be better.

My SpawnLogic
private void SpawnLogic()
{
if(PhotonNetwork.player.GetTeam() == PunTeams.Team.red)
{
GameObject Player = Resources.Load("knight")
PhotonNetwork.Instantiate(Player.name, spawnPointsRed[0].position, spawnPointsRed[0].rotation, 0);
}
else if(PhotonNetwork.player.GetTeam() == PunTeams.Team.blue)
{
GameObject Player = Resources.Load("knight")
PhotonNetwork.Instantiate(Player.name, spawnPointsBlue[0].position, spawnPointsBlue[0].rotation, 0);
}
That work well in Awake but cannot call in start

[Deleted User]
2018-08-21 08:48:31

Which work fine. But I cannot see Player who load the scene previously(other client not masterclient).And will cause any other problem which will not have when I load the scene separately.

What do you mean by loading the scene separately? If you use PhotonNetwork.LoadLevel, the client will load the given level immediately. If you are calling this on the MasterClient and you have PhotonNetwork.automaticallySyncScene enabled, each client in the room will load the level as well. If a client joins later, this client might instantiate objects before loading the level. This results in removed objects when loading the level afterwards and leads to those error messages:

Received OnSerialization for view ID 1001.We have no such PhotonView! Ignored this if you're leaving a room.State: Joined

and this behaviour:

And Main problem is cannot see the masterclient gameobject in other client window.

You can try to workaround this behaviour by marking the player objects as DontDestroyOnLoad. This way they don't get removed when loading another level. You would have to handle removing those objects on your own if necessary.

I found another problem is I cannot use PhotonNetwork.Instantiate player character, in any other callback except Awake().

Please check if the Start function gets called at all. You can easily do this by adding a Debug.Log call to it. PhotonNetwork.Instantiate is not limited to only work in Unity's Awake function. If it is not called, this might be a problem with scene loading as well. To check this, you can add Unity's OnDestroy function to the class and add a Debug.Log call here as well.

k3921
2018-08-22 11:37:11

@Christian_Simon
Thank you , everything work fine if I put it on start callback, dontdestroyonload is not work in awake.

now I face another problem, after I assign the team for player, how can I get the gameobject of the player?
I have assign the specific layer to every player, is that need to use rpc or need to get player gameobject by layer?if so, how can the code work? Thank you

[Deleted User]
2018-08-23 08:21:47

There is a TagObject field on the PhotonPlayer which can be used for that. To make use of that you can use the OnPhotonInstantiate callback. In code this might look like the following:

public void OnPhotonInstantiate(PhotonMessageInfo info)  
{  
    photonView = GetComponent();  
    photonView.owner.TagObject = gameObject;  
}

If you now want to have a GameObject which belongs to a certain player, you can get a reference to that player and access his TagObject.

Back to top