A chatroom is a project where Scratchers (New Scratchers cannot use cloud data) can talk to each other in real time using cloud data. Due to the current restrictions on cloud data, creating a chatroom project is difficult and the potential is fairly limited.
|Warning:||No matter how close you feel to someone on Scratch, never share personal information (like your birth date, real name, password, where you live) or agree to meet up in public with them.|
Rules Governing Chatroom Projects
|This article or section needs additional citations for verification. Its information may not be accurate. Editors can help by adding references. (February 2018)|
Only "whitelist chats" (a.k.a. "safe chats") may be created, where users can choose from a preselected list of messages. Free chats or blacklist chats, where users can post any message or where certain specific words/phrases are not allowed but otherwise messages are unrestricted, respectively, are not allowed, as there is a significant potential for bullying (more so than other places since users think only a few people will see their messages) or personal information being revealed and it would be very difficult for the Scratch Team to moderate them.
|Note:||The Scratch Team does not encourage the creation of chat rooms. If someone needs to be told something, use the comments, which are usually much safer, and monitored more closely by the Scratch team.|
This tutorial makes a "safe chat" with the messages:
- How was your day?
This tutorial will assume several variables. These are:
One list called "Chat" will be used too. This list will show the chat log.
Sending a message
To send a message to the chat room, first add the message to the list "Chat".
define chat [msg] add (join (join (username)[:])(msg)) to [Chat v]
Next, encode the entire chat log to ☁data. This process runs through every character in every list item and encodes the character as a two-digit number, then add "00" to separate list items since there is no 0th character of a piece of text. Both define blocks need to have "run without screen refresh" checked when making them.
when gf clicked set [c v] to [abcdefghijklmnopqrstuvwxyz0123456789_-.?!:�] // These are all supported username characters + characters in our messages + colon for separation of user and message + one "unknown character" character for contingencies. define encode set [i v] to  set [☁data v] to  repeat (length of [Chat v]) change [i v] by (1) set [j v] to  repeat (length of (item (i) of [Chat v])) change [j v] by (1) set [k v] to  repeat until <<(letter (j) of (item (i) of [Chat v])) = (letter (k) of (c))> or <(k) > (length of (c))>> change [k v] by (1) end if <(k) > (length of (c))> then // if such a character is out of range of c... set [k v] to (length of (c)) // ...replace that letter with the "unknown character" character end if <(k) < > then // if the resulting number is less than two digits long... set [☁data v] to (join (☁data) (join (k))) // ...make it two digits by adding a 0 in front, e.g. 4 becomes 04 else set [☁data v] to (join (☁data) (k)) end end set [☁data v] to (join (☁data) ) end
Receiving a message
To see messages on the list that have been sent, decode ☁data into our list. This will take every two characters of ☁data and turn them back into a text character.
define decode set [i v] to  delete (all v) of [Chat v] add  to [Chat v] repeat ((length of (☁data)) / (2)) if <(join (letter (i) of (☁data))(letter ((i) + (1)) of (☁data))) = > then add  to [Chat v] else replace item (last v) of [Chat v] with (join (item (last v) of [Chat v])(letter (join (letter (i) of (☁data))(letter ((i) + (1)) of (☁data))) of (c)) end change [i v] by (2) end delete (last v) of [Chat v]
To make an interface to send and receive messages, simply always ask for a message, then send it. Preferably, this would be in the Stage.
when gf clicked forever ask [Message id here: 1. Hello! 2. Bye! 3. How was your day? 4. Fine.] and wait if <(answer) = > then chat [Hello!] :: custom end if <(answer) = > then chat [Bye!] :: custom end if <(answer) = > then chat [How was your day?] :: custom end if <(answer) = > then chat [Fine.] :: custom end encode :: custom // actually put the message in the cloud chat decode :: custom // verify that the message is there and view new messages end