OnPhotonSerializeView() - myChar = (char) stream.ReceiveNext() not working

Options
I had the following code before, throwing error - InvalidCastException
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext((byte)this.state);
            stream.SendNext((byte)this.choice);
        }
        else
        {
            this.state = (int)stream.ReceiveNext();
            this.choice = (char)stream.ReceiveNext();
            //Debug.Log(" : received stream");
        }
    }

Full error
InvalidCastException: Cannot cast from source type to destination type.
PlayerDataView.OnPhotonSerializeView (Photon.Pun.PhotonStream stream, PhotonMessageInfo info) (at Assets/Scenes/PlayerDataView.cs:29)
Photon.Pun.PhotonView.DeserializeComponent (UnityEngine.Component component, Photon.Pun.PhotonStream stream, PhotonMessageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:351)
Photon.Pun.PhotonView.DeserializeView (Photon.Pun.PhotonStream stream, PhotonMessageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:341)
Photon.Pun.PhotonNetwork.OnSerializeRead (System.Object[] data, Photon.Realtime.Player sender, Int32 networkTime, Int16 correctPrefix) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:1730)

This is how I fixed it. Attached a toString function to convert object data type into string and then fetched int using int.Parse(). Similarly for char.Parse() for char.
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext((byte)this.state);
            stream.SendNext((byte)this.choice);
        }
        else
        {
            this.state = int.Parse(stream.ReceiveNext().ToString());
            this.choice = char.Parse(stream.ReceiveNext().ToString()); // ---- that line
        }
    }

Now I am getting the following error when I change this.choice. This error is encountered on machine that is receiving the stream. So am pretty sure it's that line
FormatException: s contains more than one character.
System.Char.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Char.cs:448)
PlayerDataView.OnPhotonSerializeView (Photon.Pun.PhotonStream stream, PhotonMessageInfo info) (at Assets/Scenes/PlayerDataView.cs:29)
Photon.Pun.PhotonView.DeserializeComponent (UnityEngine.Component component, Photon.Pun.PhotonStream stream, PhotonMessageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:351)
Photon.Pun.PhotonView.DeserializeView (Photon.Pun.PhotonStream stream, PhotonMessageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:341)
Photon.Pun.PhotonNetwork.OnSerializeRead (System.Object[] data, Photon.Realtime.Player sender, Int32 networkTime, Int16 correctPrefix) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:1730)
Photon.Pun.PhotonNetwork.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:2100)
Photon.Realtime.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:3002)
ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (ExitGames.Client.Photon.StreamBuffer stream) (at C:/Dev/photon-sdk-dotnet/PhotonDotNet/PeerBase.cs:663)
ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () (at C:/Dev/photon-sdk-dotnet/PhotonDotNet/EnetPeer.cs:545)
ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () (at C:/Dev/photon-sdk-dotnet/PhotonDotNet/PhotonPeer.cs:1759)
Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:205)
Photon.Pun.PhotonHandler.FixedUpdate () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:139)

Comments

  • toughrudder
    Options
    p.s.
    One clarification -- the last error is encountered when the value of this.choice changes on one machine(player 1). It is seen on the other machine(player 2). and vice verse
  • S_Oliver
    S_Oliver ✭✭✭
    edited April 2020
    Options

    You send two bytes so you receive two bytes. If state and choice is a int and string you send them without cast to byte and receive an int and string.

  • toughrudder
    Options
    @S_Oliver yes! Thank you!

    The thing was char is not a primitive type as per photon, So I used string instead. It worked :D