Sending a System.Object or UnityEngine.object via RPC

Hello,

I have a method:
[RPC]
public void RaiseEvent(byte eventCode, object eventContent, bool sendRemotely)
{
    if(sendRemotely)
        photonView.RPC("RaiseEvent", PhotonTargets.Others, eventCode, eventContent, false);
}
This method's syntax is very similar to the built in PUN Event system.

This method is called within a MonoBehaviour, if the bool sendRemotely is true the following line is executed:
photonView.RPC("RaiseEvent", PhotonTargets.Others, eventCode, eventContent, false);

The problem arises with the eventContent parameter. When the RPC is executed it deserializes 'object' Type eventContent into the type it contains. For example, if RaiseEvent is called like this:
byte someByte = 10;
float someFloat = 50f;
RaiseEvent(someByte, someFloat, true);
The method RaiseEvent will call correctly locally. However, the receiving end of the RPC will fail: PhotonView with ID xxxx has no method "RaiseEvent" that takes three argument(s): byte, float, bool.

It makes sense that an object sent via RPC would be converted into its serializable form; the server must receive a type it knows how to serialize(not object), and so the local code (before the RPC) converts objects to recognizable types (if possible).
The problem arises after the RPC sends. The remote PhotonView uses reflection(I assume) to find a method named "RaiseEvent" with the parameters of the RPC. However, the parameters of the RPC are now byte, float. and bool, not byte, object, and bool, and so the PhotonView gives an error that it can find no such Method. Should the PhotonView not check for a method with parameters of the sent Type OR of Type object -- since an object parameter can receive a specific Type argument?

A solution to this problem is to have multiple [RPC] tagged methods which receive each different type of eventContent that could be sent:
[RPC]
void Receive_RaiseEvent(byte eventCode, float eventContent, bool sendRemotely)
{}
[RPC]
void Receive_RaiseEvent(byte eventCode, bool eventContent, bool sendRemotely)
{}

and so forth....
However, this is cumbersome and eliminates the purpose of wrapping Types within an object.

Comments

  • You're putting a float in a call where the argument is an object.
  • I used float as an example. In my actual code its a custom class (which I have Registered with Photon within CustomTypes.cs).
    I'm not sure if that makes a difference.

    However, is this a problem?

    The method calls correctly, the float(or custom class) is received by the eventContent object arguement.
  • Hi,

    Types of RPC function parameters and RPC call should match exactly.
    If you need some sort of 'generic' parameters, try wrap them in ExitGames.Client.Photon.Hashtable
    [RPC]
    public void RaiseEvent(byte eventCode, ExitGames.Client.Photon.Hashtable eventContent)
    ...
    photonView.RPC("RaiseEvent", PhotonTargets.Others, eventCode, new ExitGames.Client.Photon.Hashtable{{1, someFloat }});