• 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
Question by NickP_2 · May 31, 2014 at 11:36 AM · multiplayerserializationdata

Multiplayer data exchange

Hello

I recently came up with an idea for multiplayer game. And I'm using Google Play's real time multiplayer services(Android App). Now I know Google play services don't have anything to do with Unity forums, but It's not that I want to talk about.

My game excists of 2 players, and is real time turn based. To exchange data to the other user, I use the following line of code:

 PlayGamesPlatform.Instance.RealTime.SendMessageToAll(reliable, data);

Where reliable true = TCP, false = UDP. Data is a byte array.

Now 2 players are in the exact same scene, just on another device, and when player1 does something in his scene, I have to let player2 know of what he did, so player2 can change it in his scene.

Now let's say we have a bowl, full of spheres with rigidbodys, all affected by gravity and forces, and one player can drag one sphere around, which interacts with all the other spheres of course. How should I handle this data? I tried:

  • Sending the dragged sphere's position and name to the other player using UDP reliability, but when a packet is out of order, the physic interactions won't be exactly the same on both hosts (and it has to be close to perfect). Also I have to loop trough all the spheres, in order to find the dragged one first, before I can change the position, which is a huge performance sucker for mobile devices, because it's done multiple times per second.

  • exactly the same with TCP, but the hosts scene will just crash, or go VERY slow.

Oh, and the way I serialize the data to a byte array:

Serializable class:

 [System.Serializable()]
 public class BlockController
 {
     public string Name { get; set; }
     public Vector3 Position { get; set; }
     
     public BlokController(string name, Vector3 position)
     {
         Name = name;
         Position = position;
     }
 }

Serialize this class before sending:

 private static byte[] ObjectToByteArrayBlock(BlockController obj)
 {
     if(obj == null)
     {
         return null;
     }
     BinaryFormatter bf = new BinaryFormatter ();
     MemoryStream ms = new MemoryStream ();
     bf.Serialize (ms, obj);
     return ms.ToArray ();
 }

and deserialize on receiving:

   public static BlockController ByteArrayToObjectBlock(byte[] arrBytes)
     {
         MemoryStream memStream = new MemoryStream ();
         BinaryFormatter bf = new BinaryFormatter ();
         memStream.Write (arrBytes, 0, arrBytes.Length);
         memStream.Seek (0, SeekOrigin.Begin);
         BlockController obj = (BlockController)bf.Deserialize (memStream);
         return obj;
     }

So basicly, what data should I send to the other host, to make sure the same physic interactions and all the sphere positions are updated? You may also correct my way of serializing, or if you have something better, shoot!

Thanks for your precious time!

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by fortunacus · Jun 14, 2014 at 10:23 AM

my suggestion is let the phisics done by active player device, for example like this: player A phisic calculated by device A, and the position simple sent to device B, player B calculated by device B, and sent the postion to device A

Comment
NickP_2

People who like this

1 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 NickP_2 · Jun 29, 2014 at 06:07 PM 0
Share

Thanks for the answer. But I moved on to another project. Ill check it out later

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Sharing custom made game levels with other users during gameplay. 0 Answers

Limit reached when serializating XML 0 Answers

how to serialize unity variables? (Sprite, Image ...) 1 Answer

Serializing Dictionary with Vector3 keys 1 Answer

Dedicated Server Database 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