Possible to put variable directly into set()To()?

The general idea that you have to understand is that in Snap!, as in most programming languages, inputs to a procedure are evaluated before the procedure is called. (The fancy name for this rule is “applicative order.”) That is, when you say untitled script pic (7), the SAY block knows only that its input value is 5, not that there was a + block involved in its computation.

Now apply that idea to the SET block. If you say


the input untitled script pic (10) is evaluated before the RUN block is called. So if the (previous) value of A is 5, you’re effectively saying SET 5 TO 0, not SET A TO 0. But what SET wants as its first input is the variable A itself, not its value, and not its name as a text string either.

Now, in Snap! there’s a way to use a piece of code as a value, namely, you put it in a ring.
untitled script pic (11)


So this:

does what you want.

By contrast, the SET VAR library block wants a variable name, an ordinary text string, as its input. So neither untitled script pic (10) nor untitled script pic (15) will do; you need the word “a” as the input:
untitled script pic (16)

As it happens, Snap! provides a way to get the names of all the variables accessible to a script:

Bottom line: You have to think about applicative order evaluation, and you have to think about what kind of thing each of SET and SET VAR wants as input, and then you can work out the meaning of all your example scripts.

You may create dedicated custom blocks to work with the var references (rings)


These work by using the unevaluated inputs

Okay, that was unbelievably helpful. Really got a grip on some new concepts. Thank you!

Huh, interesting. Nice to have another option.

For anyone reading this for a solution: I solved it by putting a “ringified” set()To() block in run()WithInput() and using a ringified input variable in the second parameter. Here is a picture:

untitled script pic (8)

If you don’t understand why the variables in the list need to be ringified (as I did), take a look at @bh’s explanation.