2 Payer random matchmaking

Hello everyone, I am developing a game using Photonnetwork. I want to do this, 2 players enter the room when the game begins. Yes the game starts but the player who created the room cannot spawn.Other player can spawn.

Only one player participates in the game.

I'm spawning the user in the "updateOtherPlayerStatus()" method, where am I making mistakes?

Thanks already for your help.

My code;
 public void Connect()
    {
        car[CarSelected].carObj.GetComponent<RCC_PhotonNetwork>().enabled = true;
        PhotonNetwork.automaticallySyncScene = true;

        PhotonNetwork.PhotonServerSettings.HostType = ServerSettings.HostingOption.BestRegion;
        PhotonNetwork.ConnectToBestCloudServer(appVersion);
    }

    public override void OnFailedToConnectToPhoton(DisconnectCause cause)
    {
        if (connectionFailedEvent != null)
            connectionFailedEvent();
    }

    private IEnumerator requestJoinRandomRoom()
    {
        while (!PhotonNetwork.connectedAndReady)
        {
            yield return new WaitForEndOfFrame();
        };
        PhotonNetwork.JoinRandomRoom();
    }

    public void StartMatching()
    {
        StartCoroutine(requestJoinRandomRoom());
    }

    private void updateOtherPlayerStatus()
    {
        if (PhotonNetwork.room.PlayerCount == 2)
        {
            //messageLabel.text = @"Rakip Bulundu";
            //statusLabel.text = @"rakip: " + PhotonNetwork.otherPlayers[0].NickName;
            //cancelButton.interactable = false;
            //SpawnPlayer();
            PhotonNetwork.LoadLevel(1);

            if (!PhotonNetwork.isMasterClient)
                return;

            this.photonView.RPC("AddPlayer",PhotonTargets.All);
        }
    }
    /// <summary>
    /// Called when something causes the connection to fail (after it was established).
    /// See the official Photon docs for more details.
    /// </summary>
    public override void OnConnectionFail(DisconnectCause cause)
    {
        if (connectionFailedEvent != null)
            connectionFailedEvent();
    }

    public override void OnConnectedToMaster()
    {
        //set my own name and try joining a game
        PhotonNetwork.playerName = "Player"+ UnityEngine.Random.Range(1,9999);
        PhotonNetwork.JoinLobby();
    }

    public override void OnPhotonRandomJoinFailed(object[] codeAndMsg)
    {
        Debug.Log("Photon did not find any matches on the Master Client we are connected to. Creating our own room... (ignore the warning above).");

        PhotonNetwork.CreateRoom(null, new RoomOptions() { MaxPlayers = 2}, null);
    }

    public override void OnPhotonCreateRoomFailed(object[] codeAndMsg)
    {
        if (connectionFailedEvent != null)
            connectionFailedEvent();
    }

    public override void OnCreatedRoom()
    {
        Debug.Log("OnCreatedRoom");

        Hashtable roomProps = new Hashtable();
        roomProps.Add(RoomExtensions.size, new int[RoomExtensions.initialArrayLength]);
        roomProps.Add(RoomExtensions.score, new int[RoomExtensions.initialArrayLength]);
        PhotonNetwork.room.SetCustomProperties(roomProps);

    }

    public override void OnJoinedLobby()
    {
        Debug.Log("OnJoinedLobby");
        //statusLabel.text = @"Sen: " + PhotonNetwork.playerName;
        StartMatching();
    }

    public override void OnJoinedRoom()
    {
        PhotonPlayer player = GetComponent<PhotonView>().owner;
        Debug.Log("Joined player(id: { player.ID }) in this room.");
        updateOtherPlayerStatus();
    }

    //IEnumerator WaitForSceneChange()
    //{
    //    while (SceneManager.GetActiveScene().buildIndex != onlineSceneIndex)
    //    {
    //        yield return null;
    //    }

    //    //we connected ourselves
    //    OnPhotonPlayerConnected(PhotonNetwork.player);
    //}
    public override void OnPhotonPlayerConnected(PhotonPlayer player)
    {
        updateOtherPlayerStatus();
    }


    //received from the master client, for this player, after successfully joining a game
    [PunRPC]
    void AddPlayer()
    {
        RCC_CarControllerV3 testArac;

        GameObject[] point = GameObject.FindGameObjectsWithTag("spawnpoint");
        int spawPointindex = UnityEngine.Random.Range(0, point.Length);

        testArac = PhotonNetwork.Instantiate(car[CarSelected].name, point[spawPointindex].transform.position, point[spawPointindex].transform.rotation, 0).GetComponent<RCC_CarControllerV3>();

        RCC.RegisterPlayerVehicle(testArac);
        RCC.SetControl(testArac, true);

        if (RCC_SceneManager.Instance.activePlayerCamera)
            RCC_SceneManager.Instance.activePlayerCamera.SetTarget(testArac.gameObject);

        Gunes.instance.playerNameText = PhotonNetwork.playerName;
    }
    public override void OnPhotonPlayerDisconnected(PhotonPlayer player)
    {
        //only let the master client handle this connection
        if (!PhotonNetwork.isMasterClient)
            return;

        Gunes.instance.target = null;
    }

    public override void OnDisconnectedFromPhoton()
    {
        if (SceneManager.GetActiveScene().buildIndex != offlineSceneIndex)
            SceneManager.LoadScene(offlineSceneIndex);
    }

}

Answers

  • Hi
    I'm not an expert with Photon and I haven-t compile your code but some things looks very strange and far from any examples I saw until today
    public void StartMatching() { StartCoroutine(requestJoinRandomRoom()); }

    I'm pretty sure that Photon mange by its own all the connection stuff, I guess this is not the right way

    Second stuff that I learned is that BestRegion could be quite tricky, especially with random room
    PhotonNetwork.PhotonServerSettings.HostType = ServerSettings.HostingOption.BestRegion;
    All my applications has the possibility for the user to choose the region, or I hardcoded the region that I meant to use for testing.
    I hope this can be helpful
    P