Search Unity

NetworkDiscovery.broadcastData can not change?

Discussion in 'Multiplayer' started by faneric, Nov 17, 2015.

  1. faneric

    faneric

    Joined:
    May 15, 2013
    Posts:
    10
    Hi, I'm making a matchMaker for wlan use. Something like the server tells the current player counts, the game name etc. Then I found NetworkDiscovery, and broadcastData can used to do so.
    My problem is when I changed the broadcastData in the server, the client doesnt change the same way, it remains the initial data that it was recieved.
    Here is some code in my LobbyManager:

    int count = 0;
    public override void OnStartHost()
    {
    base.OnStartHost();

    discovery.broadcastData = "count: " + count;
    discovery.Initialize();
    discovery.StartAsServer();
    }
    void OnCount()
    {
    count++;
    discovery.broadcastData = "count: " + count;
    }

    And MyNetDiscovery:
    public class MyNetDiscovery : NetworkDiscovery {
    public override void OnReceivedBroadcast(string fromAddress, string data)
    {
    base.OnReceivedBroadcast(fromAddress, data);
    Debug.Log("fromAddress: " + fromAddress);
    Debug.Log("data: " + data);
    }
    }

    When the connection begins, the client prints data: 0. After I called OnCount() in LobbyManager, the client never changes. Am I doing something wrong ?
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    the broadcast data cannot be changed while the broadcaster is running. There is bug filed on this already - 742022
     
  3. faneric

    faneric

    Joined:
    May 15, 2013
    Posts:
    10
    Thanks. Can I solve this by writing a NetworkDiscovery based on LLAPI? Or Maybe I should change my solution.
    By the way, where can i find the bug file?
     
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
  5. gamecakestudios

    gamecakestudios

    Joined:
    Nov 13, 2013
    Posts:
    21
  6. Oshroth

    Oshroth

    Joined:
    Apr 28, 2014
    Posts:
    99
    Why don't you just restart the broadcast server once the broadcast message is changed?
     
  7. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    Opposite question, why you cannot just stop broadcast change data start broadcast again?
     
  8. faneric

    faneric

    Joined:
    May 15, 2013
    Posts:
    10
    Yeah, I did it as you said. Thank you all the same!
     
  9. gamecakestudios

    gamecakestudios

    Joined:
    Nov 13, 2013
    Posts:
    21
    That is how I have it implemented right now, but the "StopBroadcast()" function call does not register with the NetworkTransport class until at least a frame has elapsed. If I try to restart the broadcast within the same function that I stopped it in, it gives me an error saying the broadcast hasn't stopped yet. This is very inconvenient as I'd like to do the full change within one function all on the same frame.

    Instead, I have to wait in an update loop or coroutine for NetworkTransport.IsBroadcastDiscoveryRunning() to be false, before running the rest of my logic. It works, but is not ideal.
     
  10. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    @bbharier hmmm, looks like make sense. I need to think about this. Could you please describe in detail what you need :) (Imagine that you create requirements or request new feature) How api should works and so on. (Sorry just do not have time to do it by myself) then we can discuss can this be implemented or not. OK?
     
  11. gamecakestudios

    gamecakestudios

    Joined:
    Nov 13, 2013
    Posts:
    21
    Absolutely! I will try to make this as concise as possible, because your help is greatly appreciated.

    Within the class NetworkDiscovery, I would just make two changes:

    1) - Add Method ModifyActiveBroadcast()
    • public void ModifyActiveBroadcast()
      • Throws warning explaining that "this method will only have an effect if called while broadcasting is active." if called while NetworkTransport.IsBroadcastDiscoveryRunning() == false.
      • If there is an active broadcast running, take the value in broadcastData, and pass it into NetworkTransport to replace the old broadcast message from its next tick onward.
    • - Overload Method - public void ModifyActiveBroadcast(string newBroadcastMessage)
      • Sets broadcastData = newBroadcastData
      • Runs ModifyActiveBroadcast() (non-overload version)
    2) - Add to the ///<summary> on broadcastData to explain this functionality.
    • Add sentence that says something like: "Modifying broadcastData while broadcasting is already active will not change the live broadcast message. Please run ModifyActiveBroadcast() to update the live broadcast message with the value stored in broadcastData."
    Hope that is useful to you!

    Thanks! :)
     
  12. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    gotcha thanks, added to list - "think about" :)
     
  13. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Good god, please no :)

    I don't want to care about what methods to call under different circumstances. Change the broadcastData property to be read-only and add a method called SetBroadcastData(string data) that just sets it regardless of whether or not the broadcast is running. Whatever the engine has to do in order for that to happen it should just do in the set method. One point of exposure, no need for the programmer to do additional checks themself, and no chance that they'll call the wrong method or set the wrong property.
     
    wccrawford, Emxm3, Chom1czek and 2 others like this.
  14. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Sooo - not to be a jerk or anything but....changing the broadcast data doesn't change the length of the original string? In that sending "1234" and then changing it to "abc" will send "abc4". Really guys? Seriously? That was a thing that made it through QA? Did an intern write this?
     
    d1favero likes this.
  15. YVanhoutte

    YVanhoutte

    Joined:
    Mar 5, 2018
    Posts:
    8
    I just stumbled upon this problem myself, seems that nothing's changed since 2016.
    I'm already writing my own NetworkDiscovery class, but I have a sneaking suspicion that the problem for this resides within the NetworkTransport layer which is not opensource?
     
  16. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    We dumped UNet in favor of Photon and just stripped out features that weren't supported or easily implemented ourselves. It's a shame with all the great stuff Unity has been doing that their multiplayer services continue to be a straight up dumpster fire.