Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Making a network library

Discussion in 'Multiplayer' started by MakakWasTaken, Aug 4, 2016.

  1. MakakWasTaken

    MakakWasTaken

    Joined:
    Mar 26, 2015
    Posts:
    45
    I have been trying to find some good ressources on how networking is done in games (not nessesarily a unity game)

    I would prefer making my own library because I would like to learn how this is done (not use lidgren or any toget 3rd party library).

    I can't figure out how this could be done and I don't want to create a class that contains every type/class that I could possibly want to sync and then convert it to a byte[] and send it through a socket. Isn't there a more dynamic approach to this?
     
    Last edited: Aug 4, 2016
  2. MakakWasTaken

    MakakWasTaken

    Joined:
    Mar 26, 2015
    Posts:
    45
    My best idea on how this could be done is:

    To send a object array over the internet, where the first object is a string with a method name, and the rest is the parameters for the given method, and then invoke the method with the parameters on the receiver side using System.Reflection, but I am not sure if
    - this would even be possible at all
    - this would require too much bandwidth
    - this would be the smartest approach to doing this.
     
  3. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,021
    Sending objects is step 2.
    Unless you use TCP (or Lidgren or ...), you need to wrap objects in messages. You need to know how much data to read, in which order, etc.
    So step 1 of making a networking library is to do the actual communication that will enables you to pass messages and objects later on.
    There are no DIY Tutorials for this, afaik. Best, have a look at www.gafferongames.com and try to understand an open source networking solution, like lidgren or enet.

    Have fun!
     
  4. MakakWasTaken

    MakakWasTaken

    Joined:
    Mar 26, 2015
    Posts:
    45
    Now I (finally) have a somewhat working sending/receiving system for the objects. My next question would be how to spawn/send a gameobject instance to another player, is there any way to get/find an asset by some id that will consistently stay the same on all clients.

    @tobiass
     
  5. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,021
    Have a look at prefabs and the "special" Resources folder.
    An alternative would be to setup some list of (network)assets. With such a list, you will have an ID per asset, too.
     
  6. MakakWasTaken

    MakakWasTaken

    Joined:
    Mar 26, 2015
    Posts:
    45
    I'm just bit curious as to how unity3d/photon handles this. Because it doesn't seem to require you to put the assets in a special folder. Maybe the assetID system would work better in this case, but I am not sure how to get the asset by id when it is compiled.
     
  7. DamonJager

    DamonJager

    Joined:
    Jul 16, 2015
    Posts:
    56
  8. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,021
    PUN uses the approach "all prefabs must be in a Resources folder". This was mostly done, so it doesn't require a special setup. It's simple and does the job.
    How uNet does it ... I don't know.
     
  9. MakakWasTaken

    MakakWasTaken

    Joined:
    Mar 26, 2015
    Posts:
    45
    Ohh I didn't no that, sorry for not doing my research properly before asking, but that is propably the only way then because UNet might have a "back entrance" to all this since it is officially implemented by unity.
     
  10. MakakWasTaken

    MakakWasTaken

    Joined:
    Mar 26, 2015
    Posts:
    45
    I have have hit a problem with running multiple clients, that both receive and send data. I know what is causing the problem (The port is already in use) but I have no clue on how to fix it, here is a really simplified version of my code:
    Code (CSharp):
    1.  
    2. UdpClient client;
    3. void Start () {
    4.    client = new UdpClient (12444);
    5.    IPEndPoint ep = new IPEndPoint (IPAddress.Parse ("127.0.0.1"), 12444);
    6.  
    7.    byte[] bytes = System.Text.Encoding.ASCII.GetBytes ("Test");
    8.    client.Send (bytes, bytes.Length, ep);
    9.  
    10.    client.BeginReceive (new AsyncCallback (Receiver), null);
    11. }
    12.  
    13. void Receiver(IAsyncResult res) {
    14.    IPEndPoint sender = new IPEndPoint (IPAddress.Any, 0);
    15.    byte[] data = client.EndReceive(res, ref sender);
    16.  
    17.    string text = System.Text.Encoding.ASCII.GetString(data);
    18.  
    19.    Debug.Log("Received msg from:"+sender.Address+",contents:"+text);
    20. }