0
\$\begingroup\$

I have a problem wherein this happens:img

It's problematic because I'm unfamiliar how to set the same player value in the same transform over the network. Is there a way where if I get the list of transforms, it's in the same order in other clients?

My current setup is that each client creates his own My Player Prefab with a Player Info script that stores in the Photon's Player. Spawn Manager will be listening until both player prefabs have been created. From there, they'll do this code:

public void PlayerObjectReady(){
    playerObjectReady++;

    int totalPlayerCount = PhotonNetwork.PlayerList.Length;

    // check if we've readied all player objects in all clients
    if(playerObjectReady == totalPlayerCount){
        photonView.RPC("SpawnPlayer",RpcTarget.AllBufferedViaServer);
    }
}

[PunRPC]
void SpawnPlayer(){
    // update local list for all clients
    infos = FindObjectsOfType<PlayerInfo>(); // this line causes problems

    // update player references so that each character is owned by their player
    for (int i = 0; i < infos.Length; i++){
        infos[i].Player = PhotonNetwork.PlayerList[i];
        if(PhotonNetwork.IsMasterClient)
            infos[i].transform.position = spawnPoints[i].position;
    }
    TurnManager.Instance.AddPlayers(infos);
    
    // tell everyone i'm ready
    photonView.RPC("ImReady",RpcTarget.AllBufferedViaServer,PhotonNetwork.NickName);
}

The main problem here is that the player prefabs are not always in the correct order that I wanted them to be. Plus, their positions often clash together whenever Photon Transform view is enabled. And yes, the players here are right beside each other when they're actually spread across a small map.

screenshot

Let me know a possible solution because I intend to use Photon Transform view. For now, disabling Photon Transform view is my current band-aid solution which only works when spawning and not moving the players.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I got my own solution after testing for hours.

Essentially, when I create the My Player prefab which has its own PhotonView and I instantiate this prefab by the player who should own it at the start, I can access the Photon View's Owner.ActorNumber which can provide me with the ID needed to set my own player list of Transforms/GameObject/whatever script accordingly.

See my code below for reference:

[PunRPC]
void SpawnPlayer(){
    // update local list for all clients
    infos = FindObjectsOfType<PlayerInfo>();
    Array.Sort(infos, (PlayerInfo a,PlayerInfo b)=>a.photonView.Owner.ActorNumber.CompareTo(b.photonView.Owner.ActorNumber));

    // update player references so that each character is owned by their player
    for (int i = 0; i < infos.Length; i++){
        infos[i].Player = PhotonNetwork.PlayerList[i];
        if(PhotonNetwork.IsMasterClient)
            infos[i].transform.position = spawnPoints[i].position;
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Don't forget to click the checkmark in the top-left to mark this answer as "Accepted" if it solved your problem. \$\endgroup\$
    – DMGregory
    Sep 14, 2022 at 12:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .