Loop: Difference between revisions

From Fancade Wiki
(Created page with "File:Loop1.png 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 * 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 tha...")
 
m (Use infobox; format consistency)
Line 1: Line 1:
[[File:Loop1.png]]
{{Block
|image=Loop.png
|type=s
|folder=Control
|input1={{Port|e|Before}}
|input2={{Port|n|Start}}
|input3={{Port|n|Stop}}
|output1={{Port|e|Do}}
|output2={{Port|n|Counter}}
|output3={{Port|e|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 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 Element|list]].
Line 20: Line 30:
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.
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:Screenshot_20210128-114243_Fancade.jpg]]
[[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. 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. Otherwise, you may easily run into a script limit while you're placing those blocks in a level.
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.
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.
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.
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.
Line 30: Line 42:
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:Screenshot_20210128-171605_Fancade.jpg]]
[[File:Loop_example2.jpg]]
 
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 blocks.


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 blocks.


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

Revision as of 11:53, 20 May 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.

Inputs

  • 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

  • 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 Stop, it's rounded up to the next biggest integer.

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.jpg

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. 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. 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.

File:Loop example2.jpg

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 blocks.