If one player closes the game, leaves the network the other player wins. How to implement this?

jsm
jsm
As above. I have tried the following to set the win condition when a player drops off the network but the other player doesn't win, only the spawned units disappear.
public void CheckWinCondition ()
    {
        if (PlayerController.me.DeadCount == 4 || PlayerController.enemy.DeadCount == 4 || !PlayerController.me || !PlayerController.enemy)
        {
            photonView.RPC("WinGame", RpcTarget.All, PlayerController.enemy == leftPlayer ? 0 : 1);
        }
    }

Comments

  • Just override OnPlayerLeftRoom method inside that check for win condition. T
    public override void OnPlayerLeftRoom(Player otherPlayer)
            {
    			CheckWinCondition ();
       
            }
    
  • jsm
    jsm
    edited August 2021
    Just override OnPlayerLeftRoom method inside that check for win condition. T
    public override void OnPlayerLeftRoom(Player otherPlayer)
            {
    			CheckWinCondition ();
       
            }
    



    Thanks, I tried this but it didn't work, same result.
    public override void OnPlayerLeftRoom (Player otherPlayer)
        {
            GameManager.instance.photonView.RPC("WinGame", RpcTarget.All, PlayerController.enemy == GameManager.instance.leftPlayer ? 0 : 1);
        }
    
  • Check the photon logs to see if WinGame is getting called or not. You can verify logs by enabling it in Photon Networking scriptable object.

  • Check the photon logs to see if WinGame is getting called or not. You can verify logs by enabling it in Photon Networking scriptable object.

    Where would OnPlayerLeftRoom() go? In my script (from a tutorial) this is in the Menu.cs script. Do I need to place it again into the GameManager? It says "no suitable method to override" for that one. Thanks for your help
  • Solution - Attach this script to the same gameobject the GameManager and Photon View scripts are attached to:
    using Photon.Pun;
    using Photon.Realtime;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class LeaveRoom : MonoBehaviourPunCallbacks
    {
        
        public void leave()
        {
            PhotonNetwork.LeaveRoom();
        }
        public override void OnPlayerLeftRoom(Player otherPlayer)
        {
            GameManager.instance.photonView.RPC("WinGame", RpcTarget.All, PlayerController.me == GameManager.instance.rightPlayer ? 1 : 0);
        }
    }