13
\$\begingroup\$

Scratch is a visual programming language. For the sake of counting bytes, Scratch is usually written in scratchblocks2 syntax.

Post tips that specifically apply to Scratch or scratchblocks2.

\$\endgroup\$
1
  • \$\begingroup\$ A few people might also use the scoring here (in the question post itself), although the two scores definitely don't compete. BTW, Scratch 3.0 will be released in January, but the syntax, AFAICT, isn't going to change much. \$\endgroup\$ Commented Nov 9, 2018 at 12:44

5 Answers 5

4
\$\begingroup\$

Abuse the bool type

In Scratch the true and false values are equivalent to 0 and 1, so you can perform arithmetic operations over them. A practical example would be finding the sign of a number:

define f(n
set[r v]to(<(n)>(0)>-<(n)<(0
\$\endgroup\$
3
\$\begingroup\$

Use a Function if Possible

Where possible, if the challenge says functions are allowed, use them! The syntax for defining a function is almost always shorter than the usual when gf clicked.

For example:

define f(n
say((n)+(1

Over:

when gf clicked
ask()and wait
say((answer)+(1

This also means one doesn't have to use the answer variable, which is quite longer than n.

\$\endgroup\$
3
\$\begingroup\$

Alternatives to Ternary "if...else" operators

As of the time this answer is posted, Scratch lacks a ternary operator. If we had one, it'd have looked like this:

if else ternary

However, we can use a workaround with this:

enter image description here In sb syntax (22 bytes):

((x)*<>)+((y)*<not<>>)

Make sure to replace x and y with the values for when the boolean is true and when it's false. The booleans may be empty here, but when using it should have the same conditions.

Besides, this only works when x and y are numbers. Currently this fails with strings. See edit.

This also works because of https://codegolf.stackexchange.com/a/198744/110681

EDIT: you could use our invented ternary operator with a (item()of[ v]) to index a number into the list, depending on the truth value of the boolean.

EDIT: To get single character results, Jacob has commented on his version, which is 20 bytes (replace <> with your boolean):

letter((1)+<>)of[ab]

However, notice that this doesn't work for emojis or Unicode characters, and they will return if you try using them.

\$\endgroup\$
2
  • \$\begingroup\$ This can be extended to single-character strings: (letter(true)of[ab]) is a. Note that this doesn't work for emojis / unicode characters since they take up multiple chars in Scratch. \$\endgroup\$ Commented Mar 24, 2023 at 14:52
  • \$\begingroup\$ Sorry, I meant (letter((1)+<a_boolean>)of[ab]) to get either a or b. I needed to use emojis for this challenge -- see the second part of my Scratch answer. By "single character strings" I meant getting one character from a two-character string, sorry for poor wording. \$\endgroup\$ Commented Mar 25, 2023 at 14:16
2
\$\begingroup\$

Brackets auto-complete

In the scratchblocks3 visual syntax (backwards-compatible with scratckblocks2 syntax), you can auto-complete the brackets.

E.g. The hello, world program:

when gf clicked
say[hello, world]

Can be golfed into:

when gf clicked
say[hello, world

In addition, the end statement of switching structures is auto-completed at the end. So instead of

define f(n
set[i v]to(1
set[o v]to(
repeat(length of(n
repeat((2)-((i)mod(2
set[o v]to(join(o)(letter(i)of(n
end
change[i v]by(1

You could do (-4 bytes)

define f(n
set[i v]to(0
set[o v]to(
repeat(length of(n
change[i v]by(1
repeat((2)-((i)mod(2
set[o v]to(join(o)(letter(i)of(n
\$\endgroup\$
1
\$\begingroup\$

Output via a variable or list, not say

The previous consensus for output in Scratch was that you had to say the output. However, this doesn't really make sense, since variables are automatically displayed in Scratch if not unchecked in the interface, just as say is automatically displayed if the sprite is not checked as hidden.

Now, per this consensus, it's allowed to just output by writing a global variable, which can in some cases save many bytes.

For example, this (meaningless) code:

repeat(10
change[i v]by(1
if<(i)>5>then
set[a v]to((i)*(2
end
end
say(a

Can become just:

repeat(10
change[i v]by(1
if<(i)>5>then
set[a v]to((i)*(2
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.