Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Do code in forever loops run every frame?
- Litten99
-
6 posts
Do code in forever loops run every frame?
I noticed that when I told the program to decrease a value forever, it didn't instantly get decreased massively, so i'm wondering, do code only run per frame?
Also, I made it so I take 1 damage each time I touch an enemy, it worked fine while touching one enemy at once but when I tested it, touching multiple enemies at once didn't influence the amount of health decrease at all. It was the same decrease as if I had only touched 1 enemy. Weird.
Also, I made it so I take 1 damage each time I touch an enemy, it worked fine while touching one enemy at once but when I tested it, touching multiple enemies at once didn't influence the amount of health decrease at all. It was the same decrease as if I had only touched 1 enemy. Weird.
Last edited by Litten99 (Feb. 23, 2021 19:21:05)
- Greg8128
-
500+ posts
Do code in forever loops run every frame?
The ‘forever’ loop runs as fast as it is able to. Try the following example:
However, some blocks (such as movement blocks) force the code to wait until the screen refreshes. Using such a block in the loop will cause it to run only once per frame (or once per two frames if there are two such blocks, and so forth)
- awesome-llama
-
1000+ posts
Do code in forever loops run every frame?
The forever loop (and others) run at a speed dependent on what they contain.
If everything inside can be run without refreshing the screen (e.g. setting variables), then it will run as fast as it can, many more times than the framerate.
If the blocks cannot run without refreshing the screen (most scripts), then the forever loop will just go at the normal Scratch speed (~30 FPS).
If you are including blocks that take longer than a frame, then the forever loop will take longer to complete a loop of it. The loop simply waits until the blocks inside it are done.
If everything inside can be run without refreshing the screen (e.g. setting variables), then it will run as fast as it can, many more times than the framerate.
If the blocks cannot run without refreshing the screen (most scripts), then the forever loop will just go at the normal Scratch speed (~30 FPS).
If you are including blocks that take longer than a frame, then the forever loop will take longer to complete a loop of it. The loop simply waits until the blocks inside it are done.
- AksharPremnath
-
500+ posts
Do code in forever loops run every frame?
The forever loop (and others) run at a speed dependent on what they contain.
If everything inside can be run without refreshing the screen (e.g. setting variables), then it will run as fast as it can, many more times than the framerate.
If the blocks cannot run without refreshing the screen (most scripts), then the forever loop will just go at the normal Scratch speed (~30 FPS).
If you are including blocks that take longer than a frame, then the forever loop will take longer to complete a loop of it. The loop simply waits until the blocks inside it are done.
If you use Turbo Mode and/or Turbowarp wouldn't it decrease the value more?
- awesome-llama
-
1000+ posts
Do code in forever loops run every frame?
It depends on the script.The forever loop (and others) run at a speed dependent on what they contain.
If everything inside can be run without refreshing the screen (e.g. setting variables), then it will run as fast as it can, many more times than the framerate.
If the blocks cannot run without refreshing the screen (most scripts), then the forever loop will just go at the normal Scratch speed (~30 FPS).
If you are including blocks that take longer than a frame, then the forever loop will take longer to complete a loop of it. The loop simply waits until the blocks inside it are done.
If you use Turbo Mode and/or Turbowarp wouldn't it decrease the value more?
Turbo mode allows the scripts to run much faster than usual. I forgot to mention this above, but the loop blocks have a hidden delay with them, preventing scripts from running excessively fast. It is as if there is an invisible wait () secs block at the end. Turbo mode simply disables this. It is useful when the loop delay is a significant length of time compared to how long it takes to run the scripts in it. Often, this would be used when the loop needs to run many more times than 30 per second for some sort of operation. It is not really useful if this loop delay is not that significant.
TurboWarp on the other hand is a more optimised version of Scratch. Rather than removing the delay in loops that turbo mode does, TurboWarp improves the time it takes to run the other blocks. It also features a 60 FPS mode which gives you an extra boost as now the loops can run twice as many times.
It should be noted that no matter what speed improvements you do, a script like these below will not be able to be sped up at all (for obvious reasons):
- Discussion Forums
- » Advanced Topics
-
» Do code in forever loops run every frame?