First Android Game - Part 16 - Clients Selecting Quit Aug 22, 2023
This is part 16 of making my first Android Game. If you missed part15, you can find it here Currently the users can quit the game by selecting "Quit" on the WhatYouHaveMenu. The "Quit" code is using SceneManager.LoadScene(quitToMainScene); When the client quits, this is what the master looks like. You can see that the client character stays and then another one rejoins When the client quits, this is what the client looks like. You can see that even in the main menu scene, the coins and enemies are still spawning. The client then rejoins the game even though I did not select connect I clearly don't understand what happens when user quits the game by closing the app or selecting "Quit" from the WhatYouHaveMenu. So In GameManager.cs I addeded PUN Callbacks: OnDisconnected(Photon.Realtime.DisconnectCause cause), OnLeftRoom(), OnMasterClientSwitched(Photon.Realtime.Player newMasterClient) and OnPlayerLeftRoom(Photon.Realtime.Player otherPlayer) I added Debug.Logs into each of those callbacks. I tested what happens when user quits the app by closing the app vs when the user selects "Quit" from the WhatYouHaveMenu which is calling SceneManager.LoadScene(quitToMainScene). I first tested selecting "Quit" from the menu, there were no Debug.Logs(). However, in the editor, if I quit play mode, OnLeftRoom() and OnDisconnected() gets called If the editor was the master and mobile was the client, and mobile closes the app. OnPlayerLeftRoom() would get called in the editor console. However, if the mobile was the master instead and the editor was the client, then OnPlayerLeftRoom() and OnMasterClientSwitched would get called in the editor console The conclusion is selecting "Quit" from the WhatYouHaveMenu does nothing in terms of leaving the room or server. Looking at the Asteroid example, I see that in their OnLeftRoom(), they would call PhotonNetwork.Disconnect. So I thought I try calling PhotonNetwork.Disconnect() when the user selects "Quit". This did not work and threw an error "operation set (252) not called because client is not connected" I then noticed in the Asteroid Game example, in the OnDisconnected() they would call UnityEngine.SceneManager.LoadScene(). So I removed the LoadScene from OnQuitClick(). I saw that OnLeftRoom() would get called first then OnDisconnected() would get called next. So In OnLeftRoom(), that's where I would call the loadscene. Now when the client drops out, it can join lobbies and roomes again. If the master drops out, the client will still become the master due to the implementation from the previous blog From the result below is the recording of the master. When the client drops out, the client would not rejoin, and also disappears! From the result below is the recording of client. When the client drops out, the coins and enemies don't spawn on the client side. The client can also join the lobby and create rooms! Check out blog 17 here
Recent blogs See all blogs