Communication between Scratch modifications can help mix and match the best features of each. For example, you can use BYOB and the Enchanting scratch modification together to implement recursion (BYOB feature) in a Lego Mindstorms robot program (Enchanting feature). Often, Scratch Modifications provide special blocks for communication with other programs.

Connecting

To communicate between modifications, Remote Sensor Connections must be enabled in all communicating modifications.

Communication is made up of two shared features:

Surprisingly, this is, in fact, all you need to write any connection-based program.

Setting up Protocols

Any network needs to have a protocol to limit information usage and introduce safety to the system. For example, the Hyper Text Transfer Protocol (HTTP) is widely used on the Internet. When connecting modifications, safety is not a major priority, as you are the only user. However, ease of programming, scope, and data usage and minimization are some key points to remember. Some tips are:

  • Begin each local broadcast (broadcasts which should be available only to the mod which owns it) with a prefix: <mod>-<broadcast>. For example: Scratch-movePlayer, BYOB-gameOver, Enchanting-moveRobot. This helps clear your global namespace.
Note Note: Do not use the broadcast Scratch-startclicked as it is a shortcut code to restart the project (emulate green flag press)


  • Similarly, begin each global variable with the same prefix.
  • In each project, maintain local variables, each setting its value to the global variables of the connected modification project. This frees up usage over the socket connection, thus maintaining efficiency and freeing up memory. The following scripts will help achieve that:
when gf clicked // in a modification
forever
if <not <(variable) = (oldVar)>> then // if the variable was changed
broadcast [varchanged v]
end
 set [oldVar v] to (variable)

when I receive [varChanged v] // in connected modification (e.g. Scratch)
set [Scratch-variable-copy v] to ([variable v] sensor value)

Complete Example

This example explains a way to implement an isNumber function in Scratch using a BYOB plug-in. You require two projects: plugin.ypr and connect.sb


plugin.ypr

when I receive [isNum-request v] // check if a string is a number
set [BYOB-to calculate v] to ([Scratch-toCalculate v] sensor value)
set [Global-answer v] to <is (to calculate) a [number v]? :: operators>//block found in BYOB
broadcast [updateVars v] and wait

connect.sb

when gf clicked
ask [Enter a string to check if it is a number or not] and wait
set [Scratch-toCalculate v] to (answer)
broadcast [isNum-request v] and wait
say (Scratch-answer-copy)

when I receive [updateVars v]
set [Scratch-answer-copy v] to ([Global-answer v] sensor value)

Communicating Outside Modifications

You can communicate with other programs, such as an iPhone app or Wii remote with the Remote Sensors Protocol.