Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » "More Blocks" Function Bugged
- EatLikeInsects
-
New Scratcher
26 posts
"More Blocks" Function Bugged
Because you are using a “wait until” then it wont update after you unpress it as it had already executed. You need to use a forever, if (key space pressed) then to get it to work right
- DadOfMrLog
-
Scratcher
1000+ posts
"More Blocks" Function Bugged
OK, let me add a little to the script and ask a slightly different question…
wait until <key space pressed>
set spacecheck to <key space pressed>
say “Space key is pressed”
repeat until <spacecheck = false> {
wait 0.5 seconds
}
say “Done…”
Once the space key is pressed the sprite will say “Space key is pressed”, but will it ever get to the point where it says “Done…”?
wait until <key space pressed>
set spacecheck to <key space pressed>
say “Space key is pressed”
repeat until <spacecheck = false> {
wait 0.5 seconds
}
say “Done…”
Once the space key is pressed the sprite will say “Space key is pressed”, but will it ever get to the point where it says “Done…”?
- EatLikeInsects
-
New Scratcher
26 posts
"More Blocks" Function Bugged
OK, let me add a little to the script and ask a slightly different question…
wait until <key space pressed>
set spacecheck to <key space pressed>
say “Space key is pressed”
repeat until <spacecheck = false> {
wait 0.5 seconds
}
say “Done…”
Once the space key is pressed the sprite will say “Space key is pressed”, but will it ever get to the point where it says “Done…”?
It can't because it only checks for when you press the space key the first time and sets spacecheck to that time ONLY.and it will repeat until something that will never be false as you do not use a forever, if () then script.
- DadOfMrLog
-
Scratcher
1000+ posts
"More Blocks" Function Bugged
OK, good. Now let's extend the script a bit more…
forever {
if <key space pressed> then {
set boolean1 to <key space pressed>
say “Space key is pressed”
repeat until <boolean1 = false> {
wait 0.5 seconds
}
say “Done…” for 2 seconds
}
}
So now I have put “forever” around it all, and used “if <key space pressed> then…” (and changed “spacecheck” to “boolean1”).
This will still get stuck at the “repeat until <boolean1 = false>”, though - you can see that the part within the forever loop is still the same as before, so doing it like this still won't get it unstuck, right?
forever {
if <key space pressed> then {
set boolean1 to <key space pressed>
say “Space key is pressed”
repeat until <boolean1 = false> {
wait 0.5 seconds
}
say “Done…” for 2 seconds
}
}
So now I have put “forever” around it all, and used “if <key space pressed> then…” (and changed “spacecheck” to “boolean1”).
This will still get stuck at the “repeat until <boolean1 = false>”, though - you can see that the part within the forever loop is still the same as before, so doing it like this still won't get it unstuck, right?
Last edited by DadOfMrLog (June 29, 2013 17:35:07)
- bob6
-
Scratcher
100+ posts
"More Blocks" Function Bugged
EatLikeInsects, can you explain to us what the purpose of your code is supposed to be? If you tell us that, we can maybe remake your code to a code that actually works, and explain it to you.
In my point of view, it seems that you are trying to make a code that first puts “Space” in the list, and if you keep on holding spacebar, it will put “Space2” in the list. The problem is that “Space2” keeps on getting put into the list more than once. Is this correct?
In my point of view, it seems that you are trying to make a code that first puts “Space” in the list, and if you keep on holding spacebar, it will put “Space2” in the list. The problem is that “Space2” keeps on getting put into the list more than once. Is this correct?
- EatLikeInsects
-
New Scratcher
26 posts
"More Blocks" Function Bugged
Ok. Your code is incorrect. You cant have a forever and then a block after that and even if it was inside it there is no point to having a forever because it is a define block which runs inside a different forever that has all the keys as one simple line of code. And it gets stuck in the repeat until loop because it is in a forever which just repeats it rather than checking for all of them at the same time (if you know what i mean). In my code you have a forever around the different logs but it cant get stuck because it only runs it forever and sends the boolean to the define block where that runs its script. the point is is that it shouldnt get stuck at the repeat until because it checks for when you unpress space and should stop the script after that. now onto the next topic. the thing im creating is a key tracker to check when each key was pressed and save them to a log so i can create a notepad and have files save to a list (with a bit of genius) it would be also useful in some games where you could tell exactly how long a key was pressed for. the main use is in my “Os” or whatever which will have saving functionality and online multiplayer (that works aswell as chat rooms etc. the problem: what i want it to do is to act like a real keyboard where it would type once and then repeat and save it as “space”. the “space2” was for comparision to explain the problem/bug. the end result i want: to be able to have a short bit of code for each key with my new block. sorry for the bad grammar. ob holidays and using my * samsung galaxy mini 

- DadOfMrLog
-
Scratcher
1000+ posts
"More Blocks" Function Bugged
Ok. Your code is incorrect. You cant have a forever and then a block after that…
Sorry, didn't follow why you think there's a block after the forever? Everything inside the first and last curly brackets is within the forever ‘C-loop’.
and even if it was inside it there is no point to having a forever because it is a define block…
Ah, OK. No, it wasn't meant to be thought of as being inside a define block - just imagine it has “when GreenFlag clicked” at the top of it.
…which runs inside a different forever that has all the keys as one simple line of code. And it gets stuck in the repeat until loop because it is in a forever which just repeats it rather than checking for all of them at the same time (if you know what i mean).
Hmmm, no, I don't quite know what you mean - especially by “all of them at the same time”.
And I wonder if there's some clue there to the misconception in your ‘mental model’ (if you see what I mean) of how these define blocks behave…?
In my code you have a forever around the different logs but it cant get stuck because it only runs it forever and sends the boolean to the define block where that runs its script. the point is is that it shouldnt get stuck at the repeat until because it checks for when you unpress space and should stop the script after that.
OK, maybe we should try a different approach here… can you point exactly at where (in your original scripts) you expected the value of boolean1 to get changed once space is unpressed? It doesn't happen within the define block itself, because once that define block script starts going, the value of boolean1 is fixed until the define block gets to the end. Do you agree with that? If so, then you must expect it to happen in the other script (i.e. the one that's not the define block). In which case, can you say exactly where that happens…?
now onto the next topic. the thing im creating is a key tracker to check when each key was pressed and save them to a log so i can create a notepad and have files save to a list
You might find the simplest way to do this would be to make use of lots of the “when (…) key pressed” event header block. Each one will ‘fire up’ its script whenever the specified key gets pressed. Then you can make it do whatever you want in response (like setting a variable to some character that corresponds to the pressed key, which you then add to some list/variable that's keeping track for you).
But I'm rather concerned that until we've ‘fixed’ the model you have in your mind of how these blocks should behave (is it just something about ‘define’ blocks?), you're going to find all sorts of things happening in your scripts that you don't expect…

Last edited by DadOfMrLog (June 30, 2013 11:26:17)
- drmcw
-
Scratcher
1000+ posts
"More Blocks" Function Bugged
Just to jump back in again.
How about this as an example?

First what do you think will happen? Then try it?
How about this as an example?

First what do you think will happen? Then try it?
- EatLikeInsects
-
New Scratcher
26 posts
"More Blocks" Function Bugged
Ok. I think you have figured it out. It must be that it boolean1 doesnt change as soon as the block starts. The only way to fix that is to constantly restart it or instant startup/shutdown etc. And the way you are pointing out to do it will make my pc lag trying to render all of the script so i figured to shorten it and just add keys as you go.
Onto the next topic. It will never as ten seconds is up as it only sets variable to timer as soon as you reset it. (later on in the day, some 2 minutes later) I just tried it and i was correct it never gets to ten seconds.
My point is is that its a bug (of some sorts) that it just doesnt update in the define block. The whole point of this post was to get help in coding a new script for it so that it would work as intended.
Until then,
Cheerieo
Onto the next topic. It will never as ten seconds is up as it only sets variable to timer as soon as you reset it. (later on in the day, some 2 minutes later) I just tried it and i was correct it never gets to ten seconds.
My point is is that its a bug (of some sorts) that it just doesnt update in the define block. The whole point of this post was to get help in coding a new script for it so that it would work as intended.
Until then,
Cheerieo

- drmcw
-
Scratcher
1000+ posts
"More Blocks" Function Bugged
Correct. Think of the define blocks parameters as variables, so they just take a value. So your Boolean takes the value of key space pressed when the block is called and won't change within the block.
Good luck with the script.
Good luck with the script.
Last edited by drmcw (June 30, 2013 19:53:53)
- DadOfMrLog
-
Scratcher
1000+ posts
"More Blocks" Function Bugged
Sure, no probs 
Just had a quick look at the scripts you've got at the moment, and a couple of things struck me…
1. You have “boolean1” outside the define block. Maybe that ended up there by accident, but in case not I'll note that the ‘variables’ in the header of a define block can only be used within that block's script. I suspect the value outside there will always be false (though it could end up being pretty random - don't know). Whatever, it certainly shouldn't be used outside the define block's script.
2. You have “stop this script” in your define block, yet it has no loop, so it's not going to do anything more anyway. Not causing any problems, but just wondering why you put it there…? (Maybe this got left in from something else you were doing? In which case, ignore me…)
I mentioned that what I'd probably do, if I'm understanding correctly what you want, would be to make a load of short scripts using the “when (…) key pressed” event headers (one for each key). I'm not sure if you were replying to that when you said “the way you are pointing out to do it will make my pc lag trying to render all of the script”? If so, I think you'll find that's not the case - the editor doesn't have much of a problem showing lots of very short scripts - it has much more of a problem showing one long script! (I occasionally try to use my Dell Mini to Scratch, but it's terribly slow, so I have a pretty good idea what things make it really struggle.)
Each script maybe wants to look something along these lines (this is for the “a” key):
when (a) key pressed
…record press of “a” by adding to some list…
wait until <not key (a) pressed>
…record release of “a” by adding to some list…
Then elsewhere you could have a script with a forever that monitors the list to see if it gets something added to the end (e.g. check to see if the length changes). When it does you can act on the new items in some appropriate way. (And at some point would you want to remove items from the start of the list, once you've dealt with them, so it doesn't get too long? Or do you want to keep a record permanently?)
Alternatively, you could replace the above two places I've said it records press/release in a list with a custom block that does something similar, passing it the key and whether it was pressed/released. That custom block could even process the press/release event, so you wouldn't need the forever loop I mentioned above.
Hope that gives you some ideas to work from.

Just had a quick look at the scripts you've got at the moment, and a couple of things struck me…
1. You have “boolean1” outside the define block. Maybe that ended up there by accident, but in case not I'll note that the ‘variables’ in the header of a define block can only be used within that block's script. I suspect the value outside there will always be false (though it could end up being pretty random - don't know). Whatever, it certainly shouldn't be used outside the define block's script.
2. You have “stop this script” in your define block, yet it has no loop, so it's not going to do anything more anyway. Not causing any problems, but just wondering why you put it there…? (Maybe this got left in from something else you were doing? In which case, ignore me…)
I mentioned that what I'd probably do, if I'm understanding correctly what you want, would be to make a load of short scripts using the “when (…) key pressed” event headers (one for each key). I'm not sure if you were replying to that when you said “the way you are pointing out to do it will make my pc lag trying to render all of the script”? If so, I think you'll find that's not the case - the editor doesn't have much of a problem showing lots of very short scripts - it has much more of a problem showing one long script! (I occasionally try to use my Dell Mini to Scratch, but it's terribly slow, so I have a pretty good idea what things make it really struggle.)
Each script maybe wants to look something along these lines (this is for the “a” key):
when (a) key pressed
…record press of “a” by adding to some list…
wait until <not key (a) pressed>
…record release of “a” by adding to some list…
Then elsewhere you could have a script with a forever that monitors the list to see if it gets something added to the end (e.g. check to see if the length changes). When it does you can act on the new items in some appropriate way. (And at some point would you want to remove items from the start of the list, once you've dealt with them, so it doesn't get too long? Or do you want to keep a record permanently?)
Alternatively, you could replace the above two places I've said it records press/release in a list with a custom block that does something similar, passing it the key and whether it was pressed/released. That custom block could even process the press/release event, so you wouldn't need the forever loop I mentioned above.
Hope that gives you some ideas to work from.
Last edited by DadOfMrLog (July 2, 2013 10:52:20)
- EatLikeInsects
-
New Scratcher
26 posts
"More Blocks" Function Bugged
Just redone the script and made it tidier. Now it doesnt work at all. Hmmm. And what you said might work but i need it to repeat it if you hold it in for a while like a normal keyboard would work. Ill work on it some more and see what i can do, maybe i can report it to the scratch team that it doesnt update inside the block or as i said get it to instantly run over and over again which would work.
- DadOfMrLog
-
Scratcher
1000+ posts
"More Blocks" Function Bugged
If you want key-repeat to work as if typing into a normal text-box, then “when (…) key pressed” is the way to go - you'll find that works straight away.
Give it a go with just the two blocks:
when space key pressed
change counter by 1
- and have the counter variable showing… just what you wanted
A forever in the custom block, within the forever of the main script. It's never gonna come out of that define block now, because it stays in it ‘forever’ (even if “boolean1” could become false somehow, so it gets past that “repeat until boolean1 = false”). That means it'll never get back to the main script's forever, even the first time through when space isn't pressed. But maybe that's related to the “stop this script” that you had in there before? (So it would've stopped it at some specific event.)
Give it a go with just the two blocks:
when space key pressed
change counter by 1
- and have the counter variable showing… just what you wanted

Just redone the script and made it tidierWoah, what you have now is curious…
A forever in the custom block, within the forever of the main script. It's never gonna come out of that define block now, because it stays in it ‘forever’ (even if “boolean1” could become false somehow, so it gets past that “repeat until boolean1 = false”). That means it'll never get back to the main script's forever, even the first time through when space isn't pressed. But maybe that's related to the “stop this script” that you had in there before? (So it would've stopped it at some specific event.)
Last edited by DadOfMrLog (July 2, 2013 14:46:41)
- drmcw
-
Scratcher
1000+ posts
"More Blocks" Function Bugged
Can you still help me?If that was at me, then of course. Ask away! I'll jump in when I can.
Last edited by drmcw (July 2, 2013 14:56:54)
- DadOfMrLog
-
Scratcher
1000+ posts
"More Blocks" Function Bugged
OK, I remixed it, showing the method I mentioned.
Rather tedious - particularly because of the need to detect the key-repeat and the key-release, plus with more than one key at the same time, but I think Scratch is going to force you down that path given its limitations with respect to key events (you can only check for a specific key, rather than any key and then finding out which it was, as would normally happen in other coding languages).
Haven't finished all the possible keys (only done Space and A-Z), but there are only a few left anyway (numbers and arrows).
(Pity it's missing Return/Enter, and would be nice to have some others, too, like Esc, and some of the symbols like full stop, comma, etc. - there is a way to hack it so it will work with any key, though, but it's potentially confusing for someone to see it who doesn't realise it's hacked…)
Let me know if that's basically what you have in mind…
Oh, and @drmcw: just remembered I meant to ask if you've got any other ideas how to go about this without it being quite so tedious? (So it captures both key down & key up, as well as getting the key-repeat events.)
Rather tedious - particularly because of the need to detect the key-repeat and the key-release, plus with more than one key at the same time, but I think Scratch is going to force you down that path given its limitations with respect to key events (you can only check for a specific key, rather than any key and then finding out which it was, as would normally happen in other coding languages).
Haven't finished all the possible keys (only done Space and A-Z), but there are only a few left anyway (numbers and arrows).
(Pity it's missing Return/Enter, and would be nice to have some others, too, like Esc, and some of the symbols like full stop, comma, etc. - there is a way to hack it so it will work with any key, though, but it's potentially confusing for someone to see it who doesn't realise it's hacked…)
Let me know if that's basically what you have in mind…
Oh, and @drmcw: just remembered I meant to ask if you've got any other ideas how to go about this without it being quite so tedious? (So it captures both key down & key up, as well as getting the key-repeat events.)
Last edited by DadOfMrLog (July 2, 2013 16:15:34)
- Zparx
-
Scratcher
500+ posts
"More Blocks" Function Bugged
Can I put forward a short script, and ask a couple of questions about how you understand it…?
OK, so this script has three blocks, the first of which waits for space to be pressed, the second sets a variable called “spacecheck”, and the last one waits for that variable to be false:
wait until <key space pressed>
set spacecheck to <key space pressed>
wait until <spacecheck = false>
Once this script is running, what do you expect to happen as you press and release the space key…?
He is right. The reason your block is getting stuck in a loop is because even though you have your custom in a forever loop, it still takes that photo of the “space pressed” boolean on its journey to your definition of the custom at the exact moment the block is launched. So since the space key is pressed when the block is run, it tries to repeat until the space key is not pressed, which simply doesn't coordinate with the “snapshot” taken. So your block keeps looking at the snapshot to see if the space bar has been released, which it isn't, resulting in your loop.
Last edited by Zparx (July 2, 2013 16:21:02)
- drmcw
-
Scratcher
1000+ posts
"More Blocks" Function Bugged
Oh, and @drmcw: just remembered I meant to ask if you've got any other ideas how to go about this without it being quite so tedious? (So it captures both key down & key up, as well as getting the key-repeat events.)I'm impressed. No I don't know of another way to achieve this and if I'm honest was the sort of script I envisaged when I realised that the requirement was ultimately to cover all key presses and not just space.

- EatLikeInsects
-
New Scratcher
26 posts
"More Blocks" Function Bugged
Now that I have full understanding of how the new block function in scratch 2.0 works I have decided that trying to run the new block over and over again to check for any updates is the way to go. The original idea was to have the code that I originally had to tile all the Key Tracker blocks in one big forever loop and add any new keys if i need to. Now that I know it takes a snapshot I would really like to tell this to the development team to change it but that's not going to happen unless someone knows how to. Anyone, eh?
About the way i “tidied” up the script. The forever was there by accident and thats why it didnt work so nvm.
What you have done with the remix i quite like but it doesnt have the tillablity I was shooting for and as i know you can add timing abilities to it I would like to be able to detect how long you pushed each key for.
I will try to work on this some more.
Thx for your help everyone,
Scratch FTW (The community is better
)
About the way i “tidied” up the script. The forever was there by accident and thats why it didnt work so nvm.
What you have done with the remix i quite like but it doesnt have the tillablity I was shooting for and as i know you can add timing abilities to it I would like to be able to detect how long you pushed each key for.
I will try to work on this some more.
Thx for your help everyone,
Scratch FTW (The community is better
)- EatLikeInsects
-
New Scratcher
26 posts
"More Blocks" Function Bugged
Havnt found anything yet but ill keep looking.
- Discussion Forums
- » Help with Scripts
-
» "More Blocks" Function Bugged




