Loop: Difference between revisions

From Fancade Wiki
m (Use infobox; format consistency)
(Changed a link from List Element to List)
 
(One intermediate revision by the same user not shown)
Line 11: Line 11:
}}
}}


The Loop block is used to run a script multiple times in the same frame. Often used for processing every element in a [[List Element|list]].
The Loop block is used to run a script multiple times in the same frame. Often used for processing every element in a [[List]].


Inputs
The loop will execute everything connected to the Do wire Stop - Start times, with the counter incrementing (or decrementing) from Start to Stop on each iteration.
* Start: The Loop block starts looping from the Start input.
* Stop: Then it stops looping once it reaches the Stop input.


Outputs
* Do: Plug this to the script that you want to run for every loop.
* Counter: Outputs the current loop number counting from the Start value. If the Start value is greater than the Stop value, it counts down instead of up.


== Notes ==
== Notes ==


* The counter always steps by 1 (or -1, if Start is greater than Stop)
* The counter always steps by 1 (or -1, if Start is greater than Stop).
* The counter does not output the Stop value
* If a non-integer value is provided for Start, it's rounded down to the next smallest integer.
* If a non-integer value is provided for Start, it's rounded down to the next smallest integer.
* If a non-integer value is provided for Stop, it's rounded up to the next biggest integer.
* If a non-integer value is provided for Stop, it's rounded up to the next biggest integer.
 
* The counter includes the Start value, but excludes the Stop value.
If you were to start the loop at 0 and stop it at 5, you would loop the script 5 times (Count) and the Counter outputs from 0 to 4 (End). Which shows that the Counter won't output the Stop value by the end of the loop, might want to keep that in mind.
: [[File:Loop example1.png|thumb|none]]
 
[[File:Loop_example1.jpg]]


== Example ==
== Example ==


Beginners often just place the script in the block that they want to process, in which every blocks of the same type would have the same script.
One of the most common uses of loops is to perform some operation to many blocks at once.
For cases where the script is small or there aren't too many of the same type of blocks in a level, it won't cause much of a problem.
To do that, the blocks should be added to a [[List]].
Otherwise, you may easily run into a script limit while you're placing those blocks in a level.
 
We could put those blocks in a list and loop the script for every block, so that we could process every block we need with only one script, instead of having to add duplicates of that script.


In the example below, we're moving the blocks by (0, 0, 0.1) every frame.
In the example below, we're moving the blocks by (0, 0, 0.1) every frame.


[[File:Loop_example2.jpg]]
{{Image|Loop example2.png}}


It's mentioned previously that the Counter won't output the Stop value by the end of the loop.
It's mentioned previously that the Counter won't output the Stop value by the end of the loop.
It won't be a problem because after we add the last block to a list, it increments the `$Length` variable by one.
It won't be a problem because after we add the last block to a list, it increments the <code>$Length</code> variable by one.
So, if the last block is assigned to index 4, `$Length` would equal to 5 which is exactly what the Stop input should be to account for every blocks.
So, if the last block is assigned to index 4, <code>$Length</code> would equal to 5 which is exactly what the Stop input should be to account for every block.


[[Category:Blocks]]
[[Category:Blocks]]

Latest revision as of 21:22, 4 July 2024

Loop
Loop.png
TypeScript block
ColliderNone
FolderControl
Ports
Inputs Before
Start
Stop
Outputs Do
Counter
After

The Loop block is used to run a script multiple times in the same frame. Often used for processing every element in a List.

The loop will execute everything connected to the Do wire Stop - Start times, with the counter incrementing (or decrementing) from Start to Stop on each iteration.


Notes

  • The counter always steps by 1 (or -1, if Start is greater than Stop).
  • If a non-integer value is provided for Start, it's rounded down to the next smallest integer.
  • If a non-integer value is provided for Stop, it's rounded up to the next biggest integer.
  • The counter includes the Start value, but excludes the Stop value.

Example

One of the most common uses of loops is to perform some operation to many blocks at once. To do that, the blocks should be added to a List.

In the example below, we're moving the blocks by (0, 0, 0.1) every frame.

Loop example2.png

It's mentioned previously that the Counter won't output the Stop value by the end of the loop. It won't be a problem because after we add the last block to a list, it increments the $Length variable by one. So, if the last block is assigned to index 4, $Length would equal to 5 which is exactly what the Stop input should be to account for every block.