0 Favourites

How do I send a private message in a chat room with "/pm"?

  • I currently have been working on the challenge in the chat room tutorial.

    I have two local variables named PMName and PMChat that are set to the user and the message respectively.

    I have it set up so when the user messages "/pm user Hello!" It uses --

    send "" tag "private" message "<pm from " & PMName & ">: " & PMChat

    This sends it to the host, and I can confirm that the host definitely receives it correctly because it displays it in his chat. However, I can't figure out how to get the host to know which peer to send it to so it is displayed in their chat as well. Any tips?

  • The simplest option would be to not send it to the host but rather to the peer you actually want to PM.

    To do this, you need to know the Peer's ID. But the ID is easy to get if you know his Multiplayer Alias.

    Just use Multiplayer.PeerIDFromAlias(PeerName).

    Now you just need to add an event to the peer group that handles messages with the tag "private" as private messages.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • From my understand from the manuals and a lot of other threads -- You can't send a message directly to another peer unless you are the host.

  • You are actually right. I was mistaken.

    I used the Multiplayer object in many games, but never encountered this issue...

    Anyways, let's solve this with the host then.

    If you want to message a peer, you will need to add his ID to the message you are sending to let the host know who has to receive the message.

    There are multiple ways to do this:

    Option 1 | Adding the ID to the message:

    Add the ID after the message and separate them with escaping character.

    The host would then need to parse that message (e.g. with Regex) and send the appropriate message to the peer.

    This could look like this:

    Peer who wants to PM:

    Send message "pm":  [message] & "//" & [recipientID] (to host)[/code:16mfd0o6]
    [b]Host:[/b]
    The host needs to parse the message.
    [code:16mfd0o6]On peer message "pm":
    --- Send message {
    ----------- Peer ID:     RegexMatchAt(Multiplayer.Message, "\d+\D+\/{2}(\d+\D+)","",0)
    ----------- Message:  RegexMatchAt(Multiplayer.Message, "(\d+\D+)\/{2}\d+\D+","",0)
    --- }[/code:16mfd0o6]
    (I won't explain Regex to you, there are many tutorials out there, if you want to learn it. If you don't want to learn it or dislike Regex in general, see Option 2)
    
    [u]Option 2, using a Dictionary[/u]
    This option uses a simple 2-key dictionary that contains the recipient's ID and the message in two separate keys.
    The peer will send this dictionary as JSON to the host who will then load the JSON into his own dictionary to easily parse its contents to send the PM.
    
    This could look like this:
    
    [b]Peer who wants to PM:[/b]
    [code:16mfd0o6]Dictionary "pm": Clear
    Dictionary "pm" : Add key "ID" with value "[recipientID]"
    Dictionary "pm": Add key "msg" with value "[message]"
    Send message tag "pm" with content [Dictionary]pm.AsJSON (to host)[/code:16mfd0o6]
    [b]Host:[/b]
    [code:16mfd0o6]On message "pm":
    -- Dictionary "pm": Load from JSON [Multiplayer.Message]
    -- Send message  {
    -------- ID: [Dictionary]pm.Get("ID")
    -------- Message: [Dictionary]pm.Get("msg")
    -- }[/code:16mfd0o6]
    
    ___________________________________
    
    I'm not 100% sure about the Regex method, it [i]should[/i] work though.
    The dictionary method will definitely work, if executed correctly.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)