I want to make a block that takes a variable as input and sets it internally. I've tried:
{scale{(var λ)::@addInput ring grey} by ((value)::variables)::variables}::hat control
set [var v] to ((var)*(value)) but
set [epic variable v] to (3)
scale {(epic variable)::ring grey} by (2) ::variables
//Just throws an error. How would I actually make a pointer?
You have to use the run block to set the variable, and call the ring to get the value.
You beat me too it and a much neater method
qw23
November 8, 2023, 9:30pm
4
[Edit ] In each of the above solutions the “variable” is an “Any (evaluated)” a Reporter input type. This requires a grey ring around the variable when calling the function. You can also use the “Any (evaluated)” type, as I will use below (no grey ring). [end of Edit ]
Generalization
One step beyond the “scale” block (which is similar to but for the specific mathematical operation) is:
Application example
I actually wanted it for my own implementation of the for loop. The scale was just for simplicity's sake:
dardoro
November 9, 2023, 12:51am
6
Do you know the concept of the upvar formal parameters? Ad'hoc, local variable, which can be set inside the function but is visible to the outer world.
I saw that. I can think of some uses...
Actually I used the reporter grey ring input type.
qw23
November 9, 2023, 9:25am
9
My mistake. I corrected it.
qw23
November 9, 2023, 9:34am
10
Just for fun I refactored several Snap! primitives some time ago. Below is my interpretation of the FOR-loop, with an upvar (i) of course:
dardoro
November 9, 2023, 9:43am
11
After dropping the variable, the gray ring disappears and must be manually recreated...
bh
November 9, 2023, 11:47am
12
I hadn't thought to use the SIGN function to set the increment! That's very elegant.
But instead of testing which tester to use each time through the loop, you could put that in a variable, too:
Maybe you already know how to do this and didn't for some reason, but instead of adding a loop arrow as title text I used the official loop arrow feature:
which puts the arrowhead at the right end of the actual block.
qw23
November 9, 2023, 12:38pm
13
Thx!
Agreed, much better.
I wasn't aware of the feature, even though I must have seen the tick box many times.
I'm truly looking forward to the results of Jens' project, coding Snap! primitives using Snap! itself!
How can i do if the var is a list ?
(change a list with a command block)(not a reporter...)
qw23
November 9, 2023, 12:47pm
15
Can you clarify the question, perhaps with an example?
maybe This will work:
run {{replace item (1) of [ v] with (variable)}@addInput::reporter grey} with input (listVar)::control (Sorry for the weirdness with the ""span class". The forum is interpreting the image's scratchblocks as a mention.) Here are the scratchblocks:
run {{replace item (1) of [ v] with (variable)}@addInput::reporter grey} with input (listVar)::control
You can try it on the [url=Discuss Scratch ]To see