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

NetworkServer.SendToAll, host gets the message twice

Discussion in 'Multiplayer' started by FStar, Dec 18, 2015.

  1. FStar

    FStar

    Joined:
    Sep 11, 2015
    Posts:
    50
    Hi,

    I upgraded from 5.2.2 to 5.3.1 and now the SendToAll calls from my host(host + client) results in two calls being made to the to the host for each call. I have not touched the code after upgrade and it worked perfectly before upgrading. Normal clients which are not hosts gets the message only once.

    Code (csharp):
    1.  
    2. // Example 1:
    3. NetworkServer.SendToAll(Constants.ACTION_RESPONSE_MESSAGE, new ActionResponseMessage() { player = gameObject, action = action, message = message, success = success, timer = 0 });
    4.  
    5.  
    6. // Example 2:
    7. NetworkServer.SendToAll(Constants.SPAWNITEMS_MESSAGE, msg);
    8.  
    I've debugged without being able to see anything wrong. No exceptions in the client message handler and the SendToAll call is made only once on server side.

    Could this be a new bug in 5.3 or some undocumented change in functionality?
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    It is not supposed to have changed. Can you provide a repro?
     
  3. FStar

    FStar

    Joined:
    Sep 11, 2015
    Posts:
    50
    I will look into it this evening, my project is rather complex, although I might be able to repro in a new project, will send it in this case...

    I have solved my immediate issue by not using SendToAll anymore, I needed to work on proximity checking and only send to selected players anyway.

    On a second note, I just noticed also that sync list has stopped working, I now get duplicate items in the list on the client if I change it on the server. It worked well in 5.2 but broke with 5.3.
     
  4. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    I think this is the same issue that I have with the NetworkAnimator.SetTrigger(). The host sets the trigger twice.
     
  5. Leoo

    Leoo

    Joined:
    May 13, 2013
    Posts:
    96
    I kindda need that sendtoall to even send to host too ;(
     
  6. FStar

    FStar

    Joined:
    Sep 11, 2015
    Posts:
    50
    Ligalr likes this.
  7. FStar

    FStar

    Joined:
    Sep 11, 2015
    Posts:
    50
    Ok, the SyncList bug was harder to find.

    The double receiving of items in SyncList is only happening at the first sync when a new player logs in.

    If I have a list of items in a script on the player object, I populate the list with some items on Player 1 and then log in Player 2. Player two receives Player 1 list twice, meaning all the items in the list are doubled in the initial sync. Naturally, after that everything goes bad, indexes sent in OnItemsChanged events are wrong compared to the list in the client etc.

    If I log in both players before starting to populate the lists everything is fine because it's only the initial sync that is doubled, not subsequent events.

    I suspect that SyncLists are using SendToAll somewhere in the Unity implementation and that this is in fact related to the NetworkServer.SendToAll bug.

    If you want a repro of this one as well, I can send one. It was very easy to reproduce it in a new project once I understood what was happening.
     
  8. Ligalr

    Ligalr

    Joined:
    May 6, 2015
    Posts:
    2
    We experience this also on 5.3.1,
    Every SendToAll occurs twice on the host after upgrading to 5.3.1.
     
  9. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    i cannot repro the SyncList issue in 5.3.0p1 using this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class Player : NetworkBehaviour
    6. {
    7.  
    8.     public SyncListFloat m_floats = new SyncListFloat();
    9.  
    10.     [Command]
    11.     void CmdAddFloat()
    12.     {
    13.         m_floats.Add(Time.time);
    14.     }
    15.  
    16.  
    17.     void Update()
    18.     {
    19.         if (Input.GetKeyDown(KeyCode.Space))
    20.             CmdAddFloat();
    21.     }
    22.  
    23. }
    24.  
     
  10. FStar

    FStar

    Joined:
    Sep 11, 2015
    Posts:
    50
    I can repro with this code, it is slightly more involved and requires a Text field to display the result. * Edit - Actually 2 text fields one for the local player and one for the remote player.

    Code (csharp):
    1.  
    2.  
    3. public class MyMessage : MessageBase
    4. {
    5.     public string message;
    6. }
    7.  
    8. public struct SyncItem
    9. {
    10.     public string itemName;
    11. }
    12.  
    13. public class SyncListItems : SyncListStruct<SyncItem>
    14. {
    15. }
    16.  
    17. public class Player : NetworkBehaviour
    18. {
    19.    Text _messageControl;
    20.     int itemCounter = 0;
    21.  
    22.     [SyncVar]
    23.     public SyncListItems myItems = new SyncListItems();
    24.  
    25.     // Use this for initialization
    26.     void Start()
    27.     {
    28.         if(isLocalPlayer)
    29.             _messageControl = GameObject.Find("Messages").GetComponent<Text>();
    30.         else
    31.             _messageControl = GameObject.Find("Messages2").GetComponent<Text>();
    32.  
    33.         myItems.Callback = OnItemsChanged;
    34. }
    35.  
    36.     // Update is called once per frame
    37.     void Update()
    38.     {
    39.         if (isLocalPlayer)
    40.         {
    41.             if (Input.GetKeyUp(KeyCode.Space))
    42.             {
    43.                 CmdSayHi();
    44.             }
    45.  
    46.             if (Input.GetKeyUp(KeyCode.D))
    47.             {
    48.                 CmdDelete();
    49.             }
    50.         }
    51.     }
    52.  
    53.  
    54.     private void OnItemsChanged(SyncListItems.Operation op, int index)
    55.     {
    56.         _messageControl.text = "";
    57.  
    58.         foreach(var item in myItems)
    59.             _messageControl.text += item.itemName + "\n";
    60.     }
    61.  
    62.  
    63.     [Command]
    64.     private void CmdSayHi()
    65.     {
    66.  
    67.         myItems.Add(new SyncItem() { itemName = "Test item no " + itemCounter++ });
    68.     }
    69.  
    70.     [Command]
    71.     private void CmdDelete()
    72.     {
    73.         myItems.RemoveAt(0);
    74.     }
    75. }
    76.  
    77.  
     
    Last edited: Dec 22, 2015
  11. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    It is because you have a [SyncVar] custom attribute on the SyncList. This is causing it to be serialized twice.

    added bug 756416
     
  12. FStar

    FStar

    Joined:
    Sep 11, 2015
    Posts:
    50
    Ah! hehe, it must have changed since version 5.2 then. But this is good news, means I can fix it even without a patch :)

    Thanks Seanr!


    The SendToAll bug is supposed to be fixed in next patch according to support mail I an hour ago.

    Thanks Unity Team!
     
  13. morriscurtis

    morriscurtis

    Joined:
    Dec 13, 2014
    Posts:
    3
    Hey! The last patch release came out on Dec 23 but apparently it is not fixed yet... :(
    If that patch release wasn't meant but it is in the next one, when is that one coming?

    I really need this fix in order to continue with my project.
    I tested it with a PositionMessage that contains a Vector3. (I know I can sync the position with network transforms, it is just an example. I'm making a virtual board game and therefore I want to send the moves via messages over the network.)

    I tried to do a workaround by iterating over the active connections and send every client a message directly:
    Code (CSharp):
    1. foreach (var connection in NetworkServer.connections)
    2.     if (connection != null)
    3.         NetworkServer.SendToClient(connection.connectionId, msgType, msg);
    ...but then I get other errors: (Tested with 1 host and 1 client: The message to the host with connectionId 0 works but the message with connectionId 1 throws this error)
     
    Last edited: Dec 30, 2015
  14. l3fty

    l3fty

    Joined:
    Mar 23, 2013
    Posts:
    86
    I'm still getting the duplicate messages on the server from NetworkServer.SendToAll. I'm using 5.3.1p2 on Mac.
     
    rmindel likes this.
  15. rmindel

    rmindel

    Joined:
    Apr 24, 2015
    Posts:
    4
    When will this be fixed? it's been 3-4 releases already.
    This issue involves the most basic function of Unity's Multiplayer - NetworkServer.SendToAll ....
    There are major updates for multiplayer on 5.3 and above but we can't update from 5.2.4 because of this fundamental bug :/

    The bug page still says "Open"
    https://fogbugz.unity3d.com/default.asp?756153_7dnt56hjavb8ct4k
    Maybe it wasn't uploaded to the right place?
     
  16. FStar

    FStar

    Joined:
    Sep 11, 2015
    Posts:
    50
    I have worked around this by manually sending to all players:

    Code (csharp):
    1.  
    2. ...
    3.  foreach(var player in players)
    4.             NetworkServer.SendToClientOfPlayer(player.gameObject, msgType, msg);
    5. ...
    6.  
    the "players" variable is a list of all connected players, or in my case all players in proximity of something that should be sent to clients.


    The answer I got from support on December 22 was:

    Dec 22, 15:19

    Hi ****,

    Thanks for the e-mail.

    Yes, this bug is showing as fixed. The fix will not yet be released. It should be available in the next patch release:

    https://unity3d.com/unity/qa/patch-releases

    Let me know if you have any further problems.

    Kind Regards,
    ******
    Unity Support
     
    l3fty likes this.
  17. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Fixed in today's patch
     
    rmindel, FStar and l3fty like this.
  18. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    What patch? The latest release page still says 5.3.1 which was out before your post @seanr
     
  19. l3fty

    l3fty

    Joined:
    Mar 23, 2013
    Posts:
    86
  20. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    2017.3.1; host is getting SendToAll message twice, while the message is sent only once.