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
0
Question by The-Oddler · Jul 28, 2012 at 06:38 PM · c#rpcnetworkviewgeneric

Generic RPC Call

Hi,

I'm trying to make an networkview.RPC call with a parameter that can be anything. I can't seem to get it to work though. I tried several things, but none worked:

My first try,

networkView.RPC("TestRPC",RPCMode.AllBuffered, newValue); ...
[RPC] void TestRPC(object newValue) { Debug.Log ("RPC: " + newValue); }

But this gave me an error:

Sending RPC failed because 'TestRPC' parameter 0 didn't match the RPC declaration. Expected 'System.Object' but got 'UnityEngine.Vector3' UnityEngine.NetworkView:RPC(String, RPCMode, Object[])

Which makes sense, though still sucks the Vector3 isn't just cast to an object which should be possible. So I tried to cast it myself:

networkView.RPC("TestRPC", RPCMode.AllBuffered, (object)newValue); // TestRPC method staid the same

Which gave me exactly the same error. Which also makes sense, since object is still a Vector3 and gets serialized, apparently during the deserialization it'll make it a Vector3 again (I think, if I'm wrong here please do tell.)

So then I tried another thing, c# has some great feature, generic methods, which I then tried:

networkView.RPC("TestRPC", RPCMode.AllBuffered, newValue); ...

[RPC] void TestRPC(T newValue) { Debug.Log ("RPC: " + newValue); }

And this gave almost the exact same error, only now it wants a T...

Sending RPC failed because 'TestRPC' parameter 0 didn't match the RPC declaration. Expected 'T' but got 'UnityEngine.Vector3'

Then I though, let's just make multiple "TestRPC" methods with different parameters:

networkView.RPC("TestRPC", RPCMode.AllBuffered, newValue); networkView.RPC("TestRPC", RPCMode.AllBuffered, 5); networkView.RPC("TestRPC", RPCMode.AllBuffered, 3.1415f);

[RPC] void TestRPC(Vector3 newValue) { Debug.Log ("RPC v3: " + newValue); }

[RPC] void TestRPC(int newValue) { Debug.Log ("RPC int: " + newValue); }

[RPC] void TestRPC(float newValue) { Debug.Log ("RPC fl: " + newValue); }

Then the first RPC call worked, it could find the Vector3! Though the other two failed:

Sending RPC failed because 'TestRPC' parameter 0 didn't match the RPC declaration. Expected 'UnityEngine.Vector3' but got 'System.Int32' UnityEngine.NetworkView:RPC(String, RPCMode, Object[])

and

Sending RPC failed because 'TestRPC' parameter 0 didn't match the RPC declaration. Expected 'UnityEngine.Vector3' but got 'System.Single' UnityEngine.NetworkView:RPC(String, RPCMode, Object[])

Then I got another idea: create a little wrapper for object and use that:

public class RPCObject //Tried the same with struct { object _Obj;

  public object Obj
  {
      get
      {
          return _Obj;
      }
      set
      {
          _Obj = value;
      }
  }
  public RPCObject(object obj)
  {
      _Obj = obj;
  }

}

... networkView.RPC("TestRPC", RPCMode.AllBuffered, new RPCObject(newValue)); ...

[RPC] void TestRPC(RPCObject newValue) { Debug.Log ("RPC v3: " + newValue.Obj); }

But this told me that I can't use RPCObject...

Sending RPC failed because 'TestRPC' parameter 0 (RPCObject) is not supported.

Then I found a solution! And this is it:

void CallNetworkViewRPC(string fieldName, T newValue) { System.Type curValType = curValue.GetType(); if (curValType == typeof(Vector3)) { networkView.RPC("TestRPCVector3", RPCMode.AllBuffered, newValue); } else if (curValType == typeof(int)) { networkView.RPC("TestRPCInt", RPCMode.AllBuffered, newValue); } else if (curValType == typeof(float)) { networkView.RPC("TestRPCFloat", RPCMode.AllBuffered, newValue); } }

[RPC] void TestRPCVector3(Vector3 newValue) { Debug.Log ("RPC v3: " + newValue); }

[RPC] void TestRPCInt(int newValue) { Debug.Log ("RPC int: " + newValue); }

[RPC] void TestRPCFloat(float newValue) { Debug.Log ("RPC fl: " + newValue); }

So basecly make a method for each needed variable and call the correct one depending on it's type. This does kind of suck, since now I have to make a whole lot of methods for such a silly little thing...

So, is there a better way of doing it than my working solution?

Note: While writing this I read that apparently the only possible variables send are int, float, string, NetworkPlayer, NetworkViewID, Vector3 and Quaternion. This would make my working solution not that bad to make, but still the question stands, though now more out of curiosity.

For those whom are interested, all the code above are just some tests I was running, the actual thing I'm trying to make is a RPC call that can work for any property:

public Vector3 _WantedDirection; public Vector3 WantedDirection { get{ return _WantedDirection; } set { NetworkChangeProperty("_WantedDirection", ref _WantedDirection, value); } }

void NetworkChangeProperty(string fieldName, ref T curValue, T newValue) { if (!curValue.Equals(newValue)) { if (Network.peerType == NetworkPeerType.Disconnected) { curValue = newValue; } else { networkView.RPC("NetworkChangePropertyRPC", RPCMode.AllBuffered, fieldName, >newValue); } } } [RPC] void NetworkChangePropertyRPC(string fieldName, object newValue) { this.GetType().GetField(fieldName).SetValue(this, newValue); }

Ok, that's all the information I can give I think. If anything still isn't clear, just ask.

Thanks a lot to the people who read all the way to here.

-Pablo

Comment
Add comment · Show 1
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 The-Oddler · Jul 28, 2012 at 06:39 PM 0
Share

Ok, I just posted this, and now all my code parts are broken... If it's not just fixed, I'm doing it right now

1 Reply

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

Answer by Kryptos · Jul 28, 2012 at 07:31 PM

As you can see networkView.RPC takes the name of the method in parameter. And that is the only thing. Therefore you cannot overload the method with different parameters type.

Comment
Add comment · Show 1 · 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 The-Oddler · Jul 28, 2012 at 08:11 PM 0
Share

Yes I know that now, after trying a lot of stuff as you can read :P But you are saying there is no other way around than what I did?

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

6 People are following this question.

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

Related Questions

Problem with Network Connection 1 Answer

RPC not being called 1 Answer

Clearing RPC buffer on a NetworkView 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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