Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to get the client and host IDs for Custom Messages?

Discussion in 'Netcode for GameObjects' started by farazk86, Jan 29, 2023.

  1. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    194
    Hi,

    Im moving my multiplayer system from apple's game center to Unity multiplayer, so I already have a messaging system implemented that I would like to use pretty much as is.

    For that the Custom Messages seem to work for me and the documentation for this has been helpful: https://docs-multiplayer.unity3d.com/netcode/current/advanced-topics/message-system/custom-messages

    But my question is, how do I get the client ID and host ID needed for sending the custom messages?

    I am currently using Lobby and Relay to make the connections and get the playerID and HostID from the lobby player list.

    But these IDs are not the ones needed by the CustomMessaging system but rather they are something else.

    Code (CSharp):
    1. customMessagingManager.SendUnnamedMessage(writer);
    2.  
    I want to send my message to the client and the host. (There are only 2 maxPlayers in each lobby )

    How do I get these Ids?

    Thank you
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,533
    You should be able to get these from either or both the connection approval callback on server side and the OnClientConnected callbacks on client side.
     
  3. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    194
    Thanks, Im not familiar with the connection approval callback. Can you please mention the callback function name so that I can look it up.

    Also, my understanding was that the OnClientConnected callback can only be used on the host/server. Using this callback, how can the client get the server/host id?
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,533
    I just realized, the NetworkManager has properties to get all of the IDs you want whether on server or client:
    • ServerClientId
    • LocalClientId
    • ConnectedClients or ConnectedClientsList

    approval callback is mentioned here: https://docs-multiplayer.unity3d.com/netcode/current/basics/connection-approval/index.html

    Here's an example for client connect/disconnect events:
    https://docs-multiplayer.unity3d.co....html#connection-notification-manager-example

    Note: Client connected events also run on the local client!
     
    farazk86 likes this.
  5. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    194
    Thanks very helpful :)