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 lvictorino · Aug 31, 2012 at 05:30 PM · networkingnetworkserializationnetworkviewonserializenetworkview

Client to client Serialization

Hi,

I have some troubles to synchronize players thanks to `OnSerializeNetworkView`. I have read a lot of documentation about this but nothing helped me in this specific case.

I have set a `TextMesh` (let's call it `text_entry`) in my scene and players can interact with it by entering a number.

I have another `TextMesh` (called `text_reflect`) that is instantiated thanks to `Network.Instantiate` at the beginning of the game. `text_reflect` has a `NetworkView` component and a script is attached to this component. It's a simple monobehaviour script with only `OnSerializeNetworkView` filled.

The method looks like this:

     private void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
     {
         int stream_value = 0;
         if ( stream.isWriting )
         {
             stream_value  = int.Parse(GameObject.Find("text_entry").GetComponent<TextMesh>().text);
             stream.Serialize(ref stream_value );
         }
         else if ( stream.isReading )
         {
             stream.Serialize(ref stream_value );
             GetComponent<TextMesh>().text = stream_value .ToString();
         }
     }

As you can see I want to reflect over the network the value of text_entry by changing text_reflect.

This actually works for a host <=> client serialization but not for client <=> client ... Values coming from host are correctly updated but when it comes from a client to another client the `stream.isReading` context is not even executed.

Of course this is just an example but I can't manage to make it working.

Any help would be appreciated!


After answer the code should look like this:

     private void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
     {
         int stream_value = 0;
         if ( stream.isWriting )
         {
             if (networkView.isMine)
                 stream_value  = int.Parse(GameObject.Find("text_entry").GetComponent<TextMesh>().text);
             else if ( Network.peerType == NetworkPeerType.Server ) // here server relay data
                 s = int.Parse(GetComponent<TextMesh>().text);
             
             stream.Serialize(ref stream_value);
         }
         else
         {
             stream.Serialize(ref stream_value );
             GetComponent<TextMesh>().text = s.ToString();
         }
     }
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 asafsitner · Aug 31, 2012 at 05:43 PM 0
Share

Now to more specific questions:

I presume that 's' is a class field and is initialized prior to OnNetworkSerialize.

Who instantiates `text_reflect`? Does every client have one copy that they own, replicated on the other clients' machines?

avatar image lvictorino · Aug 31, 2012 at 05:50 PM 0
Share

About 's'; it's a typo I will change in the question. 's' was in fact 'stream_value'.

Another object instantiate text_reflect. Every client have one copy they own and correctly replicated on other clients. Every ViewID are replicated.

avatar image lvictorino · Aug 31, 2012 at 05:53 PM 0
Share

@asafsitner When you try something as simple as this example... does it work for you?

1 Reply

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

Answer by ScroodgeM · Aug 31, 2012 at 10:45 PM

server not just relaying data. it receives data like client from owner, and then sends like owner to other clients. so your data chain looks like:

  1. owner serializes and sends data

  2. server deserializes and applies data

  3. server serialized and sends data

  4. remote clients deserializes and applies data

in your case (1) works but (4) fails. sine (1) vs (2) and (3) vs (4) should usually work, your miss rather between (2) and (3). so debug server about does it works ok without exceptions and what does it reads and sends.

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 lvictorino · Sep 01, 2012 at 06:08 AM 1
Share

So you mean server should not only read but also write in stream even for network view he doesn't own? I thought it was not possible to write in a stream you don't own... I will try anyway. Thank you.

avatar image ScroodgeM · Sep 05, 2012 at 07:36 PM 1
Share

only server can write (and it always do it) to serialized data.

avatar image pyjamads · Aug 29, 2014 at 11:24 AM 0
Share

dude you just saved me from going insane .... Thx so much ... :) Couldn't for the life of me figure out why my client to client communication wasn't working with Floats but it worked with Vector3, turns out, I was storing the Vector3 value on the server in the Transform.position, to have some visual feedback to my problem, but the float value, didn't get stored server side, so when the server 're-routed' the variable, it just contained 0 ....

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

9 People are following this question.

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

Related Questions

What is Send rate? 1 Answer

BinaryFormatter and MemoryStream cause a loop of "SerializationException: Unexpected binary element: 0" 1 Answer

Acess server stuff by the client 0 Answers

Networking dupplicate processing of components 0 Answers

Photon Unity Networking fix physics delay 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