Photon Instantiate Master vs. Local
Hi,
I have a question regarding Photon when instantiating players. The game is simple and only has 2 players, and when spawned it will be the master & local client
In the below code in the method SpawnAllTanks() each client is responsible for spawning their prefab.
My issue is that the m_Tanks array where I set the m_Instance will only ever be populated by the master as this code runs independently on both the master and the client.
Later on I need this array to determine the winner but as it stands I cannot use it because the array is not serialized over the network.
private void Start()
{
view = GetComponent<PhotonView>();
allPlayers = PhotonNetwork.PlayerList;
foreach (Player p in allPlayers)
{
if (p != PhotonNetwork.LocalPlayer)
{
myNumberInRoom = 0;
}
else
{
myNumberInRoom = 1;
}
}
// Create the delays so they only have to be made once.
m_StartWait = new WaitForSeconds (m_StartDelay);
m_EndWait = new WaitForSeconds (m_EndDelay);
SpawnAllTanks();
// Once the tanks have been created and the camera is using them as targets, start the game.
if (PhotonNetwork.IsMasterClient)
{
StartCoroutine(GameLoop());
}
}
private void Update()
{
m_Players = GameObject.FindGameObjectsWithTag("Tank");
}
private void SpawnAllTanks()
{
// For all the tanks...
// ... create them, set their player number and references needed for control.
if (PhotonNetwork.LocalPlayer.CustomProperties.ContainsKey("playerAvatarPrefab"))
{
GameObject shipToSpawn = m_TankPrefab[(int)PhotonNetwork.LocalPlayer.CustomProperties["playerAvatarPrefab"]];
m_Tanks[myNumberInRoom].m_Instance =
PhotonNetwork.Instantiate(shipToSpawn.name, m_Tanks[myNumberInRoom].m_SpawnPoint.position, m_Tanks[myNumberInRoom].m_SpawnPoint.rotation) as GameObject;
m_Tanks[myNumberInRoom].m_PlayerNumber = myNumberInRoom + 1;
m_Tanks[myNumberInRoom].Setup();
}
else
{
GameObject shipToSpawn = m_TankPrefab[0];
m_Tanks[myNumberInRoom].m_Instance =
PhotonNetwork.Instantiate(shipToSpawn.name, m_Tanks[myNumberInRoom].m_SpawnPoint.position, m_Tanks[myNumberInRoom].m_SpawnPoint.rotation) as GameObject;
m_Tanks[myNumberInRoom].m_PlayerNumber = myNumberInRoom + 1;
m_Tanks[myNumberInRoom].Setup();
}
}
Your answer
Follow this Question
Related Questions
Photon ccu question 0 Answers
How can we make 1v1 game with photon network 1 Answer
PhotonMono spawns instead of player. 0 Answers
in photon multiplayer camera, each player looks like using one camera 0 Answers
Photon RPC Only Working Halfway 1 Answer