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 dresing9 · Oct 02, 2012 at 09:15 AM · variablesrpcnetworkviewnetworkplayer

Rpc cannot change variable in function OnConnectedToServer!

Hi

I try to make a game where 3 players connect to a server, and the server can play along, i wanna define each player as a NetworkPlayer by counting numbers of connections. I syncronize with RPC call which works just fine to change the count variable at both server and clients. BUT when i try to call the variable in the OnConnectedToServer() function, it suddenly do not work, here is a short version of my code. Thanks in advance.

 static var connected : int = 0;
 var connected_S : int = 0;
 static var server : NetworkPlayer;
 static var player2 : NetworkPlayer;
 static var player3 : NetworkPlayer;
 static var player4 : NetworkPlayer;
 
 
 function OnGUI()
 {
     if(Network.isServer)
     {
         if(Start_Game)
         {
             networkView.RPC ("Conections", RPCMode.All, connected_S);
         }
     }
 }
 function OnPlayerConnected(player: NetworkPlayer)
 {
     connected_S += 1;
     print(connected);
 }
 
 function OnConnectedToServer()
 {
     print(connected);
     if(connected == 0)
     {
         player2 = Network.player;
     }
     if(connected == 1)
     {
         player3 = Network.player;
     }
     if(connected == 2)
     {
         player4 = Network.player;
     }
 }
 @RPC
 function Conections(connection : int)
 {
     connected = connection;
     print (connected);
 }
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 dresing9 · Oct 02, 2012 at 09:34 AM 0
Share

Hi, thanks for your answer, 'connected_S' is a local variable the server uses to set the global variable 'connected' which will be used in the OnConnectedToServer() to define how many players that is connected at the current time.

avatar image Dreamblur · Oct 02, 2012 at 11:03 AM 0
Share

Your code is not optimized and very confusing.

First of all, you should not be calling an RPC function in an OnGUI call since you'll be wasting a lot of cycles sending redundant work across the network.

Secondly, assu$$anonymous$$g that no external script would be accessing those particular variables in your script -- and there's no reason for them to do so, as far as I can tell -- then you don't have to declare them static. That might confuse you into thinking that the NetworkView automatically synchronizes them across the network.

And lastly, there is no need to declare those variables in the first place. You can always check the number of connections in the server and all of the clients' information by accessing Network.connections from the server-side script. You don't have to manually store those data in your own variables per connection. And because you can access all that data in one call, you can simply send that just before the game starts, i.e., you don't need to send it across the network piecewise.

Optimize your code a little bit based on those points and you might find your glitch somewhat nonexistent.

0 Replies

· Add your reply
  • Sort: 

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

Passing variables back and forth in client server environment using RPC 1 Answer

Send RPC to owning NetworkPlayer of networkView 1 Answer

Get NetworkPlayer from ViewID 1 Answer

Does the position change need state synchronization? 2 Answers

Problem with Playmaker Send Remote Event 1 Answer


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