Discuss Scratch
- Discussion Forums
- » Suggestions
- » The "new thread" block
- CSwitch
-
Scratcher
100+ posts
The "new thread" block
I think we should have a new block that can run scripts inline, but not block the main script. Here's an example of what it could look like:
And here's an example of how it would work:
new thread {
} ::controlAnd here's an example of how it would work:
when flag clickedBasically, the first forever loop is ran, but while it is running, it immediately continues the script following it. The same goes for the repeat loop.
switch costume to (costume1 v)
new thread {
forever
glide (pick random (2) to (7)) secs to x: (10) y: (20)
end
} ::control
new thread {
repeat (3)
wait (1) seconds
next costume
end
} ::control
say [This is cool!] for (5) seconds
- ploppiedog
-
Scratcher
56 posts
The "new thread" block
Sorry, but I don't support.
Scratch is supposed to be for beginners, and the concept of threading is not only kinda complex but also can create so many possible bugs that it really isn't worth it.
For instance, many game engines that have multithreading have many limitations put on secondary threads.For example, many game engines don't allow you to move/modify objects on a secondary thread to prevent issues. This would be pretty hard to enforce in Scratch, because it is supposed to be an extremely fault tolerant engine (and moving/modifying sprites in Scratch is pretty important).
Also, it creates many issues, such as this:
a. The variable is changed before it is checked, and the sprite says “Yay!”
b. The variable is changed after it is checked, and the sprite says nothing.
This would be very confusing for new Scratchers, since they would be confused as to why the sprite sometimes says something and why it sometimes doesn't.
In conclusion, this would create too much confusion, and so I don't think it should be added.
Scratch is supposed to be for beginners, and the concept of threading is not only kinda complex but also can create so many possible bugs that it really isn't worth it.
For instance, many game engines that have multithreading have many limitations put on secondary threads.For example, many game engines don't allow you to move/modify objects on a secondary thread to prevent issues. This would be pretty hard to enforce in Scratch, because it is supposed to be an extremely fault tolerant engine (and moving/modifying sprites in Scratch is pretty important).
Also, it creates many issues, such as this:
when green flag clickedThis would create what is called a “race condition” in which 2 possible outcomes are possible:
set [yay v] to [0]
new thread{
set [yay v] to [1]
}::control
new thread{
if <(yay) = [1]> then
say [Yay!]
end
}::control
a. The variable is changed before it is checked, and the sprite says “Yay!”
b. The variable is changed after it is checked, and the sprite says nothing.
This would be very confusing for new Scratchers, since they would be confused as to why the sprite sometimes says something and why it sometimes doesn't.
In conclusion, this would create too much confusion, and so I don't think it should be added.
- I_wantasheep
-
Scratcher
1000+ posts
The "new thread" block
how is this dfferent from a custom block with run without screen refresh on?
- Za-Chary
-
Scratcher
1000+ posts
The "new thread" block
how is this dfferent from a custom block with run without screen refresh on?It's not meant to be an “all at once” block — it's meant to be a “run at the same time” block. A workaround for this block would be to use broadcasts and run multiple scripts at the same time with the same broadcast:
when green flag clicked
switch costume to [costume1 v]
broadcast [new thread v]
say [This is cool!] for (5) secs
when I receive [new thread v]
forever
glide (pick random (2) to (7)) secs to x: (10) y: (20)
end
when I receive [new thread v]
repeat (3)
wait (1) secs
next costume
end
That may be more cluttered though, depending on who you ask.
- CSwitch
-
Scratcher
100+ posts
The "new thread" block
how is this dfferent from a custom block with run without screen refresh on?Run Without Screen Refresh blocks don't allow for time-consuming actions, like wait for seconds or a forever loop. For example:
when flag clickedThe custom blocks will run, but it will cause a lot of lag because it tries to complete in one frame.
switch costume to (costume1 v)
move around ::custom
change costumes ::custom
say [This is cool!] for (5) seconds
define move around //without screen refresh
forever
glide (pick random (2) to (7)) secs to x: (10) y: (20)
end
define change costumes //without screen refresh
repeat (3)
wait (1) seconds
next costume
end
- CSwitch
-
Scratcher
100+ posts
The "new thread" block
The first “new thread” block will always run first, before the next one.when green flag clickedThis would create what is called a “race condition” in which 2 possible outcomes are possible:
set [yay v] to [0]
new thread{
set [yay v] to [1]
}::control
new thread{
if <(yay) = [1]> then
say [Yay!]
end
}::control
a. The variable is changed before it is checked, and the sprite says “Yay!”
b. The variable is changed after it is checked, and the sprite says nothing.
This would be very confusing for new Scratchers, since they would be confused as to why the sprite sometimes says something and why it sometimes doesn't.
- I_wantasheep
-
Scratcher
1000+ posts
The "new thread" block
then it's not simultaneousThe first “new thread” block will always run first, before the next one.when green flag clickedThis would create what is called a “race condition” in which 2 possible outcomes are possible:
set [yay v] to [0]
new thread{
set [yay v] to [1]
}::control
new thread{
if <(yay) = [1]> then
say [Yay!]
end
}::control
a. The variable is changed before it is checked, and the sprite says “Yay!”
b. The variable is changed after it is checked, and the sprite says nothing.
This would be very confusing for new Scratchers, since they would be confused as to why the sprite sometimes says something and why it sometimes doesn't.
- CSwitch
-
Scratcher
100+ posts
The "new thread" block
then it's not simultaneousIt actually is, because in this example:
when green flag clicked1. “yay” is set to 0
set [yay v] to [0]
new thread{
set [yay v] to [1]
}::control
new thread{
if <(yay) = [1]> then
say [Yay!]
end
}::control
2. “new thread” block reached
3. “yay” set to 1 and second “new thread” block reached
4. Check “yay” = 1, true
5. Say “Yay!”
Or in my example from earlier:
when flag clicked1. switch to costume “costume1”
switch costume to (costume1 v)
new thread {
forever
glide (pick random (2) to (7)) secs to x: (10) y: (20)
end
} ::control
new thread {
repeat (3)
wait (1) seconds
next costume
end
} ::control
say [This is cool!] for (5) seconds
2. “new thread” block reached
3. forever loop and second “new thread” block reached
4. glide block in forever loop and repeat loop and say “This is cool!” for the next 5 seconds
Last edited by CSwitch (Yesterday 13:22:31)
- I_wantasheep
-
Scratcher
1000+ posts
The "new thread" block
that does the same thing as the code without thread blocksthen it's not simultaneousIt actually is, because in this example:when green flag clicked1. “yay” is set to 0
set [yay v] to [0]
new thread{
set [yay v] to [1]
}::control
new thread{
if <(yay) = [1]> then
say [Yay!]
end
}::control
2. “new thread” block reached
3. “yay” set to 1 and second “new thread” block reached
4. Check “yay” = 1, true
5. Say “Yay!”
- CSwitch
-
Scratcher
100+ posts
The "new thread" block
that does the same thing as the code without thread blocksSee the second example I just added.
- I_wantasheep
-
Scratcher
1000+ posts
The "new thread" block
that one's a better examplethat does the same thing as the code without thread blocksSee the second example I just added.
- knowitall1237
-
Scratcher
100+ posts
The "new thread" block
Having it as a C block looks kind of cluttered. I think (maybe) a better way to implement it would be to have it as a normal block with a hat block “attached” to it, with a line connecting the two similarly to a comment.
Kind of like this (I can't 100% recreate my idea here)
Kind of like this (I can't 100% recreate my idea here)
when green flag clicked
new thread :: control // pretend there is a line between this block and the hat block
say [I'm spinning!] for (5) secs
:: hat control
forever
turn cw (15) degrees
end
- Discussion Forums
- » Suggestions
-
» The "new thread" block