- Home /
PUN2 Chess game instantiation problem
Hi, I made a chess game, it works perfectly in single player and wanted to add a multiplayer mode, so I found PUN2. Originally all chess pieces are instantiated on start, When I tried not to change it, so I just added a photon view and photon transform view classic components and built the game, when I move a piece it shows on one side only. So I tried PhotonNetwork.Instantiate and it worked on both sides well, but it instantiated every piece twice, so there are two white teams and two black teams.. Then I tried PhotonNetwork.InstantiateSceneObject and it didn't instantiate every team twice, and it transmitted the movement on both sides, but I can move the pieces on only one side, on the other client it just doesn't work. Also when using InstantiateSceneObject all pieces have "Controlled Locally" turned off in the photon view component.. while using PhotonNetwork.Instantiate all pieces have it turned on and on the doubled pieces it is turned off. Problem: How can I use PhotonNetowrk.Instantiate without doubling teams. or how can I use PhotonNetwork.InstantiateSceneObject but control the ownership of teams, so that one client owns white team and other owns black team, or even both I think I can work around it if both can control both teams.. Thanks :D.
Can you perhaps share some code snippets of your instantiation? Where and how do you call it?
Answer by Captain_Pineapple · Jul 21, 2019 at 06:18 PM
Hey,
please read the documentation on PhotonNetwork.Instantiate carefully...
GameObject goM = PhotonNetwork.Instantiate(chessmanPrefabs[index].name, GetTileCenter(x, y), Quaternion.identity) as GameObject;
when you call your instantiation you apparently do this for every player for every chesspiece. When you call PhotonNetwork.Instantiate("Rock") at player A, it will create a chesspiece of type "Rock" for player A, giving him ownership, but also instantiate the same object for ALL other players in the room or joining later. So a solution might be that the masterclient always is white and only spawns the white chesspieces while the "non master client" spawns the black ones...
Thanks for replying, I changed the start function to be like this:
private void Start() {
Instance = this;
pv = GetComponent<PhotonView>();
if (PhotonNetwork.Is$$anonymous$$asterClient)
SpawnWhiteChessmans();
pv.RPC("SpawnBlackChessmans", RpcTarget.Others);
}
And now the white $$anonymous$$m is spawned and only master has its ownership. now how do I make the other client spawn the black $$anonymous$$m ? i tried RPC and it didn't work.. Am I missing something ?
why not:
if (PhotonNetwork.Is$$anonymous$$asterClient)
SpawnWhiteChessmans();
else
SpawnBlackChessmans();
This assumes though that there will never be more than 2 people in one room!
Yeah I tried it before, and somehow only the black king is spawned and the other client cant control it and neither does the master.. White $$anonymous$$m is working perfectly tho.. only master can control it and the client cant..
Answer by HarD-izzeR · Jul 21, 2019 at 02:41 PM
Any ideas ??
As i wrote in my comment above... please share some code snippets. Where do you call the instantiation? did you make sure it really is only called once? Does everyone only instantiate his figures or all of them? please provide some more information on what you do in your code.
Sure here is the code: https://pastebin.com/Anp2jtD8 As mentioned above.. the instantiation is called in start. (SpawnAllChessmans(); at line 28) And originally it is instantiated only once so I'am sure the whole chess code is working perfectly. its just the multiplayer part.