Loops
Loops allow you to do repetitive things without having to repeat lines of code a bunch of times. There are two loops automatically available as part of the builtin standard, which are @repeat
, and @for
.
Simplest loop, @repeat
This is the more convenient loop to use most of the time. You simply pass in the amount of times that you want the lines in its block to be repeated, and it repeates them those number of times. Here's an example:
# Press and release the key `a` 5 times #
@repeat [5] { # Of course, you can refer to numbers in vectors as well #
a # Presses and releases `a` #
# You can have as many lines as you want #
}
You can also use @end
to break out of the loop. This does not end the entire program at that point when used inside of a loop, but rather gets interpreted as sort of a break as used in other languages.
What is and how to use an @for loop
@for
allows you to acheive similar functionality to for loops in other languages. You give it a start, end, and step, and a variable name which will be set to a vector that is referred to as a number.
The start is just the initial value of that vector at component zero, and the end is the maximum component zero can reach before the loop finishes (it is inclusive, so it runs even if it is equal to the end).
The arguments are exactly in this order: start, end, step, variable vector name. Here's a basic example of how this works:
# 0 to 5, incrementing by 1. $WHAT is the vector variable used #
@for [0, 5, 1, $WHAT] {
@cursor [$WHAT, $WHAT] # Places the cursor at ($WHAT,$WHAT) #
<50> # Wait 50ms $
}
# The cursor will move to (0,0), (1,1), (2,2), (3,3), (4,4), (5,5) #
The @forEach loop
The @forEach
loop lets you loop through every number in a VECTOR
. It takes three variables names, the VECTOR
, element variable, and index variable.
The element and index variable you get to name however you like, so long as there is not a conflict. They are set on every run of the loop and correspond to the current index and number in the vector.
Keep in mind that the loop goes through the VECTOR
as it was when the whole loop started, so changes midway will not be reflected. Here's an example of using these loops:
<VECTORS>
$my_numbers = 0,5,10,15
<MACRO>
@forEach [$my_numbers, $e, $i] {
# $e will be 0->5->10->15 #
# $i will be 0->1->2->3 #
}