[RPC] Declaration does not work when you extend a class.

I was attempting to subclass a a custom class that derives from MonoBehavior and has a method with the [RPC] declaration before it, but it seems that once I subclass this main class with the [RPC] method in it, the NetworkingPeer does not find any [RPC] flagged methods on it anymore. I do not override the RPC method, but it would seem that it should still find these marked methods.

Is this a bug or just the way that it is with .NET? Unfortunately I don't have any experience when working with reflection or marking methods in this manner so I can't really comment on whether or not this is a bug.

Comments

  • Thats how photon is implemented, it will not search up the inheritance tree to reflect the RPCs, only on the instances class itself.

    There is a thread on the matter and how to expand PUN to make it reflect the whole hierarchy and offer the base classes RPCs in extended classes.
  • Did you ever get this to work? I just tried and had the same result.

    I'd much rather have my own class, this way, if I ever change which network plumping I use, I could just change my class instead of looking for photonviews everywhere.
  • Here is a patch I made for this purpose. Enjoy!
    --- a/Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs    
    +++ b/Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs    
    @@ -2907,13 +2907,14 @@ internal class NetworkingPeer : LoadbalancingPeer, IPhotonPeerListener
                     Debug.LogError("Invalid PhotonNetworkingMessage!");
                     return null;
                 }
    -
    -            MethodInfo metInfo = monob.GetType().GetMethod(
    -                methodType + string.Empty,
    -                BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
    -                null,
    -                argTypes,
    -                null);
    +               
    +                       MethodInfo metInfo;
    +                       {
    +                               string name = methodType + string.Empty;
    +                               BindingFlags flags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
    +                               metInfo = FetchMethod(type, ref name, flags, argTypes);
    +                       }
    +                       
                 if (metInfo != null)
                 {
                     methods.Add(methodType, metInfo);
    @@ -2929,4 +2930,18 @@ internal class NetworkingPeer : LoadbalancingPeer, IPhotonPeerListener
                 return null;
             }
         }
    +       
    +       private MethodInfo FetchMethod(System.Type type, ref string name, BindingFlags flags, Type[] argTypes)
    +       {
    +               MethodInfo info = type.GetMethod(name, flags, null, argTypes, null);
    +               
    +               if (info != null)
    +                       return info;
    +       
    +               Type baseType = type.BaseType;
    +               if (baseType != null)
    +                       return FetchMethod(baseType, ref name, flags, argTypes);
    +       
    +               return null;
    +       }
     }
    
  • I also changed the binding flags in PUN 1.18. This should have the same effect.
    PUN 1.18 is not yet in the Asset Store but available as preview package here:
    viewtopic.php?f=17&t=2333
  • I tried to change the binding flags at first by replacing BindingFlags.DeclaredOnly by BindingFlags.FlattenHierarchy but I didn't get the expected result. I'll have a look at this preview package once I get a chance for sure!