Help to understand some code

Options
I am new to photon. I understand well c# but some times photon confuse me. Belove I have a code that when player killes another, their kills increases. In code I can't understand how is set this kills value to the player that has killed player.
[PunRPC] //This is damage sent fro remote player instance to our local void DoDamage(float damage, PhotonPlayer player){ if(weKilled) return; if(currentHp > 0 && photonView.isMine){ this.StopAllCoroutines(); StartCoroutine(doCameraShake()); } fadeValueB = 2; currentHp -= damage; //We got killed if(currentHp < 0){ if(photonView.isMine){ GameObject wm=GameObject.FindWithTag("WM"); WeaponManager weapm=wm.GetComponent<WeaponManager>(); weapm.saveammo();} //Deactivate all child meshes for(int i = 0; i < transform.childCount; i++){ transform.GetChild(i).gameObject.SetActive(false); } //Spawn ragdoll GameObject temp; temp = Instantiate(ragdoll, transform.position, transform.rotation) as GameObject; if(!photonView.isMine){ temp.SendMessage("clearCamera"); if(PhotonNetwork.player == player){ //Send death notification message to script WhoKilledWho.cs networkObject.SendMessage("AddKillNotification", gameObject.name, SendMessageOptions.DontRequireReceiver); //Add 1 kill for our player int totalKIlls = (int)PhotonNetwork.player.customProperties["Kills"]; totalKIlls ++; Hashtable setPlayerKills = new Hashtable() {{"Kills", totalKIlls}}; PhotonNetwork.player.SetCustomProperties(setPlayerKills); //Add team score int teamScore = new int(); if((string)PhotonNetwork.player.customProperties["TeamName"] == rmm.team_1.teamName){ teamScore = (int)PhotonNetwork.room.customProperties["Team1Score"]; teamScore ++; Hashtable setTeam1Score = new Hashtable() {{"Team1Score", teamScore}}; PhotonNetwork.room.SetCustomProperties(setTeam1Score); } if((string)PhotonNetwork.player.customProperties["TeamName"] == rmm.team_2.teamName){ teamScore = (int)PhotonNetwork.room.customProperties["Team2Score"]; teamScore ++; Hashtable setTeam2Score = new Hashtable() {{"Team2Score", teamScore}}; PhotonNetwork.room.SetCustomProperties(setTeam2Score); } } }else{ //print ("We got killed"); temp.SendMessage("RespawnAfter"); //We was killed, add 1 to deaths int totalDeaths = (int)PhotonNetwork.player.customProperties["Deaths"]; totalDeaths ++; Hashtable setPlayerDeaths = new Hashtable() {{"Deaths", totalDeaths}}; PhotonNetwork.player.SetCustomProperties(setPlayerDeaths); //Destroy our player Debug.Log("Player Destryed"); StartCoroutine(DestroyPlayer(0.2f)); if(PhotonNetwork.player == player){ //Our player fell down networkObject.SendMessage("PlayerFellDown", PhotonNetwork.player.name, SendMessageOptions.DontRequireReceiver); } } currentHp = 0; weKilled = true; } }

Comments

  • vadim
    Options
    The code uses PUN's RPC feature. As seen from first comment, attacking client calls RPC on damaged player and local instances of damaged player update counters in this call.
    Please folow https://doc.photonengine.com/en/pun/current/tutorials/rpcsandraiseevent for more info on RPCs
  • Eduard
    Options
    I know what you mean but in this code I do not understand how kills are added to correct player. I want to mean through code it check if photon view isnt of current player then check if photon player is current local player but this player is killed and add kills to this player. why? I misumderstand somethin there. I think that kills should be added to player who has killed this player
    if(!photonView.isMine){ temp.SendMessage("clearCamera"); if(PhotonNetwork.player == player){ //Send death notification message to script WhoKilledWho.cs networkObject.SendMessage("AddKillNotification", gameObject.name, SendMessageOptions.DontRequireReceiver); //Add 1 kill for our player int totalKIlls = (int)PhotonNetwork.player.customProperties["Kills"]; totalKIlls ++; Hashtable setPlayerKills = new Hashtable() {{"Kills", totalKIlls}}; PhotonNetwork.player.SetCustomProperties(setPlayerKills);
  • vadim
    Options
    You better ask author of this code.
    Or better write your own w/o spending time on decoding other's.
    What are you trying to implement?
  • Eduard
    Options
    I am trying to detect when player kills another
  • vadim
    Options
    It depends on how hit detected. If on attacked player's side thin it player updates its health and checks if death condition met. After that player sends RPC to others to update state of instances accordingly. Player also may ask local attacker player update its kill count and update it on all clients same way, via RPC call to others.
    Another option is to keep all values like health and kill count in player custom properties. I this case all synchronized automatically w/o RPC.
  • Eduard
    Options
    I understand all but when kills are added I do not. The below part of script is confusing me.
    I should look more further.
    also when we say
    if(!photonView.isMine){ temp.SendMessage("clearCamera"); if(PhotonNetwork.player == player){ //Send death notification message to script WhoKilledWho.cs networkObject.SendMessage("AddKillNotification", gameObject.name, SendMessageOptions.DontRequireReceiver); //Add 1 kill for our player int totalKIlls = (int)PhotonNetwork.player.customProperties["Kills"]; totalKIlls ++; Hashtable setPlayerKills = new Hashtable() {{"Kills", totalKIlls}}; PhotonNetwork.player.SetCustomProperties(setPlayerKills);
  • vadim
    Options
    Last 4 lines of code above is getting player's "Kills" custom property, incrementing it and setting updated value back to properties. So now every client can't retrieve this kills count via player's property.