Variables, lists & broadcasts
Variables and lists are defined per target. A variable on the
Stage is global (every sprite can see it); a variable on a
Sprite is local to that sprite — the same rule the Scratch editor
uses. Broadcasts are project-wide and live on the stage.
Variables
You work with variables by name; scratch4js manages the underlying ids.
setVariable is an upsert: if a variable with that name already exists its
value is replaced, otherwise a new one is created. Values may be a string, number
or boolean.
Lists
Lists work the same way, with array values:
setList replaces the whole list. To append, read, mutate and set again:
Broadcasts
Broadcast messages are project-wide and owned by the stage:
addBroadcast only adds the message if it doesn't already exist, so calling it
twice with the same name is safe.
Monitors
The on-stage watchers that display a variable or list are stored separately,
in project.monitors. scratch4js exposes that array raw —
setting a variable does not automatically create a monitor for it. Mutate
project.monitors directly if you need to add or tweak a watcher.