![]() |
Help Explaining for()
can someone explain what for()
is for and ow i use it? plz tell meh |
A for loop is a counter driven loop, the syntax is for(var and starting value; condition to loop while true; increment) { commands to be executed }
So for instance, NPC Code: will move the player 5 spaces to the right (.5 spaces, 10 times) But don't use it like that, the best time to use it (In my opinion) is with arrays: Say I've got a 5 element array, and I want to set each element to whereever it is in the array, + 1, so that the array will be {1, 2, 3, 4, 5} (the first element's index in an array is 0, remember) I would do this: NPC Code: Get it? |
i dont get it still
|
A Quote:
One who dies... Well... Dies |
okkkk time for a TDO explanation ;D
sooo a for-loop is a possibility to do different things like: getting every array, move npcs or just count something an example for(this.i=0;this.i < 5;this.i++){ playerx++; } this small code would move the player 5 fields to the right explanation: ok for(this.i=0 .... this is a starting value of a variable at the first time this.i will be 0 ...;this.i < 5;... and as long this.i is smaller the 5 ...;this.i++){ this.i will get one added so what it really does: at the first time this.i is set to 0 it's smaller then 5 1 is added to this.i the code in { } is executed with this.i=1 then back to the beginning this.i is 1 so it's smaller then 5 so: this.i++ 1 is added to this.i and its 2 the code is executed with this.i = 2 and as long this.i is smaller then 5 the this.i gets 1 and the code is executed (so the playerx gets 5 times bigger) so for arrays (don't want to explan arrays) see this array: this.array={2,1,5,6}; if u had an array and wanted to check every element in it for a value (lets say if an element is 5) u can use a for loop this.array={2,1,5,6}; this.lengt = arraylen(this.array); //this will get the elemts in the array //now our checking for-loop for(this.i=0;this.i < this.lenght;this.i++){ if(this.array[this.i]==5){ message Arraypos #v(this.i) is 5; } } so the loop will start at the first array-pos (it's 0) and loop untill it got the full count of elements It will start with 0 if(this.array[0]==5) //at the first time this.i would be 0 and check the first element of the array (here it's 2) if this elemt is 5 ==5 it would say: "Arraypos 0 is 5" but because it's not 5 it will ignore this and add 1 to this.i then the 2nd elemt of the array is checked etc. Hope that's understandable ;) |
i understand the "for" part but not the "array" part
=/ |
Re: Help Explaining for()
Quote:
|
Quote:
|
NPC Code: If you run that in a script, command1 will be run, then while flag is true, stuff and command2 will be run. It would be the same as NPC Code: An example for moving a NPC: NPC Code: Or killing all the policemen: NPC Code: |
Re: Re: Help Explaining for()
Quote:
|
Quote:
playerx+=.5; sleep .05; playerx+=.5; sleep .05; playerx+=.5; sleep .05; playerx+=.5; sleep .05; playerx+=.5; sleep .05; playerx+=.5; sleep .05; playerx+=.5; sleep .05; playerx+=.5; sleep .05; playerx+=.5; sleep .05; playerx+=.5; sleep .05; =) and better then doing while (this.i<10) { this.i++; playerx+=.5; sleep .05; } |
Quote:
|
Sleep is one of them. :) Also set FLAG; while (flag) {//loop crap here}
|
Quote:
if (playerenters) setlevel #L; |
thats great:
if(playerenters){ while(true){ toweapons crap; sleep .01; } } |
Quote:
|
NPC Code:This would be cool if you could use message codes in weapon names :( |
and that's why u can't use messagecodes in weaponnames ;)
|
| All times are GMT +2. The time now is 05:44 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.