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

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.

customMessagingManager.SendUnnamedMessage(writer);

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

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.

[quote=“CodeSmile”, post:2, topic: 907585]
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.
[/quote]

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?

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.com/netcode/current/components/networkmanager/index.html#connection-notification-manager-example

Note: Client connected events also run on the local client!

2 Likes

[quote=“CodeSmile”, post:4, topic: 907585]
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.com/netcode/current/components/networkmanager/index.html#connection-notification-manager-example

Note: Client connected events also run on the local client!
[/quote]

Thanks very helpful :slight_smile: