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.2k stars 1.49k forks source link

Undefined variable appears when deleting variable and changing sprites #775

Closed fsih closed 6 years ago

fsih commented 6 years ago

Expected Behavior

No undefined variables

Actual Behavior

undefined variable appears

Steps to Reproduce

  1. Go to Data. Create a variable.
  2. Drag "set x to" block to workspace
  3. In the dropdown, delete variable You can also delete variables by right clicking on them in the flyout and choosing delete

all appears well

  1. Add a new sprite or change to the costume tab
  2. Return to the first sprite or the blocks tab

There is now a variable called "undefined"

Operating System and Browser

Windows 10 Chrome

fsih commented 6 years ago

From @paulkaplan: The reason this is happening is because the monitored block for the variable reporter is not being deleted when the variable is deleted. The VM is actually running the opcode for getVariable, which runs a lookupOrCreateVariable, emphasis on the Create part. The variable was deleted but then re-created. vm/scratch3_data.js only passes the variable ID to lookupOrCreateVariable, which is what causes the variable name to appear as undefined.