跳到主要内容

Events

VirtualMachine extends Node's EventEmitter. The editor and player listen to these events to keep the UI in sync, and you can too:

vm.on('PROJECT_CHANGED', () => {
console.log('the project was edited');
});

The VM re-emits most of the runtime's events under the same name, so you usually listen on vm. A few things fire only on vm.runtime; those are noted below. This list is drawn from scratch-vm/src/virtual-machine.js and scratch-vm/src/engine/runtime.js.

Playback and running

EventFires when
PROJECT_STARTThe green flag is pressed.
PROJECT_RUN_STARTThreads begin running this frame (was idle, now active).
PROJECT_RUN_STOPAll threads have stopped (was active, now idle).
PROJECT_CHANGEDThe project was edited in a way that affects serialization.
PROJECT_LOADEDA project finished loading. Fires on vm.runtime.
TURBO_MODE_ON / TURBO_MODE_OFFTurbo mode was toggled.
RUNTIME_STARTED / RUNTIME_STOPPEDThe runtime's step loop started or stopped.

Loading progress

EventPayload
LOAD_PROGRESS{stage, loaded, total} where stage is one of unzipping, parsing, checking, building, installing.
ASSET_PROGRESS(finished, total) as project assets download.

Targets, blocks, and the workspace

EventPayload
targetsUpdate{targetList, editingTarget}. The list of targets changed or the selection changed.
workspaceUpdateThe editing target's blocks, for rebuilding the block workspace.
MONITORS_UPDATEThe current monitor (stage watcher) state.
BLOCK_DRAG_UPDATE / BLOCK_DRAG_ENDA block is being dragged over the GUI / a drag finished.
VISUAL_REPORTA value to show as a bubble next to a clicked reporter.
SCRIPT_GLOW_ON / SCRIPT_GLOW_OFFA script started or stopped glowing.
BLOCK_GLOW_ON / BLOCK_GLOW_OFFA single block started or stopped glowing.
PROJECT_STOP_ALL, STOP_FOR_TARGETThe stop button was hit / one target was stopped. Fire on vm.runtime.

Extensions

EventFires when
EXTENSION_ADDEDAn extension's block category was registered. Payload is the category info.
EXTENSION_REMOVEDAn extension was removed.
EXTENSIONS_REORDEREDExtension order changed.
EXTENSION_FIELD_ADDEDAn extension registered a custom field type.
BLOCKSINFO_UPDATEAn extension's blocks were refreshed (for example after a locale change).
PERIPHERAL_LIST_UPDATE, USER_PICKED_PERIPHERAL, PERIPHERAL_CONNECTED, PERIPHERAL_DISCONNECTED, PERIPHERAL_REQUEST_ERROR, PERIPHERAL_CONNECTION_LOST_ERROR, PERIPHERAL_SCAN_TIMEOUTPeripheral scanning and connection lifecycle.

Settings that changed

These fire after the matching setter runs, so the UI can update:

EventPayload
RUNTIME_OPTIONS_CHANGEDThe current runtime options.
COMPILER_OPTIONS_CHANGEDThe current compiler options.
FRAMERATE_CHANGEDThe new frame rate.
INTERPOLATION_CHANGEDWhether interpolation is on.
STAGE_SIZE_CHANGED(width, height).
COMPILE_ERROR(target, error) when a script fails to compile.
HAS_CLOUD_DATA_UPDATEWhether the project uses cloud variables.
MIC_LISTENINGWhether the microphone is active.
LOCALE_CHANGEDThe new locale, after setLocale.

Blocks that ask the host something

Some blocks need the host UI to respond. These fire on vm.runtime:

  • SAY: a sprite says or thinks something (say/think blocks).
  • QUESTION: an ask and wait block is waiting for input. The host collects an answer and emits an ANSWER event on the runtime to unblock the script.

See also