mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-06-26 22:30:27 -04:00
Update sensing_of fields if variable gets renamed
Thanks @adroitwhiz and @fsih for some advice!
This commit is contained in:
parent
7af161f1a2
commit
03db30d400
3 changed files with 183 additions and 1 deletions
src/engine
|
@ -946,6 +946,32 @@ class Blocks {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update sensing_of blocks after a variable gets renamed.
|
||||
* @param {string} oldName The old name of the variable that was renamed.
|
||||
* @param {string} newName The new name of the variable that was renamed.
|
||||
* @param {string} targetName The name of the target the variable belongs to.
|
||||
* @return {boolean} Returns true if any of the blocks were updated.
|
||||
*/
|
||||
updateSensingOfReference (oldName, newName, targetName) {
|
||||
const blocks = this._blocks;
|
||||
let blockUpdated = false;
|
||||
for (const blockId in blocks) {
|
||||
const block = blocks[blockId];
|
||||
if (block.opcode === 'sensing_of' &&
|
||||
block.fields.PROPERTY.value === oldName &&
|
||||
// If block and shadow are different, it means a block is inserted to OBJECT, and should be ignored.
|
||||
block.inputs.OBJECT.block === block.inputs.OBJECT.shadow) {
|
||||
const inputBlock = this.getBlock(block.inputs.OBJECT.block);
|
||||
if (inputBlock.fields.OBJECT.value === targetName) {
|
||||
block.fields.PROPERTY.value = newName;
|
||||
blockUpdated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return blockUpdated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to retrieve a costume menu field from a block given its id.
|
||||
* @param {string} blockId A unique identifier for a block
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue