Multiplayer Character Kills & Scoring

Arachai

Member
So I have been playing around the the scoring when a player is killed; pretty much I am adding a point to the Owner of the AttackerPhotonView in the Die function of the Pun Health Monitor that killed the player. I end up building the demo level and menu and run 2 versions of the game in order to test it and notice that if the 1st player kills the 2nd player he gets no point; however if the 2nd player kills the 1st player the 2nd player receives the point as they should. Scoring works perfect on the 2nd player but not at all on the 1st.

Now however the 1st person can kill the turret and receives a point every time they kill it; they just cannot kill another player and get a point. I am adding the point by:
attackerPhotonView.Owner.AddScore(killScore); with killScore being an integer that is currently set to a default of 1.
 

Arachai

Member
*Bump* Any advice? It seems that the info does not update. Player 2 enters can get a point from killing player 1. Player 3 enters can get a point from killing player 1 and player 2. Player 1 and 2 do not get a point for killing player 3. This will continue with each new player that enters the room.

I'm missing something apparently because if the PUN Add-on is supposed to lay over PUN then this should work because it works without using the OPSIVE character...
 
Last edited:

Justin

Administrator
Staff member
I haven't used that method before so I would try posting on the Photon forums. The character controller doesn't use the score at all.
 

Arachai

Member
I haven't used that method before so I would try posting on the Photon forums. The character controller doesn't use the score at all.

Thank you Justin; I took a look at the photon forum and couldn't find anything helpful to my situation. However I decided to go with a thought I had the other night: increasing the score is an Event that happens On Death. Sure enough this afternoon I decided to write an OnDeath event and added it to the player prefab and the scoring works!
 

Arachai

Member
If anyone else is looking for a solution to scoring with the character on PUN then below is the code I am using. This will give the player a score of 10 for each kill. However you can not use the same character (2 Nolans in a scene) or else Player 1 will score 5 for each kill and Player 2 will score 10 for each kill.

Death Event:

Code:
using System.Collections.Generic;
using UnityEngine;
using Opsive.UltimateCharacterController.Events;
using Photon.Pun;
using Opsive.UltimateCharacterController.AddOns.Multiplayer.PhotonPun.Traits;
using ExitGames.Client.Photon;

public class PlayerDeathEvent : MonoBehaviour
{
    // Start is called before the first frame update
    public void Awake()
    {
        EventHandler.RegisterEvent<Vector3, Vector3, GameObject>(gameObject, "OnDeath", OnDeath);
    }

    /// <summary>
    /// The object has died.
    /// </summary>
    /// <param name="position">The position of the force.</param>
    /// <param name="force">The amount of force which killed the object</param>
    /// <param name="attacker">The GameObject that killed the object</param>
    public void OnDeath(Vector3 position, Vector3 force, GameObject attacker)
    {
        int killPoint = 5;
        if (attacker != null)
        {
            var attackerPhotonView = attacker.GetComponent<PhotonView>();
            //Debug.Log(attackerPhotonView.Owner.NickName + " killed " + PhotonNetwork.LocalPlayer.NickName);
            attackerPhotonView.Owner.AddScore(killPoint);
            //Debug.Log(PhotonNetwork.LocalPlayer.NickName + " was killed by " + attackerPhotonView.Owner.NickName);
            //Debug.Log("Kill Score: " + attackerPhotonView.Owner.GetScore());
            if (attackerPhotonView == null)
            {
                Debug.LogError("Error: The attacker " + attacker.name + " must have a PhotonView component.");
                return;
            }
        }
    }

    public void OnDestroy()
    {
        EventHandler.UnregisterEvent<Vector3, Vector3, GameObject>(gameObject, "OnDeath", OnDeath);
    }
}

UI Display:

Code:
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using Opsive.UltimateCharacterController.AddOns.Multiplayer.PhotonPun.Traits;
using ExitGames.Client.Photon;
using UnityEngine.UI;

public class PlayerInfoManager : MonoBehaviour
{
    Player p;
    public Text scoreText;
    public Text actText;
    public Text kText;
    //public Text masterText;
    //private Dictionary<int, GameObject> playerListEntries;

    private void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (PhotonNetwork.LocalPlayer.GetScore().ToString() == "0")
        {
            scoreText.text = "Score: 0";
        }

        else
        {
            scoreText.text = "Score: " + PhotonNetwork.LocalPlayer.GetScore().ToString();
        }
    }
}

Photon Score Extensions:

Code:
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using Hashtable = ExitGames.Client.Photon.Hashtable;

namespace Opsive.UltimateCharacterController.AddOns.Multiplayer.PhotonPun.Traits
{
    public class PunScoreManager : MonoBehaviour
    {
        public const string PlayerScoreProp = "score";
    }

    public static class ScoreExtensions
    {
        public static void SetScore(this Player player, int newScore)
        {
            Hashtable score = new Hashtable();  // using PUN's implementation of Hashtable
            score[PunScoreManager.PlayerScoreProp] = newScore;

            player.SetCustomProperties(score);  // this locally sets the score and will sync it in-game asap.
        }

        public static void AddScore(this Player player, int scoreToAddToCurrent)
        {
            int current = player.GetScore();
            current = current + scoreToAddToCurrent;

            Hashtable score = new Hashtable();  // using PUN's implementation of Hashtable
            score[PunScoreManager.PlayerScoreProp] = current;

            player.SetCustomProperties(score);  // this locally sets the score and will sync it in-game asap.
        }

        public static int GetScore(this Player player)
        {
            object score;
            if (player.CustomProperties.TryGetValue(PunScoreManager.PlayerScoreProp, out score))
            {
                return (int)score;
            }

            return 0;
        }
    }
}
 
Top