Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Omerduru · Apr 07 at 01:49 AM · photonplayerprefsrpcintpun

Sending int value owned by Masterclient to other client

My game has 2 scenes named "menu" and "game". There is an int value that players can change in the menu. (this value is 1-2 or 3). The room is set up, the players join the room, and when the players are ready, masterclient start the game and the "game" scene is loaded. What I want in this part: The int value belonging to the masterclient is initially taken, this value is sent to all other players, and the int value of the other players equals the int value of the masterclient. I tried doing this with rpc, I don't get any errors but the values ​​are not synchronized. Anyone know why?

 public class xcontrol : MonoBehaviourPunCallbacks {
     private int x;
     public GameObject c1,c2,c3;
 
 [PunRPC]
     private void xkaç(int x)
     {
         if (PhotonNetwork.IsMasterClient)
         {
             x= PlayerPrefs.GetInt("xset");
             Debug.Log(x);
             PlayerPrefs.SetInt("xset", x);
         }
 
 
     }
     void Start()
     {
         photonView.RPC("xkaç", RpcTarget.OthersBuffered, x);
         xkaç(x);
         if (x == 1)
         {
             c1.SetActive(true);
             c2.SetActive(false);
             c3.SetActive(false);
         }
         if (x == 2)
         {
             c1.SetActive(false);
             c2.SetActive(true);
             c3.SetActive(false);
         }
         if (x== 3)
         {
             c1.SetActive(false);
             c2.SetActive(false);
             c3.SetActive(true);
         }
 
 
     }
 }


Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Captain_Pineapple · Apr 07 at 06:39 AM

Yes.


your function xkaç might be called on all clients but it does nothing with the value you receive. Only the master client will save it to the playerprefs. All other players do nothing at all.


May i propose some changes to your code - please DON'T blindly copy it, understand it and then use it if you deem it good:

 public class xcontrol : MonoBehaviourPunCallbacks {
     private int x;
     public GameObject[] cObjects; //assign former c1,c2,c3 in order to this array
  
     [PunRPC]
     private void xkaç(int receivedX)
     {
         Debug.Log($"Setting new value for x: {receivedX}");
         x = receivedX;
        
         if (!PhotonNetwork.IsMasterClient)
         {
              //could set x for all non-master clients here:
              //PlayerPrefs.SetInt("xset", receivedX);
         }

         setCObjectsActiveState();
     }
 
     void Start()
     {
         if (PhotonNetwork.IsMasterClient)
         {
              // give x as second argument here to return it as default if "xset" is not present as key:
              x = PlayerPrefs.GetInt("xset", x);
              photonView.RPC("xkaç", RpcTarget.OthersBuffered, x);
              xkaç(x);
         }
     }
 
     private void setCObjectsActiveState()
     {
         for(int i=0;i<cObjects.Length;i++)
         {
             cObjects[i].SetActive(i==x);
         }
     }
 }


Also as a sidenote: you should work on your variable names. Variables, functions and classes must always describe what they are doing. This helps you and others to understand the code. e.g. c1, c2 ... or x and xcontrol are bad names. same for xkaç


Also i changed c1, c2 and c3 to be a list instead. this helps to keep your code maintainable as you can now simply add more objects by adding just one element to the array and you don't have to change any code for it to still work.


EDIT: For clean complete solution added the IsMasterClient check in Start as this was indeed missing.

Comment
Add comment · Show 3 · 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 Omerduru · Apr 07 at 11:08 PM 0
Share

Thank you for your suggestions and edits. If I remove this, I can't access the x value : x = PlayerPrefs.GetInt("xset") thanks to debug.log i see x is always 0. And I only see x value in masterclient's console, For other clients debug.log is invalid. Result: each player gets his own x value. I still can't equate x value to masterclient's x value

avatar image Captain_Pineapple Omerduru · Apr 08 at 06:11 AM 1
Share

Hey, i updated the answer with some "Lit" changes. This should now work as intended as everything in Start should only be executed on the masterclient, this is now the case. Let me know if something is unclear.

avatar image Omerduru Captain_Pineapple · Apr 08 at 09:15 PM 0
Share

Thank you very much, it works fine with its updated version

avatar image
0

Answer by GetLitGames · Apr 07 at 01:00 PM

You only want the Master client to send the RPC with the x value to the other clients. Move "if (PhotonNetwork.IsMasterClient)" down to the line above "photo view.RPC" so only Master client sends it. Then all you need inside "xkaç(int" function is this.x = x; and setCObjectsActiveState();.. So, your master sends it's x value to all the other clients and they set their this.x to be that value and call setCObjectsActiveState() but it might be slightly better if that function took a parameter instead of using this.x do setCObjectsActiveState(x)

Comment
Add comment · Show 11 · 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 Captain_Pineapple · Apr 07 at 02:08 PM 0
Share

Hey man,

