Keeping user/team dictionary in sync with RPC
Options
I am trying to keep a dictionary of player username's and teamIDs in sync over the network.
However, I am seeing results with AllBuffered that are puzzling me. I'm sure it is just a lapse in understanding somewhere.
When the second player joins the game, the following prints out 0. I would have expected it to print out 1 since the first player's username/teamid pair should already exist.
However, during the Update(), the count is correctly 2. :?
Full code below
And the AssignTeam function is called within the Start() of my GameLobbyNetworkManager class.
Could anyone clear up my confusion around this?
Has the player just not gotten the AllBuffered messages yet? Is this a timing thing?
Or do I have some other misunderstanding? Is there a cleaner way to do this?
Is there a callback for when a client has received all of the AllBuffered messages?
Or should I be using player properties to handle this?
Edit:
Also, I should mention that the current scene is being loaded using PhotonNetwork.LoadLevel
Thanks!
However, I am seeing results with AllBuffered that are puzzling me. I'm sure it is just a lapse in understanding somewhere.
When the second player joins the game, the following prints out 0. I would have expected it to print out 1 since the first player's username/teamid pair should already exist.
Debug.Log ("playerTeamIDs.Count=" + playerTeamIDs.Count);
However, during the Update(), the count is correctly 2. :?
void Update() { Debug.Log (playerTeamIDs.Count); }
Full code below
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; public class TeamManager : MonoBehaviour { const int MAX_PLAYERS_PER_TEAM = 1; // key = string (username) // value = int (team id) // used to lookup team id from username Dictionary<string, int> playerTeamIDs; PhotonView photonView; void Awake() { // don't destroy the team manager when we load the game scene DontDestroyOnLoad(transform.gameObject); photonView = GetComponent<PhotonView>(); playerTeamIDs = new Dictionary<string, int>(); } public int GetTeamID(string username) { if (playerTeamIDs.ContainsKey(username)) { return playerTeamIDs[username]; } Debug.Log ("playerTeamIDs doesn't have player with username: " + username); return 0; // no team } [RPC] void SetTeamID_RPC(string username, int teamID) { Debug.Log ("Adding " + username + " to team " + teamID); playerTeamIDs.Add (username, teamID); } int GetTeamCount(int teamID) { // use linq to get the number of player's in the team Debug.Log ("playerTeamIDs.Count=" + playerTeamIDs.Count); return playerTeamIDs.Count(id => id.Value == teamID); } public void AssignTeam(string username) { // team1 is full if (GetTeamCount (1) == MAX_PLAYERS_PER_TEAM) { // assign player to team2 photonView.RPC("SetTeamID_RPC", PhotonTargets.AllBuffered, username, 2); } else if (GetTeamCount(2) == MAX_PLAYERS_PER_TEAM) // team2 is full { // assign player to team1 photonView.RPC("SetTeamID_RPC", PhotonTargets.AllBuffered, username, 1); } else // neither team is full yet { // assign the player to a random team int teamID = Random.Range (1, 3); photonView.RPC("SetTeamID_RPC", PhotonTargets.AllBuffered, username, teamID); } } void Update() { Debug.Log (playerTeamIDs.Count); } }
And the AssignTeam function is called within the Start() of my GameLobbyNetworkManager class.
using UnityEngine; using System.Collections; using UnityEngine.UI; public class GameLobbyNetworkManager : MonoBehaviour { TeamManager teamManager; void Awake() { teamManager = GameObject.FindGameObjectWithTag("TeamManager").GetComponent<TeamManager>(); } void Start() { // Assign to a team teamManager.AssignTeam(PhotonNetwork.player.name); } }
Could anyone clear up my confusion around this?
Has the player just not gotten the AllBuffered messages yet? Is this a timing thing?
Or do I have some other misunderstanding? Is there a cleaner way to do this?
Is there a callback for when a client has received all of the AllBuffered messages?
Or should I be using player properties to handle this?
Edit:
Also, I should mention that the current scene is being loaded using PhotonNetwork.LoadLevel
void OnJoinedRoom() { Debug.Log ("OnJoinedRoom"); PhotonNetwork.LoadLevel ("gameLobby"); }
Thanks!
0
Comments
-
HI,
Check team implementation in Assets\Photon Unity Networking\UtilityScripts\PunTeams.cs based on player properties.0 -
Thank you vadim!0
-
Thank you vadim!0