Variable in List

I’m making a custom block and it needs the ability to change variables, but I can’t put the variable names inside the change variable by or set variable to blocks and that find of unavailability won’t help, in fact I can’t do anything about he custom block until they add it or somebody tells me a work-around

The first input slot in SET (and CHANGE) is a pulldown menu of all the variables available at that point in your program:
Google Chrome001

run({change [ v] by (9)}) w/ inputs [x]

Ok guys, here’s my new (and unfinished) block!

That's a very good first effort. There are two things you need to learn to make it perfect.

First, about the variable name. Your block will count up the variable named var itself, not the variable whose name you put in that input slot when you use your block. That is, your SET block says set var to... and so it sets var. The mechanism we supply to do what you're trying to do is called the upvar. Look at the FOR block in Control. It has that orange variable oval with i in it. The way to get such an orange variable in your block is, in the long-form input dialog, the one you use to set the input type, down at the bottom it says


and you click that upvar button.

The great magic of upvars is that the user of your block can click on the orange oval to change the name from var (or whatever you call it) to some other name, say foo, and then the same variable is called foo outside your block, in the caller's script, but still called var inside your block, so you can compute with it exactly as you do in your code.

Upvars are one of the small innovations in Snap! that Jens and I are really proud of.

The second thing is that you can't put that REPORT block inside the REPEAT UNTIL loop. REPORT ends your custom block, because a block can report only one value. (That's why, if you look closely, there's no tab at the bottom of REPORT to connect it to another block below it.) You could put the REPORT below the loop.

... except that if your block is going to report a value, you have to create it as a reporter, not a command:

Thx I'll try the 1st one but the only reason I put a report block in is because I thought if someone where to rinfigy the block it would output the report instead of a picture of the script, but if the variable input whould only be "var" then whats the point of the menus?

I'm not sure I understand what you're asking. You mean the menus in SET and CHANGE? In your code, those menus would include var, from, to, count, script inside, plus any global variables in the project.

No, a ring around a script or an expression means that the script itself, or the expression itself, is the desired value. (It's not "a picture of" the script; it's the actual runnable script itself.)

No I meant these

Screenshot 2020-11-01 at 2.41.38 AM

On the bottom right of the input value menu

Oh ok

The variables menu in that list of menu options gives you the same thing that the SET block has: all the variables visible to this procedure, which means its own variables and the global ones.

So are you saying that the menu on the block itself

Screenshot 2020-11-01 at 2.56.13 AM

Has global AND local variables?

Yes. :~)

Also I took your advice about the block being a reporter since you can probably put it in a run block

Oh ya I forgot, the “var” input won’t go in the variable block so I left it until I find a solution

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.