this is a funny one indeed. Not sure what your understanding of up/downvoting is... Please leave a comment and explain what is wrong about an answer when you downvote something.

This will help people a lot.


Either way, as your answer is a extension to the code i posted it might be helpful to note that with your answer. Your addition here is indeed a good idea.

avatar image Caeser_21 Captain_Pineapple · Apr 07 at 02:20 PM 0
Share

I think he downvoted your answer because, you downvoted his answer on this thread... but again this is just purely assumption.

avatar image Caeser_21 Caeser_21 · Apr 07 at 02:25 PM 0
Share

I'll upvote to make it neutral until @Omerduru checks out the answer or @GetLitGames replies why he think your answer is wrong :)

avatar image GetLitGames Captain_Pineapple · Apr 08 at 01:56 AM 0
Share
  1. The fact that you gave someone new code, that they will not understand, and you asked them not to just copy/paste but to understand it shows that you already knew you did something wrong. The fact you put things in bold like they are orders is also pretty ridiculous.

  2. Your answer was completely wrong. The root of the problem is that only the MasterClient should send the RPC with the value and your code wasn't even correct in any way, shape, or form. Furthermore adding the array was pointless and increases code complexity, you don't even know if he needs more than 3 objects.

  3. It was instant karma that you downvoted my correct answer (that in your opinion had some kind of misleading part to it) and I happened to look into what kind of other answers you give only to find an atrocious answer like this.

avatar image Captain_Pineapple GetLitGames · Apr 08 at 05:49 AM 0
Share
  1. Nice of you to imply that @Omerduru is not smart enough to understand the code changes - i want to make sure that people do this as this is imo the crucial part of a learning experience. I take things in bold as emphasis on information, not as orders but whatever. Especially since when does an order begin with a please?


2. Man in my last comment i even chose to give you credit for givin good and correct input but ok if you want to s%&! all over it here we go again: No, the original version of xkac did nothing but print x. Simply changing the remote execution to only master client would not (emphasis - not an order) have solved the issue of x not being changed on the non-master clients. Only the combined changes of your version and mine will fix all issues in here.

I am also not really sure if you are aware of the definition of code complexity. Yes an array is more "complex" in a way but 3 if statements with bad maintainabilty are not better in any way.


3. I feel sorry for you if a downvote in some forum means that much to you personally that you are like: "Man this guy downvoted my answer. Instead of considering that half of my answer is indeed incorrect and standing by some mistake i made i'll go and downvote some answer of them! That'll show them!".

Idk, if that is a profilepic of you then telling you to grow up wont help here i guess.

Say whatever you like, i will not continue this discussion any further.

Show more comments
avatar image Omerduru · Apr 07 at 11:32 PM 0
Share
 [PunRPC]
      private void xkaç(int x)
      {
         
            this.x=x;
 //    x= PlayerPrefs.GetInt("xset);
         setCObjectsActiveState(x);
      }
  
      void Start()
      {     if (PhotonNetwork.IsMasterClient)
          photonView.RPC("xkaç", RpcTarget.OthersBuffered, x);
          xkaç(x);
      }
  
      private void setCObjectsActiveState(int x)
      {
          for(int i=0;i<cObjects.Length;i++)
          {
              cObjects[i].SetActive(i==x);
          }
      }



If I don't use this, the x value shows up as 0 every time. x = PlayerPrefs.GetInt("xset") I couldn't understand why I shouldn't use this. Isn't this how I need to get the int value that the players save in the "menu" scene? But the result is still the same unfortunately. Result: each player gets his own x value. I still can't sync x to masterclient's x.

avatar image GetLitGames Omerduru · Apr 08 at 02:03 AM 1
Share

You were almost there, remember the MasterClient is telling the other clients what value to use. So you need one more line added like shown below to read the value from PlayerPrefs and send that value to the other clients. The other clients will receive the value in your xkaç RPC function call, and you can do whatever you need to there. Hopefully you have a little better understanding now - have you MasterClient issue RPCs to the other clients to give them values or ask them to do something. To improve your code you may want to remove "x" class field, since it is better habit to use function parameters and pass things around rather than using "global" fields/variables.

 void Start()
 {     
           if (PhotonNetwork.IsMasterClient)
          {
               var masterValue = PlayerPrefs.GetInt("xset");
               photonView.RPC("xkaç", RpcTarget.OthersBuffered, masterValue);
               xkaç(masterValue);
            }
 
avatar image GetLitGames GetLitGames · Apr 08 at 02:05 AM 1
Share

a "class member field" is a variable you declared at the class scope: private int x;

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

146 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Update int value with photon rpc? 0 Answers

Photon PUN2, Rpc score can't correctly. 1 Answer

Photon RPC relationship question. 0 Answers

Photon Unity Networking carrying scene objects 1 Answer

I'm trying to generate a random key and share it with other players when they join the game i'm using PUN. it's not working please help! 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