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. Dismiss Notice

Disconnecting and reconnecting

Discussion in 'NetCode for ECS' started by Blargenflargle, May 9, 2022.

  1. Blargenflargle

    Blargenflargle

    Joined:
    Feb 24, 2019
    Posts:
    92
    Hello, I am building a game where you can join a match and while in that match connect to a friends lobby. However I am running into some issues when I try to disconnect from one server to connect to another. What steps do I have to go through to "tear down" an active connection so that I can safely run
    Code (CSharp):
    1. World.GetExistingSystem<NetworkStreamReceiveSystem>().Connect(networkEndPoint);
    without getting any undesired behavior.
     
  2. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    774
    you should add a NetworkStreamRequestDisconnect component to the connection. This will disconnect the client from the server at the beginning of the next frame.
    You then should just wait until the connection has been destroyed before calling the Connect again
     
  3. Blargenflargle

    Blargenflargle

    Joined:
    Feb 24, 2019
    Posts:
    92
    Can I safely GetSingletonEntity<NetworkStreamInGame>() to get the connection entity on the client? If not, is there any singleton I can target to get the connection entity?
     
  4. Lukas_Kastern

    Lukas_Kastern

    Joined:
    Aug 31, 2018
    Posts:
    97
    The presence of NetworkStreamInGame is handled by your own connection flow/logic.
    So if you guarantee that it's been added to your network connection and won't be removed, sure.
    Otherwise, you can just use NetworkIdComponent to identify connection entities or the connection singleton on the client.