Skip to content

swap-local-global: allow conversion between local and global across multiple sprites - #9075

Open
griffpatch wants to merge 1 commit into
ScratchAddons:masterfrom
griffpatch:pr/swap-local-global
Open

swap-local-global: allow conversion between local and global across multiple sprites#9075
griffpatch wants to merge 1 commit into
ScratchAddons:masterfrom
griffpatch:pr/swap-local-global

Conversation

@griffpatch

Copy link
Copy Markdown
Contributor

Overview

The swap-local-global addon now allows converting a variable between local and global even when it is used by multiple sprites. Previously this was blocked with an error message.

Changes

Global → Local: When converting a global variable to local on the current sprite, any other sprite that also uses that variable now automatically gets its own independent local copy with the same name and current value. All of those sprites' block references are remapped to their new local ID so nothing breaks.

Local → Global: When converting a local variable to global, any other sprite that happens to have a same-named local variable is automatically merged into the new global — its blocks are remapped to point at the global ID and its now-redundant local is deleted.

A small shared helper remapVariableReferencesInTarget was added to walk a target's block fields and update any field whose ID matches the old variable, used by both conversion paths.

Reason for changes

The previous behaviour blocked very common real-world cases. A game's score or lives variable is often shared between a player sprite and the stage. A student clicking "make local" on such a variable was met with an unhelpful error rather than a useful outcome. The new behaviour does what users intuitively expect: each sprite gets its own copy (or they all share the one global), with no manual cleanup needed.

Tests

Tested in Chrome:

  • Global → local on a variable used by 3 sprites: each sprite received an independent local copy; all scripts continued to work correctly
  • Local → global where 2 other sprites had a same-named local: both were merged into the global; block references updated correctly in all sprites
  • Single-sprite conversions (the original behaviour) are unchanged

…ultiple sprites

When converting global→local, create a local copy in each other sprite
that uses the variable so their blocks keep working.

When converting local→global, merge any same-named locals in other
sprites into the new global by remapping block references and deleting
the now-redundant locals.

@apple502j apple502j left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, should the code change sensing_of (for local->global?)

@griffpatch

griffpatch commented Jun 21, 2026 via email

Copy link
Copy Markdown
Contributor Author

@pufferfish101007 pufferfish101007 added type: enhancement New feature for the project scope: addon Related to one or multiple addons labels Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: addon Related to one or multiple addons type: enhancement New feature for the project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants