Need Help With Teams
Options
xXGriMe
✭
Im getting inconsistent things happening with my script and could really use some help. It works but randomly when a client joins they some how become out of sync(just that client). It only happens when first joining the room to.
Example - Client 1 joins, client 2 joins and everything is working fine client 3 joins and its out of sync, client 4 joins and everything is working fine. Client 1,2,4 are all fine and working the way i want. Client 3 stays out of sync. Its seems completely random to because its not always client 3 that is out of sync sometimes client 3 will be fine but client 6 with be out of sync. Sometimes it will be more than 1 client at a time that is out of sync. I have tested this 5 or 6 times and i cant seem to get a constant result.
Some images to help better describe my issue.
Client 7
Client 3
---This is attached to a empty gameobject in the scene---
---This is attached to my instantiated player---
Example - Client 1 joins, client 2 joins and everything is working fine client 3 joins and its out of sync, client 4 joins and everything is working fine. Client 1,2,4 are all fine and working the way i want. Client 3 stays out of sync. Its seems completely random to because its not always client 3 that is out of sync sometimes client 3 will be fine but client 6 with be out of sync. Sometimes it will be more than 1 client at a time that is out of sync. I have tested this 5 or 6 times and i cant seem to get a constant result.
Some images to help better describe my issue.
Client 7
Client 3
---This is attached to a empty gameobject in the scene---
using UnityEngine; using System.Collections; using UnityEngine.UI; public class LogicGameLobby : Photon.MonoBehaviour { public Text AuthText; public Text roomName; public GameObject[] sofPlayers; public GameObject[] savagesPlayers; // Use this for initialization void Start () { roomName.text = PhotonNetwork.room.name; } // Update is called once per frame void Update () { AuthText.text = (PhotonNetwork.connectionStateDetailed.ToString()); } public void OnLevelWasLoaded() { PhotonNetwork.Instantiate ("_Prefabs/JoinedPlayer", transform.position, transform.rotation, 0, null); } }
---This is attached to my instantiated player---
using UnityEngine; using System.Collections; using UnityEngine.UI; using Hashtable = ExitGames.Client.Photon.Hashtable; public class LobbyPlayerViewer : Photon.MonoBehaviour { private int savagesAmount; private int sofAmount; private Text playerName; private GameObject scripts; //Holds the game object _Scripts private LogicGameLobby gameLobbyScript; //Holds the script for the game lobby so i can acess varibles inside said script public Hashtable PlayerCustomProperties = new Hashtable(); // Use this for initialization void Start () { //Find the _Scripts GameObject scripts = GameObject.FindGameObjectWithTag ("Scripts"); //Get the script component from the GameObject gameLobbyScript = scripts.GetComponent<LogicGameLobby> (); //Get the Text object for PlayerName playerName = gameObject.GetComponentInChildren <Text> (); //Set the players name playerName.text = PhotonNetwork.player.name + photonView.ownerId; SetPlayerProps (); CountTeamsPlayers (); CalculateTeamToStartOn (); SetOtherPlayersParent (); SetPlayerParent (); } public void Update() { } public void SetPlayerProps() { if (photonView.isMine == true) { PlayerCustomProperties["Team"] = "noTeam"; PhotonNetwork.player.SetCustomProperties (PlayerCustomProperties); } } public void CountTeamsPlayers() { foreach (PhotonPlayer player in PhotonNetwork.playerList) { if((string)player.customProperties["Team"] == "Sof" && photonView.isMine) { sofAmount++; } } foreach (PhotonPlayer player in PhotonNetwork.playerList) { if((string)player.customProperties["Team"] == "Savage" && photonView.isMine) { savagesAmount++; } } } public void CalculateTeamToStartOn() { if(sofAmount > savagesAmount) { PlayerCustomProperties["Team"] = "Savage"; PhotonNetwork.player.SetCustomProperties (PlayerCustomProperties); } if(sofAmount <= savagesAmount) { PlayerCustomProperties["Team"] = "Sof"; PhotonNetwork.player.SetCustomProperties (PlayerCustomProperties); } } public void SetOtherPlayersParent() { if (!photonView.isMine) { foreach (GameObject sofSlots in gameLobbyScript.sofPlayers) { if (sofSlots.transform.childCount == 0 && (string)PhotonNetwork.player.customProperties ["Team"] == "Sof") { transform.SetParent (sofSlots.gameObject.transform, false); break; } } foreach (GameObject savageSlots in gameLobbyScript.savagesPlayers) { if (savageSlots.transform.childCount == 0 && (string)PhotonNetwork.player.customProperties ["Team"] == "Savage") { transform.SetParent (savageSlots.gameObject.transform, false); break; } } } } public void SetPlayerParent() { if (photonView.isMine) { foreach (GameObject sofSlots in gameLobbyScript.sofPlayers) { if (sofSlots.transform.childCount == 0 && (string)PhotonNetwork.player.customProperties ["Team"] == "Sof") { transform.SetParent (sofSlots.gameObject.transform, false); break; } } foreach (GameObject savageSlots in gameLobbyScript.savagesPlayers) { if (savageSlots.transform.childCount == 0 && (string)PhotonNetwork.player.customProperties ["Team"] == "Savage") { transform.SetParent (savageSlots.gameObject.transform, false); break; } } } } public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { if(stream.isWriting) { // We own this player: send the others our data stream.SendNext(sofAmount); stream.SendNext(savagesAmount); } else { // Network player, receive data sofAmount = (int)stream.ReceiveNext(); savagesAmount = (int)stream.ReceiveNext(); } } }
0
Comments
-
Fixed now using code blocks. Sorry about that still getting familiar with the new design.0
-
The new forum is still a bit alien. We will work on the code blocks, too...
You can't set PhotonNetwork.player.customProperties ["Team"] directly.
You have to use SetCustomProperties(). Otherwise things won't get synchronized properly.0 -
I thought I was using SetCustomProperties. I'm not sure what you mean? Can you show me the correct syntax?0
-
Is something like this what you mean? I re did my code to use SetCustomProperties but im still getting the inconsistency i talked about in my first post.
PhotonNetwork.player.SetCustomProperties (PlayerCustomProperties = new Hashtable(){{"Team", "noTeam"}} );
0 -
Yes, I meant using SetCustomProperties() like you do now. I don't know why you get the inconsistency. There will still be some logic/timing issue.
You could take a look at the Demo "Pickup, Teams, Score" (scene: DemoPickup-Scene) and the script "PunTeams". It implements them with custom properties, too.
0