Both sides previous revision Previous revision | Next revision Both sides next revision | ||
hpl2:tutorials:forloop [2011/08/14 00:52] thegreatcthulhu |
hpl2:tutorials:forloop [2011/08/14 00:55] thegreatcthulhu |
||
---|---|---|---|
Line 11: | Line 11: | ||
- | <code cpp> | + | <code cpp>for (counter; condition; step) |
- | for (counter; condition; step) | + | |
DoSomething(); | DoSomething(); | ||
</code> | </code> | ||
Line 20: | Line 19: | ||
- | <code cpp> | + | <code cpp>for (counter; condition; step) |
- | for (counter; condition; step) | + | |
{ | { | ||
DoSomething(); | DoSomething(); | ||
Line 44: | Line 42: | ||
- | Let's take this apart. "void OnStart()" is the location it is in, which happens when the level starts up. | + | Let's take this apart. "void OnStart()" is the location it is in, which happens when the level starts up. |
- | The "for(int i = 0; i < 4; i++)" runs the loop body (in this case, the loop body adds a timer that will trigger "TimerFunction") 4 times before the loop breaks, because integer "i" has to be less than 4, as stated in the loop condition. \\ | + | The "for(int i = 0; i < 4; i++)" runs the loop body (in this case, the loop body adds a timer that will trigger "TimerFunction") 4 times before the loop breaks, because integer "i" has to be less than 4, as stated in the loop condition. |
- | NOTE: It's an old programming tradtion to start your counters from 0 - maybe this seems counter-intuitive, but it's often more convenient to do so. Just remember, if you see the initial value of the counter variable set to 0, and the condition states i < 4, a loop like this will execute 4 times, //not// 3. | + | NOTE: It's an old programming tradtion to start your counters from 0 - maybe this seems counter-intuitive, but it's often more convenient to do so. Just remember, if you see the initial value of the counter variable set to 0, and the condition states i < 4, a loop like this will execute 4 times, //not// 3.\\ Why? Look at how the value changes: i= 0, 1, 2, 3 , (4 doesn't execute, as the condition is not true for i=4). |
- | + | ||
- | + | ||
- | Why? Look at how the value changes: i= 0, 1, 2, 3 , (4 doesn't execute, as the condition is not true for i=4). | + | |
Line 88: | Line 83: | ||
- | <code cpp> | + | <code cpp>for (int i = 9; i >= 0; i--) |
- | for (int i = 9; i >= 0; i--) | + | |
{ | { | ||
FadeLightTo("corridor_light_" + i, 0, 0, 0, 1, -1, 9 - i); | FadeLightTo("corridor_light_" + i, 0, 0, 0, 1, -1, 9 - i); |