MODSon[line.com] Wiki - Beta 1.0
Scripting: Loops
From MODSonline Wiki
Contents |
[edit] What is a loop?
Loops are blocks of code that allow you to do similar things over and over. It is like looping a song that you like, but with code. Loops allow you to keep doing a function over and over without having to do a long script that will just keep calling something. Loops are good for "looping" through arrays. There are many different kinds of loops that will allow you to do different things. They are "while" and "for" loops.
[edit] For Loops For All
For loops are great for looping through those arrays. They run only a set amount of times. You can find how how many things are in an array, then loop it that many times! Here is an example of a for loop.
main() { for(i=0;i<5;i++) { iprintln("Loop # "+i); // Print i } }
This will print i on each loop, or print 01234. Notice how it will not print 5. I will explain this latter, in the mean time, let's look at the for loop.
There are 3 parts to a for loop: the int, condition, and increase.
1. Int: Used to specify what the "counter" is. In this case i and it equals 0.
2. Condition: This is checked at the beginning of each loop. If it is FALSE, it will break the loop.
3. Increase: This is how the "counter" should be increased, or decreased on each loop. Most of the time you want to increase by only 1.
Now this is where they come in handy with arrays. You can get the size of an array, then loop for each of those objects on the array. Here is an example.
main() { ent = getEntArray("objects", "targetname"); for(i=0;i<ent.size;i++) { ent[i] delete(); } }
Every entity with the targetname of objects will deleted. This is because we got the entity array, then told the script to loop until i is greater than the size of the objects array. Now this is why for loops are good working with arrays. You can get the size of the array and do something for every single one of them!
[edit] While This, Do This
While loops are great for checking things, and also good for doing things that will be repeated an endless amount of times. However, you have to be careful! You do not want an infinite loop. You have to put in a kind of wait in every while loop. Here is an example of a while loop.
main() { trig = getEnt("trigger", "targetname"); // Get trigger trig waittill("trigger", player); // Wait till trigger triggers. while(player isTouching(trig)) // Loop while player is touching the trigger { player iprintln("You are inside a trigger!"); wait 1; // Add a wait... this is so we don't get an infinite loop! } player iprintln("You steped out of the trigger!"); }
The above script will wait until a player triggers a trigger. Once the player triggers it, the while loop begins. As long as the player is touching the trigger, a message will be printed every second. Once the player steps out of the trigger, the while loop will break, causing the message saying the player is no longer in the trigger to print on the screen.
Again, be sure you add some kind of wait time until the next loop!
You can also use it so you can trigger and trigger until the map is finished.
main() { thread trigger(); } trigger() { trig = getEnt("trigger", "targetname"); // Get trigger while(1) // 1 is always true, so it will loop forever! { trig waittill("trigger", player); // Wait until the trigger is triggered. iprintln(player.name+" has triggered the trigger!"); } }
Here it gets the trigger and starts a while loop. while(1) means Loop Forever! Sense 1 is always true, the while statement will loop until the script is killed. This is very useful on triggers that need to be able to be triggered the whole map, such as elevators and artillery calls!
[edit] Continuing On
Now, let's say you need to skip over something in a loop. You can use continue; to go on to the next loop. It is almost as though that single loop finished early. Here is an example:
main() { triggers = getEntArray("trigger_multiple", "classname"); for(i=0;i<triggers.size;i++) { if(triggers[i].no_delete == "1") continue; triggers[i] delete(); } }
In this loop, it takes all of the triggers in the map and checks for the key no_delete. If no_delete is set to 1, the loop will NOT delete that trigger! It will continue to the next loop.
[edit] Breaking The Fun
Now there may be an instance where you need to break a loop. This will completely stop the loop. Once you call break, there is no going back! You just broke the loop.
main() { trig = getEnt("trigger", "targetname"); trig waittill("trigger", player); while(player isTouching(trig)) { if(player useButtonPressed()) break; player iprintln("You are in the trigger!"); } player ipintln("Exited trigger or pressed use key!"); }
In the example above, wait until the trigger is triggered. While the player is touching the trigger loop. If he presses his use key OR exits the trigger, end the loop. If you are standing inside the trigger and press your use key, the while statement will break!
