Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loop help
tonitoni1998 Offline
Member

Posts: 163
Threads: 54
Joined: Oct 2012
Reputation: 1
#1
For loop help

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.

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?

When you are looking for someone, to do the scripting for your Custom Story, ask me!
02-17-2013, 11:07 PM
Find
NaxEla Offline
Senior Member

Posts: 415
Threads: 5
Joined: Dec 2012
Reputation: 28
#2
RE: For loop help

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.

In Ruins [WIP]
02-17-2013, 11:19 PM
Find
darksky Offline
Member

Posts: 52
Threads: 8
Joined: Nov 2012
Reputation: 2
#3
RE: For loop help

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

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

is equivalent to

for(int i= 1; i <6; i++)
{
    AddEnemyPatrolNode("grunt_1", "PathNodeArea_"+i, 0, "");
}
(This post was last modified: 02-17-2013, 11:23 PM by darksky.)
02-17-2013, 11:23 PM
Find
tonitoni1998 Offline
Member

Posts: 163
Threads: 54
Joined: Oct 2012
Reputation: 1
#4
RE: For loop help

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

When you are looking for someone, to do the scripting for your Custom Story, ask me!
02-17-2013, 11:25 PM
Find
TheGreatCthulhu Offline
Member

Posts: 213
Threads: 10
Joined: Oct 2010
Reputation: 32
#5
RE: For loop help

If you're interested in scripting, you can learn more about various loops (while, do-while and the for loop) here.
02-17-2013, 11:51 PM
Find
tonitoni1998 Offline
Member

Posts: 163
Threads: 54
Joined: Oct 2012
Reputation: 1
#6
RE: For loop help

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

When you are looking for someone, to do the scripting for your Custom Story, ask me!
02-18-2013, 01:45 AM
Find




Users browsing this thread: 2 Guest(s)