How to get team code

Hello all, I'm trying to get the team code, which I'm going to use for spawning, I've come up with this

    public int GetTeamCode(byte code)
    {
        PhotonTeam team;
        if(PhotonTeamsManager.Instance.TryGetTeamByCode(code, out team))
        {
            return team.Code;
        }
        return 0;
    }

But I'm unsure how I can grab the current players code with this, thanks in advance for all help!

Answers

  • The local player's code?

    PhotonNetwork.LocalPlayer is extended with GetTeam()

  • I ended up using this:

    team = PhotonNetwork.LocalPlayer.GetPhotonTeam();
    

    then used team.Code to spawn the players, but I'm having some problems.

    Currently I'm spawning players based on their Actor Numbers, and it works fine, spawns all players, but when I change it to this

                team = PhotonNetwork.LocalPlayer.GetPhotonTeam();
                PhotonNetwork.Instantiate("Player Shooting", spawnPoints[team.Code].position, Quaternion.identity);
    

    Only the master client gets spawned. And I get a error, the second line returns null on the second player, why is this? When a player added to a team, I debug it and it works fine, and I can also see both players on the PhotonTeamsManager.cs.


  • Please don't bump. If you still need help, you might want to add new info, which you figured out since last time you posted. Thanks.

    That aside: It's not guaranteed that the local player always has a team, so it may return null.

    Your code can check if team is null and make sure the team gets assigned first. Then instantiate.

  • My bad, and so far I have not found anything.

    And I guess a miracle just happened, I just added a line to check if the player is null

                team = PhotonNetwork.LocalPlayer.GetPhotonTeam();
                if(team == null)
                {
                    Debug.Log("Player not assigned to a team");
                }
                Debug.Log(team.Code);
                PhotonNetwork.Instantiate("Player Shooting", spawnPoints[PhotonNetwork.LocalPlayer.ActorNumber].position, Quaternion.identity);
    

    and it started working. I'll just remove that line and see if it stills works.

  • Johan_Liebert96
    edited October 2022

    Umm its working without the null checks, idk why (not complaining) but I guess a restart did it!!

    EDIT - Wait, never mind, I never changed the PhotonNetwork.LocalPlayer.ActorNumber to team.Code 😅

    So your guess was correct, the other players are not assigned to a team, why though? I can see them in the PhotonTeamsManager, and it also debugs when you have joined a team?

    So theres no reason for it to return null

  • Just elaborating on my previous post, if this works, theres no reason why it's returning null

                    if (player.GetPhotonTeam() == null)
                    {
                        player.JoinTeam(team.Code);
                        Debug.Log(team.Code); // Added to check if the player actually joins a team
                    }
    
  • Tobias
    Tobias admin
    edited October 2022

    You have to assign to team. Then check if it's null.

    Right now, I don't know if / where you are stuck, so my reply is not really helping. Sorry. Please recap latest developments.

  • Ok, so I did what you said, and checked if the player is null using this code

                team = PhotonNetwork.LocalPlayer.GetPhotonTeam();
                if(team == null)
                {
                    Debug.LogError("Player has not been assigned to a team");
                }
    

    And it returns true for every player except the master client, but I'm confused why it's returning null, because the player is correctly assigned to a team based on this


    and this

                    if (player.GetPhotonTeam() == null)
                    {
                        player.JoinTeam(team.Code);
                        Debug.Log(team.Code);
                    }
    

    And the code above correctly debugs the correct team code for each player.

    So my question is why is it returning null?

  • it returns true for every player

    You mean you run the code on each client and locally it returns a team? Ok.

    So my question is why is it returning null?

    When and where? And how do you assign teams?

    When you assign a team, this is synchronized. This takes a moment (it's only instant locally) before others have the info. And does every client just assign it for it's own player?

  • When and where? And how do you assign teams?

    So i'll clue you in how my game works, once my player logs in, it'll be loaded to a lobby scene, after clicked play it'll join a room or create one, and a matchmaking screen is activated which basically displays the players in the room, and the player is automatically assigned to a team, I have properties to join a team for each game mode, which is the max players per team, how many teams etc.

                if (teamCount < gameMode.TeamSize) // team count being the number of players in the team, game mode.Teamsize is the max players per team
                {
                    Debug.Log($"Assigned {player.NickName} to {team.Name}");
                    if (player.GetPhotonTeam() == null)
                    {
                        player.JoinTeam(team.Code);
                        Debug.Log(team.Code);
                    }
                    else if (player.GetPhotonTeam().Code != team.Code)
                    {
                        player.SwitchTeam(team.Code);
                        Debug.Log(team.Code);
                    }
                    break;
                }
    

    And this works, I'm just testing with 2 players, first one joins team 1, and it is debugged, when the second player clicks play, it joins team 2 and is debugged correctly. This is correctly shown on the PhotonTeamsManager

    Now when the room is full, the selected game mode scene is loaded, the problem I'm having is instantiating players with their team code, im currently using this

                team = PhotonNetwork.LocalPlayer.GetPhotonTeam();
                if(team == null)
                {
                    Debug.LogError("Player has not been assigned to a team");
                    return;
                }
                Debug.Log(team.Code);
                PhotonNetwork.Instantiate("Player Shooting", spawnPoints[team.Code].position, Quaternion.identity);
    

    The problem is only the masterClient is being instantiated to its correct spawn position, all other players arent and this

    Debug.LogError("Player has not been assigned to a team");
    

    is being debugged, if I get rid of that, the photonNetwork.Instantiate line is returning null for the players except the MasterClient, How is the player not assigned to a team when it clearly was before and it's being shown as well.

  • So both players are aware of each other's team before they move on and somehow .. one (or both) players can't access the own team code anymore?

    I am properly lost, sorry. I have no idea how you could get rid of the team code. Maybe you have to put a breakpoint before this code and debug it. You want to check if the team values are available or not. If they are, why GetPhotonTeam still fails.

  • Well yes, as I showed you in the screenshot, you can see both players in the photonTeamsManager, this is before the game scene has loaded/started, you can see a player in team 1 and 2. Now, when I load the gamescene, I can still see both players on the PhotonTeamsManager, but when trying to get team code it fails for all players except the master client, the master client is loaded and spawned at the correct spawn point, according to its team code. I really can't explain it much clearer

  • After a couple of weeks, I created my own methods for joining a team and setting the custom properties for the player and it works just great!!

  • Glad this worked out for you.

    Can you somehow let us know why the Teams Manager didn't work for you?

  • So it actually never working for me, it never recognised any other players team apart from the master clients team. So what I did is actually implemented a player method which sets the team code and stores it in CustomProperties. And I can easily access it in another script. Still not sure why Teams Manager doesn't work, maybe broken for all other players? But yeah, creating your own method for the Player which sets the custom properties for team code, and can easily be accessed using another method which returns the custom properties

  • Thanks for the update. When time permits, I'll take another look at the Teams Manager.