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

Get help using Construct 2

Post » Tue Dec 20, 2016 5:29 am

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?
B
8
S
1
Posts: 4
Reputation: 450

Post » Tue Dec 20, 2016 2:44 pm

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.
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

Post » Tue Dec 20, 2016 4:04 pm

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.
B
8
S
1
Posts: 4
Reputation: 450

Post » Tue Dec 20, 2016 5:26 pm

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:
Code: Select all
Send message "pm":  [message] & "//" & [recipientID] (to host)

Host:
The host needs to parse the message.
Code: Select all
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)
--- }

(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)

Option 2, using a Dictionary
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:

Peer who wants to PM:
Code: Select all
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)

Host:
Code: Select all
On message "pm":
-- Dictionary "pm": Load from JSON [Multiplayer.Message]
-- Send message  {
-------- ID: [Dictionary]pm.Get("ID")
-------- Message: [Dictionary]pm.Get("msg")
-- }


___________________________________

I'm not 100% sure about the Regex method, it should work though.
The dictionary method will definitely work, if executed correctly.
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091


Return to How do I....?

Who is online

Users browsing this forum: Ashley, Felipiwi and 22 guests