Loops are things made to make your life easier, at least, if you know how to
use them. Loops can be powerfull when used right, but can also bring down any
script. Loops come in handy when you need to go through a array, or when you
need to do the same thing x times after eachother. The loops available in the
Athena scripting language are For, While and Do ... While. All of those will
be covered in this article, with their own examples. Also, the special loop
commands break; and continue; will be covered. The intention of this page is
that you will get to understand how loops work, and how to use them properly.
The for loop is probably the most seen loop used in Hercules scripting, this
is usually used to loop through arrays and execute a series of commands for
each array entry. But you can use it every time you need to execute a series
of commands x times. For loops are build up of 3 or 4 parts, depending on how
they are used. The general buildup for a For loop is as following:
for (initialization(1); condition(2); update(3)) {
Code to execute(4);
}
If you don't know what all parts do, don't panic, since I'm going to explain
that now ;)
0
1 2 3 4 5 6 7 8 9 Note that the 10 is not there, since @i
set @i,0;
while (@i
The result will be exactly the same. As long as @i is smaller then 10, the
code is executed. While loops can be used for a lot of things, including a
(theoretically) never ending cycle:
while (1) {
mes "I won't go away!";
next;
}
Note that the above is a script that will need you to relog!
Ah... We've already reached the last loop type. Wow, I actually managed to
write stuff about the for and while loop eh... Well then. This loop is
basically the same as a while loop, only the way it is executed is slightly
different (and the same goes for the coding). Since there isn't much I can
explain on loops anymore, I'll just start with the general format:
do {
Code to execute(1)
} while (expression(2));
See how the code and expression changed? Basically, this is a inverted while
loop, and the do is only there to let the engine know there is a loop. Once
this part of code is reached, the code will be executed first (!). Once the
code is executed, the expression is evaluated. If the expression returns true,
it loops back to the do, and runs the code again. If you noticed that this
way, the code is always executed at least once, good job =). If you didn't,
well, then you know it now :P. This is because the engine executes scripts
from top to bottom (excluded goto's and such), that way, the engine will first
find the do {, then the code, and then the while. Again, the simple example
from before:
[Set] @i,0;
[Do] {
[Mes] @i;
[Set] @i,@i+1;
} [While] (@i
for (set @i,0; @i
This will break out of the loop once @i is equal to 4, meaning the the result
of this loop is:
0
1
2
3
Continue also quits a part of the loop, but will jump to the last line (or the
beginning) of the loop. That way, only that run of the loop is not executed. A
simple example for continue:
for (set @i,0; @i
This will return:
0
1
2
3
4
5
9
10
The 'mes @i;' code is not executed when the if in front of the continue; is
true, which is for @i > 5 and @i
if (@a != @b)
getmapinfo @map$,@x,@y,0;
else if (@a > @b)
set @c,getcharid(3);
else if (@a