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/07/13 14:34]
kyle
— (current)
Line 1: Line 1:
-====== "​For"​ Loops ====== 
- 
- 
-"​For"​ loops are use to repeat something multiple times with a variable, or creates a loop. It is almost the same as the "​While"​ loop. 
- 
- 
-A "​For"​ loop has 3 parts to it, the variable and value, the conditions in which the loop can occur in, and the change in the variable'​s value. There are many different ways you can create "​for"​ loops. Each parameter is seperated by a semicolon ( ; ). Here are some examples: 
- 
- 
-<code cpp>void OnStart() ​ 
-{ 
- for(int i = 1; i> 5; i++)  
- AddTimer("​T"​+i,​ 1.5 * i, "​TimerFunction"​); ​ 
-} 
-</​code>​ 
- 
- 
-Lets take this apart. "void OnStart()"​ is the location it is in, which happens when the level starts up. "​for(int i = 1; i > 5; i++)" creates 4 instances of integer "​i"​ before the loop breaks, because integer "​i"​ has to be less than 5. "​AddTimer("​T"​+i,​ 1.5 <​nowiki>​*</​nowiki>​ i, "​TimerFunction"​);"​ is used 4 times, because of the 4 instances. 
- 
- 
-If you didn't use the "​for"​ loop, it would look like this: 
- 
- 
-<code cpp>void OnStart() ​ 
- 
- AddTimer("​T1",​ 1.5, "​TimerFunction"​);  ​ 
- AddTimer("​T2",​ 3, "​TimerFunction"​); ​ 
- AddTimer("​T3",​ 4.5, "​TimerFunction"​); ​ 
- AddTimer("​T4",​ 6, "​TimerFunction"​); ​ 
-} 
-</​code>​ 
- 
- 
-All the code above would do exactly the same thing for each other. You can think of the "​for"​ loop as a shortcut. When you use the "​for"​ loop in your script, don't be afraid to use it. You could always go back here and check out how to do it again. When using the "​for"​ loop, you can change whatever value a parameter has when it's in the "​for"​ loop. I use braces ({ }) to help show where exactly the "​for"​ loop is using. Also another tip is that you can use whatever variable you want for the "​for"​ loop, so long that it doesn'​t interfere with an existing variable that could do stuff that you don't want to happen. Here is and example for how a "​for"​ loop could vary: 
- 
- 
-<code cpp>void OnStart() ​ 
- 
- for (int x = 0; x <= 4; x + 2)  
- {  ​ 
- AddEntityCollideCallback("​Player",​ "​ScriptArea_+x,​ true, 1);  
- 
-} 
-</​code>​ 
- 
- 
-I also want to say that in the scripts I provided don't include the functions that could be created based on what I have in there, like a timer function and entity collide functions. 
- 
- 
-== This wiki entry has been made by Kyle S. If you have any comments or need help with this, send me a private message on the Frictional Games Forum. (My name on there is Kyle) == 
  
hpl2/tutorials/forloop.1310567670.txt.gz ยท Last modified: 2011/07/13 14:34 by kyle