How client can send data(int) to another client ???

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How client can send data(int) to another client ???

IcePrince
2020-09-16 20:48:05

I make simple project. With 1 param "int test", when Client press button -> test+=5; And changed data send to clients.
Bit it works just when MasterClient (host) changing param... when client changing param it's don't work. I use interface "IPunObservable" and script Photon View. I add video to this discussion, can u help me how client can send changed data to another client ?

video with problem: https://youtu.be/u65q_MHdrjI
project link: https://github.com/iceprinc/PhotonNetwork-TEST

code:

using System.Collections;  
using UnityEngine;  
using Photon.Pun;  
using UnityEngine.UI;  
using System;

public class PhotonController : MonoBehaviourPunCallbacks, IPunObservable  
{  
    public Text testText;  
    private bool connected = false;  
    private int test = 0;  
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)  
    {  
        if (stream.IsWriting)  
        {  
            stream.SendNext(test);  
        }  
        if (stream.IsReading)  
        {  
            test = (int)stream.ReceiveNext();  
        }

    }  
    private void Start()  
    {  
        PhotonNetwork.NickName = $"Player {UnityEngine.Random.Range(1000, 9999)}";  
        PhotonNetwork.AutomaticallySyncScene = true;  
        PhotonNetwork.GameVersion = "1";  
        PhotonNetwork.ConnectUsingSettings();  
        Log("Connecting");  
    }  
    private void Update()  
    {  
        if (connected == true)  
        {  
            Log(Convert.ToString(test));  
        }  
    }  
    public void ClickTestButton()  
    {  
        test += 5;  
        Debug.Log($"Local test = {test}");  
    }  
    public void ClickCreateRoom()  
    {  
        Log("Create room");  
        PhotonNetwork.CreateRoom(null, new Photon.Realtime.RoomOptions { MaxPlayers = 4 });  
    }  
    public void ClickJoinRoom()  
    {  
        Log("Joinning room");  
        PhotonNetwork.JoinRandomRoom();  
    }  
    public override void OnConnectedToMaster()  
    {  
        Log("ConnectedToMaster");  
    }  
    public override void OnJoinedRoom()  
    {  
        Log("Connected");  
        connected = true;  
    }  
    private void Log(string message)  
    {  
        Debug.Log(message);  
        testText.text = message;  
    }

}  

Comments

IcePrince
2020-09-24 16:55:05

Some one help

JohnTube
2020-09-25 11:14:19

Hi @IcePrince,

Thank you for choosing Photon!

This is by design, scene networked objects are controlled by Master Client only.
Read more here.
And stream.IsWriting is true only if photonView.IsMine is true.

So I suggest that you use an RPC with target MasterClient or PhotonView owner to request value change:

    public void ClickTestButton()  
    {  
        if (photonView.IsMine)  
        {  
            AddFive();  
        }  
        else  
        {  
            photonView.RPC("AddFive", RpcTarget.MasterClient);  
            //photonView.RPC("AddFive", photonView.Controller);  
        }  
        Debug.Log($"Local test = {test}");  
    }

    [PunRPC]  
    private void AddFive()  
    {  
       test += 5;  
    }
Back to top