Discuss Scratch

ethanbros
Scratcher
41 posts

[Suggestion] The Label Block

label: [label1 v] :: grey
code goes here
more code
even more code
jump to [label1 v] :: grey
In most programming languages, there are locations in code that the interpreter can jump to called “labels”. Labels exist in lots of different languages, such as BASIC, Python, et cetera.

With labels, the interpreter can go anywhere in scripts; all running and non-running. Please note that this block should NOT be confused with the “forever” and “repeat” blocks, as this does not have a Boolean requirement.

EXAMPLE:
Let's say that we are creating a “pick a number” game. The criteria must be between 1 and 10. If it's out of this range, the question should be repromted. The very unoptimized solution would be:
when green flag clicked
broadcast [ask v]

when i receive [ask v]
ask [Pick a number between 1 and 10, please?] and wait
if <not <<(answer) > [0]> or <(answer) < [11]>>> then
broadcast [ask v]
stop [this script v]
end
say (answer)
Although this script DOES work, we can do better. We can merge the broadcast block and the stop block into a goto (jump to) block that goes to a label. Another advantage of this addon is that we can get rid of the broadcast block, which frees up good space (by not a whole lot but count the little things!)
when green flag clicked
label [label1 v] :: grey
ask [Pick a number between 1 and 10, please?] and wait
if <not <<(answer) > [0]> or <(answer) < [11]>>> then
jump to [label1 v] and terminate :: grey cap
end
say (answer)
In conclusion, there are advantages to putting in a label block. First, it brings the user one more step into real coding, and second, optimization can be taken even further with labels!
(However, there should NOT be a hat block for labels, otherwise, it'll just be a broadcast block.)

My account is nearing its finish.
It'll be great.
I promise.
ACE009
Scratcher
100+ posts

[Suggestion] The Label Block

That’s a nice way of formatting it (although you should pick a better name than label). What happens when someone does this though?
repeat (5)
...
label [label 1 v] :: control
...
end

jump to [label 1 v] :: control
Does it repeat 5 times, or does it execute once? (You can’t refrence other languages for that since repeat doesn’t exist in them.) Also, I’m assuming that label creation acts the same as broadcasts.

Last edited by ACE009 (May 16, 2018 21:11:23)


when this signature read :: events hat
if <I know what [kumquats] are :: sensing> then //false
have (Katty :: pen) [eat v] [nearby kumquats v] :: motion // Katty eats the kumquats for me, whatever they are
else
learn what [kumquats] are :: sensing // this block must be malfunctional
end
ACE009
Scratcher
100+ posts

[Suggestion] The Label Block

I thought of what is probably the best way to handle the problem above: the label block will save the current execution state of the script (the last time it was executed), such as how many repeats are left, and then load that when it is jumped to. This also solves for whenever there are multiple label declarations in the same script.

Another problem that I thought of with that solution is what happens if the block has never been executed yet?

when this signature read :: events hat
if <I know what [kumquats] are :: sensing> then //false
have (Katty :: pen) [eat v] [nearby kumquats v] :: motion // Katty eats the kumquats for me, whatever they are
else
learn what [kumquats] are :: sensing // this block must be malfunctional
end
kenny2scratch
Scratcher
500+ posts

[Suggestion] The Label Block

First of all,

ethanbros wrote:

In most programming languages, there are locations in code that the interpreter can jump to called “labels”. Labels exist in lots of different languages, such as BASIC, Python
Gotos and labels do not exist in Python.

Next, I'll explain why they don't exist in Python: they make code impossible to follow.

ACE009 wrote:

What happens when someone does this though?
repeat (5)
...
label [label 1 v] :: control
...
end

jump to [label 1 v] :: control cap
Does it repeat 5 times, or does it execute once?
This is a very valid question. My intuitive answer is as follows:
repeat (5)
do first thing // repeats 5 times
label [label 1 v] :: control
do second thing // repeats 6 times
end
jump to [label 1 v] :: control cap
However, it's not immediately obvious why that's the case - so it makes bugs much harder to find.

Your use case:
when gf clicked
label [label 1 v] :: control
ask [Pick a number between 1 and 10] and wait
if <<(answer) < [1]> or <(answer) > [10]>> then
jump to [label 1 v] :: control cap
end
say (answer)
Can be replaced with this, too:
when gf clicked
repeat until <not <<(answer) < [1]> or <(answer) > [10]>>>
ask [Pick a number between 1 and 10] and wait
end
say (answer)
See how simple that is? (In other languages, that wouldn't be nearly as simple. Take Python for an example:
answer = 0
while answer < 1 or answer > 10:
    answer = int(input('Pick a number between 1 and 10: '))
print(answer)
Python needs a redundant declaration of “answer”. Scratch does not, because all variables are pre-declared with values at the start of the program. Thus Scratch simplifies this not insignificantly.)

post brought to you by the preview links bug and previously the uploads site bug. 看一下中文 Scratch 維基想參加?請參考這頁
Join the Scratch Wiki!
Made by Scratchers, for Scratchers, since December 6, 2008

LuckyLucky7
Scratcher
1000+ posts

[Suggestion] The Label Block

kenny2scratch wrote:

First of all,

ethanbros wrote:

In most programming languages, there are locations in code that the interpreter can jump to called “labels”. Labels exist in lots of different languages, such as BASIC, Python
Gotos and labels do not exist in Python.
But Sublime Text 3 does have gotos, but I don't know about labels. Also, it depends on what version of python it is and what programming editor is being used.

I have about 3450 posts, 90 shared projects, 180 total created/followed topics, and 425 followers.

kenny2scratch
Scratcher
500+ posts

[Suggestion] The Label Block

LuckyLucky7 wrote:

kenny2scratch wrote:

First of all,

ethanbros wrote:

In most programming languages, there are locations in code that the interpreter can jump to called “labels”. Labels exist in lots of different languages, such as BASIC, Python
Gotos and labels do not exist in Python.
But Sublime Text 3 does have gotos, but I don't know about labels. Also, it depends on what version of python it is and what programming editor is being used.
Sublime Text is an editor, not a programming language. Python has never had gotos (no matter what version). The programming editor does not affect the language.

post brought to you by the preview links bug and previously the uploads site bug. 看一下中文 Scratch 維基想參加?請參考這頁
Join the Scratch Wiki!
Made by Scratchers, for Scratchers, since December 6, 2008

ethanbros
Scratcher
41 posts

[Suggestion] The Label Block

ACE009 wrote:

That’s a nice way of formatting it (although you should pick a better name than label). What happens when someone does this though?
repeat (5)
...
label [label 1 v] :: control
...
end

jump to [label 1 v] :: control
Does it repeat 5 times, or does it execute once? (You can’t refrence other languages for that since repeat doesn’t exist in them.) Also, I’m assuming that label creation arcs the same as broadcasts.

Jumping from a “jump to” block to a label block should escape from the repeat block's cycle, such as what BASIC does when you enter a GOTO command in a FOR (repeat) command. If there's no label block intact, the interpreter should ignore it and go on without jumping anywhere, such as what Scratch does when the user tries to divide by 0.

My account is nearing its finish.
It'll be great.
I promise.
GiggyMantis
Scratcher
65 posts

[Suggestion] The Label Block

No support. It doesn't match the same style as the Scratch palette.

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
ethanbros
Scratcher
41 posts

[Suggestion] The Label Block

GiggyMantis wrote:

No support. It doesn't match the same style as the Scratch palette.
I made the block gray because I really do not know where Scratch would put it: Control? Or operators?
Also, just giving a first glance on the block and making a comment about it is judging a book by its cover, and that is NOT a good habit to get into.

My account is nearing its finish.
It'll be great.
I promise.
_nix
Scratcher
1000+ posts

[Suggestion] The Label Block

ethanbros wrote:

Also, just giving a first glance on the block and making a comment about it is judging a book by its cover, and that is NOT a good habit to get into.
Ehh, I agree with your point that you made it grey, because you weren't sure what color works best. (I suggest control, since it controls the flow of the program, like “repeat” and “if” blocks.) But judging a block on how it looks does make sense a bit, at least when you're doing it constructively. For example, I could tell you that it saying “label” doesn't make sense, because “label” is an ambiguous word. It could mean drawing text with the pen, or it could look like an in-script code comment; I think most people wouldn't guess that it means a place you can make the execution of the program jump to using a “jump to” block. Same deal with the “jump to” block – to most Scratch users, that would probably look like a block that animates the sprite jumping to a position! Which obviously is not the block that you're suggesting here.

So as we see, in this case, the block is a bit of a different style from other blocks; other blocks mostly have obvious meanings, but this one doesn't. Another difference between this block and most others – “label (my label)” is kind of like an embedded “when I receive block”. It doesn't do anything immediately, unlike every other stack block. The idea that a block doesn't immediately do anything at all might confuse users a bit.

══ trans autistic lesbian enbydoggirls // 16 17 18 19 20, she/they
sparrows one word to the paragraph // <3 // ~(quasar) nebula
miniepicness
Scratcher
1000+ posts

[Suggestion] The Label Block

goldfish678
Scratcher
1000+ posts

[Suggestion] The Label Block

miniepicness wrote:

I already suggested jumps
can you provide a forum thread link?

for the actual suggestion, this could be a nice feature. i've used it in batch before, and it makes things much easier in that context, but i wouldn't be entirely sure about scratch. as far as kids' programming languages go, this can be a pretty advanced control flow concept (i do agree that it would go under “control”), and i just don't know that it would work well with scratch's already existing control functions. i'd have to do further research, though.

Powered by DjangoBB