User Tools

Site Tools


hpl2:tutorials:forloop

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
hpl2:tutorials:forloop [2011/08/14 00:21]
thegreatcthulhu
hpl2:tutorials:forloop [2012/09/12 13:19]
jens removed
Line 5: Line 5:
  
  
-A "​for"​ loop's header defines 3 parts things: (1) <font 9pt:​normal/​arial>​the counting variable</​font> ​usually ​set to an initial value, (2) <font 9pt:​normal/​arial>​the loop condition</​font> ​, and (3) the <font 9pt:​normal/​arial>​change in the variable'​s value</​font> ​. These are separated by a semicolon ( ; ).  After the header, comes the body of the loop, which defines what is it that should be done on each loop cycle (note: a loop cycle is usually called an //​iteration//​). It can consist of a single statement, or of multiple statements in a code block.+A "​for"​ loop's header defines 3 parts things: (1) the counting variable, set to an initial value, (2) the loop condition, and (3) the change in the variable'​s value. These are separated by a semicolon ( ; ).  After the header, comes the body of the loop, which defines what is it that should be done on each loop cycle (note: a loop cycle is usually called an //​iteration//​). It can consist of a single statement, or of multiple statements in a code block.
  
  
Line 11: Line 11:
  
  
-<​code>​ +<​code ​cpp>for (counter; condition; step)
-for (counter; condition; step)+
     DoSomething();​     DoSomething();​
 </​code>​ </​code>​
Line 20: Line 19:
  
  
-<​code>​ +<​code ​cpp>for (counter; condition; step)
-for (counter; condition; step)+
 { {
     DoSomething();​     DoSomething();​
Line 44: Line 42:
  
  
-Lets take this apart. "void OnStart()"​ is the location it is in, which happens when the level starts up. "​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.+Let'​s ​take this apart. "void OnStart()"​ is the location it is in, which happens when the level starts up.
  
  
-NOTE: It's an old programming tradtion to start your counters from - 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.+The "​for(int i = 0i < 4; i++)" runs the loop body (in this case, the loop body adds a timer that will trigger "​TimerFunction"​) ​4 times before the loop breaksbecause integer "​i"​ has to be less than 4, as stated in the loop condition.
  
  
-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).\\  ​+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).
  
  
Line 73: Line 71:
  for (int x = 0; x <= 4; x += 2)   for (int x = 0; x <= 4; x += 2) 
  {  ​  {  ​
- AddEntityCollideCallback("​Player",​ "​ScriptArea_"​+x,​ true, 1); + AddEntityCollideCallback("​Player",​ "​ScriptArea_"​ + x, true, 1); 
  
 } }
Line 80: Line 78:
  
 Note that the condition here checks if x is //less then or equal// to 4 (<= 4). Also note that the x variable is incremented by 2 on each loop cycle (''​x += 2''​ is just shorthand for''​x = x + 2''​). This means that the loop will body execute 3 times (remember, the counter started from 0, not 1 - so, i = 0, 2, 4). Note that the condition here checks if x is //less then or equal// to 4 (<= 4). Also note that the x variable is incremented by 2 on each loop cycle (''​x += 2''​ is just shorthand for''​x = x + 2''​). This means that the loop will body execute 3 times (remember, the counter started from 0, not 1 - so, i = 0, 2, 4).
 +
 +
 +You can also loop backwards. For example, assuming you have a long corridor with 10 lights made in the editor, and that the player is closest to corridor_light_0,​ and that the corridor_light_9 is the farthest, this loop will create an interesting effect, by fading out the corridor_light_9 first, and then working it's way back toward corridor_light_0,​ increasing the fadeout time (the last parameter) in each step.
 +
 +
 +<code cpp>for (int i = 9; i >= 0; i--)
 +{
 +    FadeLightTo("​corridor_light_"​ + i, 0, 0, 0, 1, -1, 9 - i);
 +}
 +</​code>​
  
  
Line 88: Line 96:
  
  
-**- Edited by Xiphirx. Fixed errors in code and in text.**+**- Edited by Xiphirx. Fixed errors in code and in text.**  
 + 
 + 
 +**- Edited by TheGreatCthulhu. Added the introduction,​ some additional info here and there, formatted code where required.**