purpose of isConnecting = false in PUN Tutorial part 4

I have finished part 4, but don't quite understand the parts where isConnecting is set to false.

For OnConnectedToMaster(), my understanding is that when the player enters a room and clicks "Leave", a new Launcher is created. There is no carrying over of information from the previous Launcher. I have tried removing the line isConnecting = false and the program still runs as expected.

For OnDisconnected, the only instance when isConnecting == true is after the line isConnecting = PhotonNetwork.ConnectUsingSettings() and before the line isConnecting = false. If the client were to disconnect in between these lines and attempt to connect again, isConnecting = PhotonNetwork.ConnectUsingSettings() will run once more, setting isConnecting to true. Thus, setting isConnecting to false doesn't result in any effect, since isConnecting will be set to true again.

I tried commenting out both of the lines where isConnecting = false, and the game runs without any difference. What then is the use of isConnecting = false? Thanks.

Here is a section of the working code from Launcher.cs.
public void Connect() {
        progressLabel.SetActive(true);
        controlPanel.SetActive(false);

        // we check if we are connected or not, we join if we are , else we initiate the connection to the server.
        if (PhotonNetwork.IsConnected) {
            // #Critical we need at this point to attempt joining a Random Room. If it fails, we'll get notified in OnJoinRandomFailed() and we'll create one.
            // Called when we click Play, leave a room and click play a second time
            PhotonNetwork.JoinRandomRoom();
        } else {
            // #Critical, we must first and foremost connect to Photon Online Server.
            // keep track of the will to join a room, because when we come back from the game we will get a callback that we are connected, so we need to know what to do then
            isConnecting = PhotonNetwork.ConnectUsingSettings();
            PhotonNetwork.GameVersion = gameVersion;
        }
    }


    #endregion

    #region MonoBehaviourPunCallbacks Callbacks


    public override void OnConnectedToMaster() {
        Debug.Log("PUN Basics Tutorial/Launcher: OnConnectedToMaster() was called by PUN");
        // we don't want to do anything if we are not attempting to join a room.
        // this case where isConnecting is false is typically when you lost or quit the game, when this level is loaded, OnConnectedToMaster will be called, in that case
        // we don't want to do anything.
        if (isConnecting) {
            // #Critical: The first we try to do is to join a potential existing room. If there is, good, else, we'll be called back with OnJoinRandomFailed()
            PhotonNetwork.JoinRandomRoom();
            // #Question what is the use of this line if a new Launcher is created whenever we go to lobby?
            isConnecting = false;
        }
    }


    public override void OnDisconnected(DisconnectCause cause) {
        Debug.LogWarningFormat("PUN Basics Tutorial/Launcher: OnDisconnected() was called by PUN with reason {0}", cause);
        // Runs when the client disconnects to show control panel once again
        progressLabel.SetActive(false);
        controlPanel.SetActive(true);
        isConnecting = false;
    }

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    edited April 2020
    Hi @yoloswaglord,

    Thank you for choosing Photon!

    The PUN Basics Tutorial was originally written for PUN Classic by @jeanfabre.
    The idea behind isConnecting flag is explained in these lines:
    // we don't want to do anything if we are not attempting to join a room.
    // this case where isConnecting is false is typically when you lost or quit the game, when this level is loaded, OnConnectedToMaster will be called, in that case
    // we don't want to do anything.

    If you quit a room using LeaveRoom, OnConnectedToMaster will be called and in this case we don't want to join a room but wait for another call to Connect method.

    Unless I'm wrong and we disconnect when quitting the room by calling Disconnect.
    Maybe in the tutorial there are no cases where the client would be connected to master server without attempting to join a room. I think the tutorial was simplified or skipped this part where you would be connected to master first and then, based on user interaction (input) try join a room.
  • Hi,

    yes, this boolean flag make sure we don't reconnect when we are disconnected or leaving a room. it basically let our code know that we initiated the process of connecting and joining a room.

    Bye,

    Jean
  • Thanks for your reply @JohnTube. I still don't quite see the need for setting isConnecting to false.
    JohnTube wrote: »
    If you quit a room using LeaveRoom, OnConnectedToMaster will be called and in this case we don't want to join a room but wait for another call to Connect method.
    Yes, but when we quit a room, a new Launcher is instantiated, and isConnecting will have a default value of false.
    JohnTube wrote: »
    I think the tutorial was simplified or skipped this part where you would be connected to master first and then, based on user interaction (input) try join a room.
    I don't quite understand how having a intermediate option of deciding to join a room affect whether we should have the line isConnecting = false.
  • Hi,

    it's best to always properly set variables to their expected state and always reflect the current situation they describe with their values. It's a practice I strongly encourage to adopt: never leave a property to a value that is not what it should be even if it doesn't matter at the first glance. It will come back with a vengeance at some point during your development ... :)

    Bye,

    Jean
  • Thank you for your reply @jeanfabre!