Frictional Games Forum (read-only)
For loop help - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: For loop help (/thread-20375.html)



For loop help - tonitoni1998 - 02-17-2013

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?


RE: For loop help - NaxEla - 02-17-2013

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.


RE: For loop help - darksky - 02-17-2013

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, "");
}



RE: For loop help - tonitoni1998 - 02-17-2013

Ah okay thanks. I got it now and it works Smile


RE: For loop help - TheGreatCthulhu - 02-17-2013

If you're interested in scripting, you can learn more about various loops (while, do-while and the for loop) here.


RE: For loop help - tonitoni1998 - 02-18-2013

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