Frictional Games Forum (read-only)

Full Version: For loop help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys,
Id like some help on the for loops because it was a bit hard to understand for me at the wiki (im not english and dont have any experiences in c++) so in case you have some time i'd be nice if you could answer my questions.

Code:
for(int i= 1; i <6; i++)
{
    AddEnemyPatrolNode("grunt_1", "PathNodeArea_"+i, 0, "");
}

What i (think i) understand:
- i is, what is added to the function each time it is called
- i </>x: as long as it lower than 6, it returns true. That means everything will restart, when it returns false
-"PathNodeArea_" +i says, that "i" will be added to the function esch time it is called.

What i dont understand:
- what is i++ for?
-what if i want the loop to start at node 16 and not 1? Id it "PathNodeArea_16"+i then?
Quote:- what is i++ for?

i++ basically says that each time the loop executes, the variable i will increase by 1. If you wanted i to increase by 2 each time the loop executes, you would write i += 2 (which is the same as i = i + 2)

Quote:-what if i want the loop to start at node 16 and not 1? Id it "PathNodeArea_16"+i then?

You would change the initialization for the loop (the part that says int i = 1) to int i = 16.

I would suggest reading this (link) for more information.
for (variable; condition ; to do after each repetition){
do something;
}

as long as the condition is true, "do something" will be called again and again. after each call " to do after each repetition" is called. i++; is a short term for i = i + 1;

"PathNodeArea_"+i just inserts i at the end of the string. e.g. if i=2
then "PathNodeArea_"+i would resolve to "PathNodeArea_2"

if you want i to start with 16, just set i=16 ( for (int i = 16;...;..) )

maybe it helps if you take a look to the equivalent while-loop

Code:
int i = 1;
while (i<6){
   AddEnemyPatrolNode("grunt_1", "PathNodeArea_"+i, 0, "");
   i++;
}

is equivalent to

Code:
for(int i= 1; i <6; i++)
{
    AddEnemyPatrolNode("grunt_1", "PathNodeArea_"+i, 0, "");
}
Ah okay thanks. I got it now and it works Smile
If you're interested in scripting, you can learn more about various loops (while, do-while and the for loop) here.
Yes im always looking at the wiki but sometimes i dont really understand what they are exactly talking about, because im not english. But ill see if i can understand the other loops on my own Wink