kyle
kyle3w ago

Getting global ID of other player

I'm trying to broadcast a message via RealtimeModelEvent from one player to another. In the event handler, I only want the event handler of the receiving player to act on it. To do this, I'd like the event to include some kind of recipient ID, so that all clients who receive the evnet can check the recipient ID against their own ID. Is there some kind of ID I can use for this purpose which is consistent within a given room?
2 Replies
kapa.ai
kapa.ai3w ago
Hi @kyle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, you can use the client ID for this purpose, which is consistent within a given room in Normcore. When working with Normcore's RealtimeModel events, you can include the sender's client ID in your event data and have recipients check if they are the intended target. The client ID is a unique identifier assigned to each client in a room. Here's how you can implement this: 1. In your RealtimeModel, include properties for both the sender ID and recipient ID:
[RealtimeModel]
public partial class MessageModel {
[RealtimeProperty(1, true)] private int _trigger;
[RealtimeProperty(2, true)] private int _senderID;
[RealtimeProperty(3, true)] private int _recipientID;
[RealtimeProperty(4, true)] private string _message;

public void SendMessage(int senderID, int recipientID, string message) {
this.trigger++;
this.senderID = senderID;
this.recipientID = recipientID;
this.message = message;
}

public delegate void MessageHandler(int senderID, int recipientID, string message);
public event MessageHandler messageReceived;

[RealtimeCallback(RealtimeModelEvent.OnDidRead)]
private void DidRead() {
if (messageReceived != null && trigger != 0)
messageReceived(senderID, recipientID, message);
}
}
[RealtimeModel]
public partial class MessageModel {
[RealtimeProperty(1, true)] private int _trigger;
[RealtimeProperty(2, true)] private int _senderID;
[RealtimeProperty(3, true)] private int _recipientID;
[RealtimeProperty(4, true)] private string _message;

public void SendMessage(int senderID, int recipientID, string message) {
this.trigger++;
this.senderID = senderID;
this.recipientID = recipientID;
this.message = message;
}

public delegate void MessageHandler(int senderID, int recipientID, string message);
public event MessageHandler messageReceived;

[RealtimeCallback(RealtimeModelEvent.OnDidRead)]
private void DidRead() {
if (messageReceived != null && trigger != 0)
messageReceived(senderID, recipientID, message);
}
}
2. When sending a message, you can get your local client ID from the Realtime instance:
// Get local client ID
int myClientID = realtime.clientID;

// Send message to specific client
messageModel.SendMessage(myClientID, targetClientID, "Hello!");
// Get local client ID
int myClientID = realtime.clientID;

// Send message to specific client
messageModel.SendMessage(myClientID, targetClientID, "Hello!");
3. When receiving a message, check if you're the intended recipient:
messageModel.messageReceived += (senderID, recipientID, message) => {
// Only process if I'm the recipient
if (recipientID == realtime.clientID) {
Debug.Log($"Received message from client {senderID}: {message}");
// Handle the message
}
};
messageModel.messageReceived += (senderID, recipientID, message) => {
// Only process if I'm the recipient
if (recipientID == realtime.clientID) {
Debug.Log($"Received message from client {senderID}: {message}");
// Handle the message
}
};
You can get information about who is in the room using RealtimeAvatarManager's avatars dictionary, which maps client IDs to their respective RealtimeAvatar components RealtimeAvatarManager. This approach ensures that while all clients receive the event (as Normcore broadcasts model changes to everyone), only the intended recipient will act on it. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@401595391549898754> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?