How to change the region?

Hello there,
I have got a multiplayer game with players around the world. They all get connected to their "Best-Match" region. The players want to be able to switch the server to play with their friends from another region.

I first tried to bring them all to one region (us) but it results in players having high pings (300+) depending on the region.

So after connecting them to their best-match region I want them to be able to change the region an reconnect to the selected region.

Any approach?

Greets

Comments

  • Example:
    PhotonNetwork.ConnectToRegion("us");
  • if you're looking to do a nice little dropdown menu I was posted this solution.

    https://forum.photonengine.com/discussion/13209/is-there-a-way-to-select-region#latest

    Let me know how it works for you, does take a bit of tweaking to get working.
  • Jaudatus
    Jaudatus
    edited March 2019
    Thanks for your replies but unfortunately they did not really helped me.

    I managed it by myself. I know that I can change the region with ConnectToRegion but there is more than just calling the method.

    The trick is to wait until you get disconnected from the "old" region and then start a new connection.

    However, here is what works for me:
    using UnityEngine;
    using Photon.Realtime;
    using Photon.Pun;
    
    public class PhotonRegionManager : MonoBehaviourPunCallbacks {
    
        private bool shouldChangeRegion;
        private string nextRegionToConnect;
    
        private void Start()
        {
            PhotonNetwork.ConnectUsingSettings();
        }
    
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Space))
                ChangeRegion("ru");
    
            Debug.Log(PhotonNetwork.CloudRegion);
        }
    
        private void ChangeRegion (string newRegion)
        {
            if (PhotonNetwork.CloudRegion == newRegion)
                return;
    
            shouldChangeRegion = true;
            nextRegionToConnect = newRegion;
    
            if(PhotonNetwork.IsConnected)
                PhotonNetwork.Disconnect();
        }
    
        public override void OnDisconnected(DisconnectCause cause)
        {
            base.OnDisconnected(cause);
    
            if (shouldChangeRegion)
            {
                PhotonNetwork.ConnectToRegion(nextRegionToConnect);
            }
        }
    
        public override void OnConnected()
        {
            base.OnConnected();
    
            if (shouldChangeRegion)
                shouldChangeRegion = false;
        }
    
    }
    
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @Jaudatus,

    I would change one line (minor optimization):
    if (PhotonNetwork.CloudRegion == newRegion)
    
    with
    if (string.IsNullOrEmpty(newRegion))
    {
        // optional log error
        return;
    }
    newRegion = newRegion.Trim();
    if (newRegion.Equals(PhotonNetwork.CloudRegion, StringComparison.OrdinalIgnoreCase))
    {
        return;
    }
    And you need to know that overriding methods from MonoBehaviourPunCallbacks does not require calling base methods since those do nothing.