OnPhotonSerializeView, different data for different players

Options
Hello there!
I'm trying to implement some score points manager.
Let's say we have continuous shooting. And when a player shoots another player, the first player should get score points, but since the shooting is continuous, i can't send RPC, because in this case I have to send RPC every frame which is not good. So when a player shoots another player, I collect the score points, and then I want to sent them to the player which was shooting.
And since there might be a few players that are shooting one player, I want to send correct amount of score points to each player.
How can I make it with OnPhotonSerializeView ?
I imagine it like this
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
for (int i = 0; i < scorePoints.Length; i++)
{
if (scorePoints[i].value > 0)
{
stream.SendNext(scorePoints[i].value, scorePoints[i].playerId);
scorePoints[i].value = 0;
}
}
}
else
{
int points = (int)stream.ReceiveNext();
ScoreManager.Instance.AddScore(points);
}
}

Comments

  • Bunzaga
    Bunzaga
    edited April 2017
    Options
    You can't really use streams for values which may change in number like this. The read/write stream is a very strict relationship. For every write, there has to be a read, and of the correct type, otherwise exceptions are thrown.

    Looking at your algorithm, you could just do something like... use your for loop to add up all your score values together, in one int; then write that single int. Then in read stream, you read that one int.

    Unless you really need all those values broken up for some reason, in which case, you could first write stream the Length of scorePoints, then go through the for loop and write stream the point values. In your read stream, first read stream the length value, then you'll know how many values to iterate...

    Good luck :D
  • ChazAshley
    Options
    i've made it like this

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections.Generic;

    namespace Com.Hikkainc.MazeBattle
    {
    [RequireComponent(typeof(PhotonView))]
    public class ScoreManager : SingletonMonoBehaviour
    {
    public Text ScoresText, FinalScore;

    private List _scores;
    private PhotonView _photonView;
    private int _score, _scoreIncrement = 1;
    private float pauseInSeconds = 1, time;

    private void Awake()
    {
    _photonView = GetComponent();
    _scores = new List();
    time = Time.time;
    }

    void Update()
    {
    if (Time.time - time >= pauseInSeconds)
    {
    lock (_scores)
    {
    for(int i =0; i<_scores.Count; i++)
    {
    var s = _scores[i];
    if (s.score > 0)
    {
    _photonView.RPC("AddScore", PhotonTargets.Others, s.playerId, s.score);
    s.score = 0;
    _scores[i] = s;
    }
    }
    }
    time = Time.time;
    }
    }

    public void SetFinalScore()
    {
    FinalScore.text = SocialShit.Instance.IncrementScore(_score).ToString() ;
    }

    [PunRPC]
    void AddScore(int playerId, int score)
    {
    if (playerId == PhotonNetwork.player.ID)
    {
    _score += score;
    DisplayScore();
    }
    }

    public void SendScore(int playerId)
    {
    var index = _scores.FindIndex(s => s.playerId == playerId);
    if (index>=0)
    {
    var score = _scores[index];
    score.score += _scoreIncrement;
    _scores[index] = score;
    }
    else
    {
    _scores.Add(new ScoreAndPlayerId(){playerId = playerId, score = 1});
    }
    }

    public void DisplayScore()
    {
    ScoresText.text = _score.ToString();
    }

    struct ScoreAndPlayerId
    {
    public int playerId, score;
    }
    }
    }