Eng:Communicating Between Scratch Modifications

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:
 * Broadcasts are shared between all connected modifications.
 * Global Variables are readable (but not writable) by all mods, via the Sensor value block

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  (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: . For example:  . This helps clear your global namespace.

when gf clicked // in a modification forever if > // if the variable was changed broadcast [varchanged v] end set [oldVar v] to (variable)
 * 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 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:  and

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 <(to calculate) is a [number v]?>//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.