How to differentiate between who sent the messages in the photon chat other than usernames?

So I have created a chat system for Multiplayer and it is working fine but I want a change in my UI so that player recognizes that messages were sent by other user or not. I have attached a picture for ref. White background should be personal user while brown be other clients.

But i am not getting how to do that , any help would be appreciated...

Answers

  • Tobias
    Tobias admin

    This need to be done by sender id / username. There is nothing else to identify anyone.

  • @Tobias sir I don't get how would I access the senders User Id here.

    I am doing this to send message -

        public Text InstantiateMessageBox()
        {
            //if (chatClient.UserId == userName) // should i do something Like this??
            //if(view.IsMine)     // or like this
            {
                GameObject messageBox = Instantiate(userMessageBox, chatMessagePanel.transform);
                Text messageText = messageBox.GetComponentInChildren<Text>();
    
    
                return messageText;
            }
            else
            {
                GameObject messageBox = Instantiate(otherUserMessageBox, chatMessagePanel.transform);
                Text messageText = messageBox.GetComponentInChildren<Text>();
    
    
                return messageText;
            }
        }
    
    
        public void SendMsg()
        {
            chatClient.PublishMessage(region, messageField.text);
            messageField.text = "";
            //sRect.velocity = new Vector2(0f, 1000f);
        }
    
        public void OnGetMessages(string channelName, string[] senders, object[] messages)
        {
            for (int i = 0; i < senders.Length; i++)
            {
                Text message = InstantiateMessageBox();
                message.text += messages[i];
                sRect.velocity = new Vector2(0f, 1000f);
            }
        }
    
  • Tobias
    Tobias admin

    In OnGetMessages, there is a list of senders and corresponding messages...

  • After some more research I was able to make it work. Changes in the code block --

    public Text InstantiateMessageBox(string id)
        {
            if(chatClient.UserId == id)    
            {
                GameObject messageBox = Instantiate(userMessageBox, chatMessagePanel.transform);
                Text messageText = messageBox.GetComponentInChildren<Text>();
    
    
                return messageText;
            }
            else
            {
                GameObject messageBox = Instantiate(otherUserMessageBox, chatMessagePanel.transform);
                Text messageText = messageBox.GetComponentInChildren<Text>();
    
    
                return messageText;
            }
        }
    
    public void OnGetMessages(string channelName, string[] senders, object[] messages)
        {
            for (int i = 0; i < senders.Length; i++)
            {
                Text message = InstantiateMessageBox(senders[i]);
                message.text += messages[i];
                sRect.velocity = new Vector2(0f, 1000f);
            }
        }
    
    
  • tleylan
    tleylan ✭✭

    Hi Darshan... I realize that you didn't ask but I thought you (and others) might like to see common optimizations. Blocks of repeated code are one of the common places bugs can occur.

    public Text InstantiateMessageBox(string id)
    {
    	var msgBox = (chatClient.UserId == id)? userMessageBox : otherUserMessageBox;
    
    	var messageBox = Instantiate(msgBox, chatMessagePanel.transform);
    	return messageBox.GetComponentInChildren<Text>();
    }
    
  • @tleylan thanks for the optimization bro.