Use room properties wrong my game.

Hello guys, I try to summarize my problem :).

First I have a GameManager prefab with a script to manage multiplayer part of the game, and here I made the code what is Instantiate my player. This prefab hasn't PhotonView component and I Instantiate my player like this.

Code (CSharp):
GameObject myPlayer = (GameObject)PhotonNetwork.Instantiate("aTestPrefab", spawnPoints[0].transform.position, Quaternion.identity, 0);
It's working, well and the game run with no error if I run the build client and the editor same time and test it and its fine. My problem starts there when I want to make a spawn system to not spawning two players on the same spawn.

Code (CSharp):
private void Start()
{
if (PhotonNetwork.isMasterClient)
{
UploadFreeSpawnPoints();
SpawnPlayer();

}
}
Code (CSharp):
public override void OnPhotonPlayerConnected(PhotonPlayer other)
{
if (!PhotonNetwork.isMasterClient)
{
Debug.Log("OnPhotonPlayerConnected isMasterClient " + PhotonNetwork.isMasterClient); // called before OnPhotonPlayerDisconnected
SpawnPlayer();
}
}
Code (CSharp):
private void SpawnPlayer()
{
///Increase connected player numbers
#pragma warning disable CS0618 // Type or member is obsolete
int connectedPlayers = (int)PhotonNetwork.room.customProperties["ConnectedPlayers"];
#pragma warning restore CS0618 // Type or member is obsolete
Debug.Log(connectedPlayers);
connectedPlayers++;
ExitGames.Client.Photon.Hashtable hashtable = new ExitGames.Client.Photon.Hashtable
{
{ "ConnectedPlayers", connectedPlayers }
};
PhotonNetwork.room.SetCustomProperties(hashtable);
// we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate.
SpawnPoint random = ReservePlayerSpawnPoint();
if (random == null)
Debug.LogError("Spawn point is nullPointer(GameManager)");
else
Debug.Log(random);
GameObject myPlayer = (GameObject)PhotonNetwork.Instantiate("aTestPrefab", random.transform.position, Quaternion.identity, 0);
}

private void UploadFreeSpawnPoints()
{
for (int i = 0; i < spawnPoints.Length; i++)
{
freeSpawn[i] = Int16.Parse(spawnPoints[i].gameObject.name.ToString());
}
ExitGames.Client.Photon.Hashtable freeSpawnHash = new ExitGames.Client.Photon.Hashtable
{
{ "FreeSpawnPoints", freeSpawn }
};
PhotonNetwork.room.SetCustomProperties(freeSpawnHash);
}
private SpawnPoint ReservePlayerSpawnPoint()
{
#pragma warning disable CS0618 // Type or member is obsolete
freeSpawn = (int[])PhotonNetwork.room.customProperties["FreeSpawnPoints"];
#pragma warning restore CS0618 // Type or member is obsolete
Debug.Log(freeSpawn);
int spawnPoint = freeSpawn[UnityEngine.Random.Range(0, freeSpawn.Length)];
SpawnPoint returnPoint = null;
foreach (SpawnPoint spawn in spawnPoints)
{
if (spawn.gameObject.name == spawnPoint.ToString())
returnPoint = spawn;
}
return returnPoint;
}
Here is the code, if I not use the Properties part I don't get an error, if I use the game is running but sync all objects and its full bugged. The main question is that, how can the properties effect the game :(.

Comments

  • Hi @Beast,

    I'm not sure if this is a proper solution, since you are trying to reinvent the wheel particularly. First thing is, that you are storing the number of players in the Custom Room Properties. Of course you can do this, but you don't have to, instead you can call PhotonNetwork.room.PlayerCount to get the amount of players (when the client is in a room). This value gets automatically updated whenever a client joins or leaves the room.

    Another thing is the selection of the Spawn Points. There is a PlayerRoomIndexing script included in the PUN package. You can find it in \Assets\Photon Unity Networking\UtilityScripts\PhotonPlayer\. When you attach this script to an object in the scene (for example to your GameManager), you can use the Player Extensions and use int roomIndex = PhotonNetwork.player.GetRoomIndex(); to get the (unique) Index of the local player. This value can be mapped to a certain Spawn Point from your list.
  • Beast
    Beast
    edited August 2018
    Thanks for your answer @Christian_Simon :) !