Discuss Scratch

1132262
Scratcher
1000+ posts

JS Script to Change Scratch Variables from the Editor?

How can I run a script on the editor tab that changes a normal (non-cloud) Scratch variable?




Sheep_maker
Scratcher
1000+ posts

JS Script to Change Scratch Variables from the Editor?

Does it matter when you run the script? It could be fairly easy to redefine some JavaScript built-in method to gain access to the VM instance, but this must be done before Scratch loads (so you can't run it after)

- Sheep_maker This is a kumquat-free signature. :P
This, my signature, appears below all my posts. Discuss it on my profile, not the forums. Here's how to make your own.
.postsignature { overflow: auto; } .scratchblocks { overflow-x: auto; overflow-y: hidden; }
1132262
Scratcher
1000+ posts

JS Script to Change Scratch Variables from the Editor?

Sheep_maker wrote:

Does it matter when you run the script? It could be fairly easy to redefine some JavaScript built-in method to gain access to the VM instance, but this must be done before Scratch loads (so you can't run it after)
I think that would work.




Sheep_maker
Scratcher
1000+ posts

JS Script to Change Scratch Variables from the Editor?

This is from @apple502j, who credits @_nix for it:
const findVM = () => new Promise(resolve => {
    const oldBind = Function.prototype.bind;
    Function.prototype.bind = function(...args) {
        if (args[0] && args[0].hasOwnProperty('editingTarget') && args[0].hasOwnProperty('runtime')) {
            Function.prototype.bind = oldBind;
            resolve(args[0]);
        }
        return oldBind.apply(this, args);
    };
});
findVM().then(vm => {
    console.log('*** Found VM: ***');
    console.log(vm);
});
The VM has a setVariableValue method, but it requires a variable ID and target (sprite) ID. You can get the runtime instance by doing vm.runtime and then get the sprite using the runtime's getSpriteTargetByName method. From there, you can get the sprite's variable using lookupVariableByNameAndType, and I'm assuming you can set the value of the variable through the variable object already, but it might be more reliable to use setVariableValue and get the variable/target IDs from their respective instances

- Sheep_maker This is a kumquat-free signature. :P
This, my signature, appears below all my posts. Discuss it on my profile, not the forums. Here's how to make your own.
.postsignature { overflow: auto; } .scratchblocks { overflow-x: auto; overflow-y: hidden; }
MiniCoder11
Scratcher
56 posts

JS Script to Change Scratch Variables from the Editor?

Is this possible to achieve from a userscript running while the player is open? Could you possibly give an example script that would set a variable: “Foo”, in sprite: “Sprite” to some value?

Please let me know if there is anything else I can do to help

Weekly bulletin:
• Current Scratch 3.0 rating: | ★★★★☆ 4.0 |
PhysicsKit has been updated!
• Suggest ideas for Scratch Royale here!
Sheep_maker
Scratcher
1000+ posts

JS Script to Change Scratch Variables from the Editor?

MiniCoder11 wrote:

Is this possible to achieve from a userscript running while the player is open? Could you possibly give an example script that would set a variable: “Foo”, in sprite: “Sprite” to some value?
Yes, it's probably possible with a userscript, but no, I cannot give you an example script.

It also can't run while the player is open since by that point Scratch has loaded; it needs to be run before Scratch loads

- Sheep_maker This is a kumquat-free signature. :P
This, my signature, appears below all my posts. Discuss it on my profile, not the forums. Here's how to make your own.
.postsignature { overflow: auto; } .scratchblocks { overflow-x: auto; overflow-y: hidden; }
MiniCoder11
Scratcher
56 posts

JS Script to Change Scratch Variables from the Editor?

Sheep_maker wrote:

It also can't run while the player is open since by that point Scratch has loaded; it needs to be run before Scratch loads

In that case, I guess It isn't possible at the moment to do what I was hoping. It seems like there should be some way to change the value of a variable that is being stored locally on your computer's browser.

Please let me know if there is anything else I can do to help

Weekly bulletin:
• Current Scratch 3.0 rating: | ★★★★☆ 4.0 |
PhysicsKit has been updated!
• Suggest ideas for Scratch Royale here!
Boomer001
Scratcher
1000+ posts

JS Script to Change Scratch Variables from the Editor?

This can create a variable:
Blockly.getMainWorkspace().createVariable("variable name here")
This can rename a variable:
Blockly.getMainWorkspace().renameVariableById(Blockly.getMainWorkspace().getVariable("variable name here").getId(),"text to rename to here")
This can delete a variable:
Blockly.getMainWorkspace().deleteVariableById(Blockly.getMainWorkspace().getVariable("variable name here").getId())


???













(- ͜-) // This is mister sneaky. He projects my siggy against evil kumquats. 
// Do you see error 666 above? That was an evil kumquat. But mister sneaky turned him into error 666.
// His name is that because its at the bottom of my siggy; very sneaky...




















Be moist






















MiniCoder11
Scratcher
56 posts

JS Script to Change Scratch Variables from the Editor?

Boomer001 wrote:

This can create a variable:
Blockly.getMainWorkspace().createVariable("variable name here")
This can rename a variable:
Blockly.getMainWorkspace().renameVariableById(Blockly.getMainWorkspace().getVariable("variable name here").getId(),"text to rename to here")
This can delete a variable:
Blockly.getMainWorkspace().deleteVariableById(Blockly.getMainWorkspace().getVariable("variable name here").getId())

Thank you so much! Do you know what can set a variable?

Would it just be:

Blockly.getMainWorkspace().setVariableById(Blockly.getMainWorkspace().getVariable("Foo").getID())

Last edited by MiniCoder11 (May 22, 2020 19:33:51)


Please let me know if there is anything else I can do to help

Weekly bulletin:
• Current Scratch 3.0 rating: | ★★★★☆ 4.0 |
PhysicsKit has been updated!
• Suggest ideas for Scratch Royale here!
Boomer001
Scratcher
1000+ posts

JS Script to Change Scratch Variables from the Editor?

MiniCoder11 wrote:

Boomer001 wrote:

This can create a variable:
Blockly.getMainWorkspace().createVariable("variable name here")
This can rename a variable:
Blockly.getMainWorkspace().renameVariableById(Blockly.getMainWorkspace().getVariable("variable name here").getId(),"text to rename to here")
This can delete a variable:
Blockly.getMainWorkspace().deleteVariableById(Blockly.getMainWorkspace().getVariable("variable name here").getId())

Thank you so much! Do you know what can set a variable?

Would it just be:

Blockly.getMainWorkspace().setVariableById(Blockly.getMainWorkspace().getVariable("Foo").getID())
I haven't found a way yet… If there was, you could set a high score variable to a really high number, then a cloud variable saves that. That is hacking, which will probably result in an alert, or maybe a ban.


???













(- ͜-) // This is mister sneaky. He projects my siggy against evil kumquats. 
// Do you see error 666 above? That was an evil kumquat. But mister sneaky turned him into error 666.
// His name is that because its at the bottom of my siggy; very sneaky...




















Be moist






















MiniCoder11
Scratcher
56 posts

JS Script to Change Scratch Variables from the Editor?

Boomer001 wrote:

I haven't found a way yet… If there was, you could set a high score variable to a really high number, then a cloud variable saves that. That is hacking, which will probably result in an alert, or maybe a ban.

I’m not looking to change any cloud variables (I already know how to do that). I just want to know if there is a way for my userscript to set an “isOnMobile” variable to true or false.

Please let me know if there is anything else I can do to help

Weekly bulletin:
• Current Scratch 3.0 rating: | ★★★★☆ 4.0 |
PhysicsKit has been updated!
• Suggest ideas for Scratch Royale here!
Boomer001
Scratcher
1000+ posts

JS Script to Change Scratch Variables from the Editor?

MiniCoder11 wrote:

Boomer001 wrote:

I haven't found a way yet… If there was, you could set a high score variable to a really high number, then a cloud variable saves that. That is hacking, which will probably result in an alert, or maybe a ban.

I’m not looking to change any cloud variables (I already know how to do that). I just want to know if there is a way for my userscript to set an “isOnMobile” variable to true or false.
It could be that they are local JS variables, meaning it's hard to change them…

[offtopic]

Sheep_maker wrote:

This is from @apple502j, who credits @_nix for it:
-snip-
OMG! I can turn off compatibility mode with it! I can make projects run at 60 FPS!
[/offtopic]

Last edited by Boomer001 (May 23, 2020 09:57:18)



???













(- ͜-) // This is mister sneaky. He projects my siggy against evil kumquats. 
// Do you see error 666 above? That was an evil kumquat. But mister sneaky turned him into error 666.
// His name is that because its at the bottom of my siggy; very sneaky...




















Be moist






















Powered by DjangoBB

Standard | Mobile