scratchfoundation / scratch-vm

Virtual Machine used to represent, run, and maintain the state of programs for Scratch 3.0
http://scratchfoundation.github.io/scratch-vm/
BSD 3-Clause "New" or "Revised" License
1.21k stars 1.51k forks source link

Copying and pasting (⌘C / ⌘V) blocks between sprites can cause local variables to share the same ID. #3963

Open Wowfunhappy opened 1 year ago

Wowfunhappy commented 1 year ago

Expected Behavior

No two distinct variables in a given project should have the same ID.

Actual Behavior

Two different local variables in different sprites can share a single ID. This interferes with monitors. Although each sprite can independently access and modify their own copy of the variable (changing the variable in one sprite doesn't change it in the other sprite), the user will only be able to show one of the two variable monitors.

Steps to Reproduce

  1. Create a new project
  2. Create a variable "for this sprite only"
  3. Drag a block which uses your new variable (e.g. set foo to 0) onto the workspace.
  4. Add a second sprite.
  5. Select your first sprite. Click the block you added to the workspace in step 3, and copy it to your clipboard with ⌘C. (Presumably this would be ctrl+C on Windows/Linux?)
  6. Select your second sprite. Paste the block you copied with ⌘V (or ctrl+V, etc).

Operating System and Browser

Mac OS X, Chromium 111.


This may be a duplicate of https://github.com/LLK/scratch-vm/issues/2429, but that issue seems to be caused by some sort of race condition in very large projects . By contrast, the problem described above can be easily replicated in projects of any size, and I've had it come up frequently while teaching students in schools.

Notably, if you drag your block between sprites (share the love) instead of using ⌘C and ⌘V, the new local variable will be created properly. It's merely copying and pasting that causes problems.

Where is the code responsible for fixing variables that are dragged between sprites, and could that same code be applied to pasting?

P.S. See also: https://github.com/LLK/scratch-vm/issues/1766, https://github.com/LLK/scratch-vm/issues/1375

Wowfunhappy commented 1 year ago

Where is the code responsible for fixing variables that are dragged between sprites, and could that same code be applied to pasting?

Okay, so I've at least found the answer to that question:

https://github.com/LLK/scratch-gui/pull/2619/commits/07a679d19e50a21f5c78f139fe8ecdddf3529788 https://github.com/LLK/scratch-vm/blob/develop/src/virtual-machine.js#L1240