Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 roamcel · Aug 02, 2011 at 04:23 PM · variablesrpcnetworkviewpassing

Passing variables back and forth in client server environment using RPC

I have seen quite a bit of examples and documentation until now regarding rpc and client server however, there's a very stupid and very obscure subject on which I hope those good amongst you can shed some light: requesting a server variable from a client, and updating said value... when the scripts are in different gameobjects.

I have seen quite some examples of RPC calls when the script calls an rpc function on that same script on the server or .others, but what about calling an rpc function somewhere else, asking for a variable value, or trying to set up a variable there?

My attempts at making a STATIC rpc function, or even a very stupid "return server variable" rpc function have failed miserably, especially since RPC returns always VOID.

Please help, thanks.

This is basically the code that I need to use, but that doesn't update my needed variable, when run client-side.

 private int variable = 0;

 void OnNetworkLoadedLevel () {
     if (Network.isServer) {
         print("As a server");
         variable = returnvariable();        
     }
     else {
         print("As a client");
         networkView.RPC("returnvariableRPC",RPCMode.Server, Network.player);
     }
     print (variable + " doesn't hold the value it should");
 }

 private int returnvariable() {
     do_stuff();
 }

 [RPC]
 public void returnvariableRPC(NetworkPlayer player) {
     int localvariable = do_stuff();
     networkView.RPC("assignvariableRPC",player,localvariable);
 }

 [RPC]
 public void assignvariableRPC(int parameter) {
     variable = parameter;
 }
Comment
Add comment · Show 2
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 BerggreenDK · Aug 02, 2011 at 10:38 PM 0
Share

Perhaps you would consider showing some of your code, so we might have a look at it and see if we can spot the problem?

avatar image roamcel · Aug 02, 2011 at 10:48 PM 0
Share

I've basically added all the code I've come up with. Basically the client never receives the call "back" from the server, apparently. I have not found a way to make it so that the client receives the server's actual variable value.

1 Reply

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

Answer by roamcel · Aug 03, 2011 at 03:41 PM

Ok I've understood what's going on. The code is correct, but there's a timing problem. In particular, here

 else {
     print("As a client");
     networkView.RPC("returnvariableRPC",RPCMode.Server, Network.player);
 }
 print (variable + " doesn't hold the value it should");

networkView.RPC will run and will immediately return focus to the OnNetworkLoadedLevel, without waiting for completion. variable WILL actually be correctly updated... but long after the calling routine has completed. How do I fix this? Is there a way to put the script to 'sleep' until networkView.RPC completes the call, or I'm just using the wrong approach? It feels like the second option, actually. As a matter of fact, I'd rather have the connection time out rather than the script going forward without the expected variable value.

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 roamcel · Aug 04, 2011 at 08:46 AM 0
Share

Quite clearly the only "solution" is to implement the follow-up code into the "returnvariableRPC" function, which will run on the client that instantiated that very call. However it doesn't feel like the right way to go, actually.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rpc cannot change variable in function OnConnectedToServer! 0 Answers

RPC Mode set at ALL but only sender executes function 1 Answer

Where is a free, helpful tutorial on how to use Unity's networking? 2 Answers

How to update score (M2H networking) 0 Answers

Inaccurate network position 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