Discuss Scratch

50_scratch_tabs
Scratcher
500+ posts

I made this TW extension that should already exist, what do you guys think?

I made an extension that allows you to drop reporters in the variables blocks. I wish this were already in scratch 3, but whatever.
It's basically the difference between:
set [variable v] to [0]
set (variable v) to [0]
(Makes more sense if you view on Scratchblocks homepage due to being Scratchblocks 2 used trying to explain a Scratch 3 thing)

This kind of already exists in the variables and lists extension, but it's nice to have it look exactly like the normal variables section…less intimidating. Also the show/hide variable thing doesn't exist in that one.

Here is the code. It must run as unsandboxed, so fire up turbowarp, click add extension, scroll down to custom extensions, click the text tab, paste the code below, and MAKE SURE RUN WITHOUT SANDBOX IS CHECKED!
Here's the code for you.
(function(Scratch) {
'use strict';

if (!Scratch.extensions.unsandboxed) {
throw new Error('This variables extension must run unsandboxed');
}

class CustomVariables {
constructor() {
this.hide=false;
}
buildVarMenu() {
let itms=[];
Scratch.vm.runtime.targets.forEach(function (i) {
Object.entries(i.variables).forEach(function (j) {
itms.push(i.variables[j[0]].name.toString());
})});
return itms;
};
getInfo() {
this.info = {
id: 'getdynamicnamedvariable',
name: 'Dynamically-named variables',
color1: '#FF8C1A',
color2: '#FF8C1A',
blocks: [
{
opcode: 'getVarNamed',
blockType: Scratch.BlockType.REPORTER,
text: 'Get variable named [NAME]',
arguments: {
NAME: { type: Scratch.ArgumentType.STRING, menu:"VARNAMEMENU" }
}
},
{
opcode: 'setVarNamed',
blockType: Scratch.BlockType.COMMAND,
text: 'set [NAME] to [VALUE]',
arguments: {
NAME: { type: Scratch.ArgumentType.STRING, menu:"VARNAMEMENU" },
VALUE: { type: Scratch.ArgumentType.STRING, defaultValue:"0"}
}
},
{
opcode: 'changeVarNamed',
blockType: Scratch.BlockType.COMMAND,
text: 'change [NAME] by [VALUE]',
arguments: {
NAME: { type: Scratch.ArgumentType.STRING, menu:"VARNAMEMENU" },
VALUE: { type: Scratch.ArgumentType.NUMBER, defaultValue:"1"}
}
},
{
opcode: 'showVarNamed',
blockType: Scratch.BlockType.COMMAND,
text: 'show variable [NAME]',
arguments: {
NAME: { type: Scratch.ArgumentType.STRING, menu:"VARNAMEMENU" }
}
},
{
opcode: 'hideVarNamed',
blockType: Scratch.BlockType.COMMAND,
text: 'hide variable [NAME]',
arguments: {
NAME: { type: Scratch.ArgumentType.STRING, menu:"VARNAMEMENU" }
}
}
],
menus: { VARNAMEMENU: {
acceptReporters: true, items:"buildVarMenu"}}
};
return this.info;
}
getVarNamed(args,util) {
const variable = util.target.lookupVariableByNameAndType(args.NAME.toString(), "");
if (!variable) { return "";};
return variable.value;
}
setVarNamed(args,util) {
const variable = util.target.lookupVariableByNameAndType(args.NAME.toString(), "");
vm.setVariableValue(util.target.id,variable.id,args.VALUE)
}
changeVarNamed(args,util) {
const variable = util.target.lookupVariableByNameAndType(args.NAME.toString(), "");
vm.setVariableValue(util.target.id,variable.id,this.getVarNamed(args,util)+args.VALUE)
}
changeMonitorVisibility (id, visible) {
// Send the monitor blocks an event like the flyout checkbox event.
// This both updates the monitor state and changes the isMonitored block flag.
Scratch.vm.runtime.monitorBlocks.changeBlock({
id: id, // Monitor blocks for variables are the variable ID.
element: 'checkbox', // Mimic checkbox event from flyout.
value: visible
}, this.runtime);
}
showVarNamed (args,util) {
this.changeMonitorVisibility(util.target.lookupVariableByNameAndType(args.NAME.toString(),"").id,true);
}
hideVarNamed (args,util) {
this.changeMonitorVisibility(util.target.lookupVariableByNameAndType(args.NAME.toString(),"").id,false);
}
refresh(shouldagain=true) {
this.hide=!this.hide;
let x= Scratch.vm.extensionManager.refreshBlocks()
if (shouldagain) {x.then((e) => {this.refresh(false);},this)}
}
}
Scratch.extensions.register(new CustomVariables());
})(Scratch);

Highlight this text and Shift+Down for more fun!

If my post is helpful and/or inspirational, please consider following me. If not, please consider following me.
I make awesome stuff in Python, Scratch, and PHP. If I'm feeling patient, I create decent stuff in C/C++. I create awesome stuff in Minecraft, but I don't care about making it look cool.
I don't mind if you post off-topic replies on my profile, but please don't post links to posts you think I missed. I follow every topic I post in, and I'm often too busy to be on Scratch.

I used to have the 9000th post on TOLORS.
My first post was an accident
Interesting comment thread


Tic-tac-toe :: events hat
(1::grey) (X:: variables) (3::grey):: stack grey
(4::grey) (O:: sensing) (6::grey):: stack grey
(X:: variables) (O:: control) (X :: variables):: stack grey

Colors :: events hat
50_scratch_tabs \(me) :: variables
8to16 :: sensing
SaberDragon73 :: control
Comment a move on my profile and I will add it.




If you have a signature that requires Shift+down, paste this at the bottom of it. If you actually read this far, you deserve my respect. -50_scratch_tabs

Powered by DjangoBB