Mobile Game - change host on moving game to background

Options
hi guys.

i made a small mobile game that hold up to 5 players in a room.

once host on android press his "back button" on the device the effect doesn't fire out like on my in game exit button that work fine and switch host and disconnect the player.

i think its because it just "block" or "override everything" compared to the exit button script.
some times only a part of the code getting "fired up".

when he click that button its translated to escape button on unity and here is the code for a reference:
void Update(){
		if (Input.GetKeyDown (KeyCode.Escape)) {
			if(currentlevel==1){
				Application.Quit();
			}else{
				if(currentlevel==3){
					if(PhotonNetwork.isMasterClient){
					foreach (PhotonPlayer player in PhotonNetwork.playerList) {
						if(!player.isLocal&&!sonce){
							sonce=true;
							PhotonNetwork.SetMasterClient(player);
						}
					}
					}
					PhotonNetwork.Disconnect();
				}
				Application.LoadLevel("MainMenu");
			}
			sonce=false;
		}

now when he press the game exit button i just disconnect him and continue the game loop with :
void OnMasterClientSwitched
and every thing working fine.

the same problem happen also when he click the home button (code doesn't fire up to disconnect him) in the middle of the game.
some times only a part of the code getting "fired up".

this is the code:
u will notice that if he is alone in the room he suppose to get disconnected and to be sent to the main menu
void OnApplicationPause(bool pauseStatus) {
		Debug.Log ("paused " + pauseStatus);

		if (pauseStatus) {
			if(PhotonNetwork.isMasterClient){
			foreach (PhotonPlayer player in PhotonNetwork.playerList) {
				if(!player.isLocal&&!sonce){
					sonce=true;
						PhotonNetwork.SetMasterClient(player);
				}
			}
				if(!sonce){
					PhotonNetwork.Disconnect();
					Application.LoadLevel("MainMenu");
				}
			}
			GameObject playerr = GameObject.FindGameObjectWithTag("Ome");
			pos = playerr.transform.position;
			playerr.transform.position=new Vector3(1000f,1000f,1000f);
		} else {
			GameObject playerr = GameObject.FindGameObjectWithTag("Ome");
			playerr.transform.position=pos;
		}
		sonce = false;
	}

so i might not thinking about it the proper way, what is the way to handle a game that been sent to the background once a user clicked his home button or received a phone call? or clicked his back button on the android device?
maybe there is a way to automatically disconnect a user even if the game is in background after lets say 10 seconds?

Comments

  • guyhazi
    Options
    i was managed to handle the android back button issue by adding disconnection when the new scene open up if the user is connected, it was manage able as the game keep running.


    but when the user presses his home button while in game that stile is an issue so:

    is there a way to control it with a different user and disconnect another user or switch master client ?

    like user A haven't move for 10 seconds and all his "handled" instantiated object didn't move for 10 seconds so i can easly find out about it if I'm the user B that just joined in and user B take the lead by making him self the master client and disconnect user A?
  • Tobias
    Options
    In some cases, we can't send a disconnect when the OS (Android/iOS) will instantly pause the application. You can try a disconnect in either case (Unity should have callbacks for "home button pressed", too) but it's not guaranteed to work - sadly!

    After a few seconds, the connection will time out and some other player will become Master Client automatically. This should not take longer than 10 seconds usually.
    Did you try this? How long does it take for you?
  • guyhazi
    Options
    i didn't really try on iOS but one time testing i notice that it disconnect few seconds after user press home button.

    but on android it doesn't until the program is closed from background so what did to over come this issue is i made special script that receive rpc every 2 seconds and passes an int that count ++ and made a system to check if that int didn't change in the last 5 seconds the user will be local destroyed and the player with the lowest number will be the new host, and for all the instantiated object that the old host made i added a script on them that if pos.x or y or z changed less than 0.5 in 3 seconds(they constantly moving) they will be local destroyed to, eventually all will be normal destroyed once he will disconnect.

    it would be awesome if there would be a more proper way of doing it once there is need of a constantly active room with more then 2 players
  • Tobias
    Options
    We are thinking about using the "inactive" state or adding a new one for players who are (temporarily) not active. That way, we can strike a better balance between a long(ish) timeout and making sure someone is responsive.
    This affects a pretty low level of the API and workflow, so it's a bit more work to insert this while not destroying other stuff.
    As said: We are interested in this and will do something similar, but there's no timing set in stone yet.
  • guyhazi
    Options
    that is awesome hope fully will see it soon.

    as for every one who see this and have the same problem in order to get over it just do this:

    make an rpc that is unique for the host and send it to every one every amount of time lets say 2 seconds.

    when receiving the rpc increase an int - int player1ping++;

    make a coroutine that check every amount of time if that rpc recived int increase that was received from host had actually been increased in the last 5 seconds, if not then host is inactive.

    and add an if statement that change host to the lowest other player id and local destroy the old host until he will normally disconnect.

    you can also instantiate host object as scene object to not have to much mess handling them on the same situation.