Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jorgenpt · Oct 08, 2012 at 04:17 PM · c#networkingrpcextension-methods

Is it possible to RPC a C# extension method?

I've tested the below code, but it fails due to it not recognizing a "RemoveRPCsOnServer" on the other end. The method is called like gameObject.networkView.RemoveRPCsOnServer (). Is this a limitation of attributes & extension methods, or is it a limitation in how NetworkView looks for RPC-able methods? Any suggestions for workarounds would be appreciated!

 using UnityEngine;
 
 namespace ExtensionMethods
 {
     public static class NetworkViewExtensions
     {
         /** Same as Network.RemoveRPCs, but ensure it is executed on the server. */
         [RPC]
         public static void RemoveRPCsOnServer (this NetworkView networkView)
         {
             if (Network.isServer) {
                 Network.RemoveRPCs (networkView.viewID);
             } else {
                 networkView.RPC ("RemoveRPCsOnServer", RPCMode.Server);
             }
         }
     }
 }
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kmeboe · Oct 08, 2012 at 07:27 PM 0
Share

I'm not going to post an actual answer, to increase your chances of running into someone who knows for sure. But in the documentation for RPC (http://docs.unity3d.com/Documentation/Components/net-RPCDetails.html) it says that RPC's are limited to only receiving a handful of parameter types (int, string, etc). Your extension method (by necessity) takes a NetworkView object as a parameter, and so I'm guessing this won't work.

avatar image jorgenpt · Oct 08, 2012 at 08:07 PM 0
Share

Thanks for the response - but I'm not actually passing a NetworkView as the parameter - NetworkView is 'this'. Internally, I'm assu$$anonymous$$g networkView.RPC passes the NetworkViewID of the NetworkView, then on the other end looks up the NetworkView that has the given NetworkViewID, and looks for the named object on any of the other components assigned to the GameObject.

There's no problem with the serialization - the networkView.RPC ("RemoveRPCsOnServer", RPC$$anonymous$$ode.Server) call fails because it can't find the RemoveRPCsOnServer method, not because it can't serialize the data. :-)

avatar image jorgenpt · Oct 09, 2012 at 03:48 AM 0
Share

If you want to see how I finally solved it:

https://github.com/jorgenpt/unity-utilities/blob/master/Behaviours/NetworkDestroyer.cs

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by whydoidoit · Oct 08, 2012 at 07:51 PM

No I'm afraid not. It has to be an actual method on the class. You can pass a NetworkViewID but not a NetworkView.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jorgenpt · Oct 08, 2012 at 08:08 PM 0
Share

What do you mean, "an actual method on the class"? I think the problem is that NetworkViews don't look inside the NetworkView class for potential RPC recipients, only on other assigned Components (or possibly $$anonymous$$onoBehaviours). The way extension methods work, this creates an actual instance method called "RemoveRPCsOnServer" on the NetworkView class.

avatar image whydoidoit · Oct 08, 2012 at 08:11 PM 0
Share

I mean

  networkView.RPC ("RemoveRPCsOnServer", RPC$$anonymous$$ode.Server);

Cannot call RemoveRPCsOnServer because that method is not in any script attached to the object with the networkView.

avatar image whydoidoit · Oct 08, 2012 at 08:17 PM 1
Share

Extension methods do not create a new instance method - that would break encapsulation. They are syntactic sugar for calling your method the normal way:

    NetworkViewExtensions.RemoveRPCsOnServer(networkView);

Which you will find you can still do (and must when calling things that were written as extension methods when using Unity Script).

avatar image jorgenpt · Oct 08, 2012 at 08:36 PM 0
Share

Yeah, someone else just brought this up as well. I thought extension methods worked like classes in Ruby (where you can "re-open" the class definition and add new, first-class methods). Oh well, I guess I have to find a new way to approach this problem.

avatar image whydoidoit · Oct 08, 2012 at 08:39 PM 0
Share

There are some very cool things in Ruby I reckon. Wish we could do that in C#...

Actually I do something a bit like what you are trying to do. I have this method of having the call written on the class, but use an extension method to decide whether to execute it or pass it to the server. It's kind of like what you are doing but backwards. I'll post something here in a few - just on a support call

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Network.Instantiate doesn't update in Hierarchy? 0 Answers

Networking RPC sends to wrong target 0 Answers

Multiple Cars not working 1 Answer

SyncVar issue 0 Answers

ClientRPC not getting sent. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges