Page 1 of 1

Using the for statment

PostPosted: Fri Jul 03, 2009 9:18 pm
by Hblade
Hey, I was wondering how the heck do you use the for statement :/

Re: Using the for statment

PostPosted: Fri Jul 03, 2009 10:11 pm
by DST
Code: Select all
for(i=0; i<10; i++){
do stuff;
}


i=0; - initializes the loop at 0. Sometimes i use i=1 because 0 means nothing is there (my first creep of each level will use slot 1, the second slot 2, etc), but usually this starts at 0.

i<10; - if the 'if' part of the statement. The loop will execute as long as this is true. So in my above loop, it will run ten times, the first being 0 and the last being 9. On the tenth loop, i will equal 10 and the for will no longer be true, and it will stop.

i++ - first, notice there is no ; at the end of this. second, this is what i will do each time the loop runs.

for instance, we can run it in reverse:

for (i=10; i>0; i--){
do stuff;}


So a great example is for an actor shooting a spread shot.
for(i=0; i<power; i++){
CreateActor("bullet", "bullet00", "no parent", "no path", 10*i, 0, false);
}

Now each bullet will appear 10 pixels over from the last one. This is great, because you can use i for all sorts of things. Since i use creator.eangle for the bullets angle, i'll use this:

eangle=120-(i*10);

Now if the power is 6, bullet one appears at 120 degrees, bullet 2 at 110 degrees, etc, with bullet 6 being at 60 degrees.

U can use I for all sorts of great stuff!

Re: Using the for statment

PostPosted: Sat Jul 04, 2009 8:28 am
by Fuzzy
for() acts as a counter loop.

Some little known facts:

You can use more than one variable in it.
Code: Select all
for(j = 10; z<k; i++){
   // stuff
}


Code: Select all
for(i++; odd(j/2); j+=i){
    // do stuff
}

untested, but it should add one before the if() test, check that half of j is an odd number(you'd need the odd function too), and then add i to j.. half of the time.

It doesnt have to be a loop with a linear count.
Code: Select all
  for(i = 1; j != dontShow; k = 3) {
   // do odd stuff with the counter
}


Why the heck would you want to do these things? I dont know. You could use it to reset the variables i and k every time the for loops. I know, you could do it inside the loop, but...

This only scratches the surface. You could do some really interesting things with for.

It really shows that one can reconstruct C using itself. Of course you need to be able to point at and execute functions, or even write code on the fly.

Which is a little tricky with GE, and dangerous at the best of times.

Re: Using the for statment

PostPosted: Tue Jul 07, 2009 4:56 am
by Hblade
Ok
I want it to do this:
When it reaches a certian level of height, it changes a value, but ONLY like this.. ok, say your yscreen is 400, then the value would be devided by the max height of the screen by 100...
It's confusing, here let me try it this way...



Look at the size of these
.

.

.
.
.

See how they got bigger when there closer to the botom, well, I want the value to be more at the botom, and less as it raises, but I dont want it to be in solid numbers, meaning I dont want it to be, when(480) value = 100, I want it to be when(480) value = .100 anyone think they can do that

Re: Using the for statment

PostPosted: Tue Jul 07, 2009 5:13 am
by skydereign
So your example is backwards? Also your division is off, it would be, when(480) value = 4.8, unless this is not be what you wanted. Also, I don't know what you are using this for or what variable you are using, so it actually has no use. I assume this is for 3d like set up using canvas, but then again, that would be different, not using a for loop.

Code: Select all
double result;
for(yscreen=0; yscreen<480; yscreen++) // this is a weird variable to be using, maybe you meant something else
{
     result=yscreen/100; // notice it doesn't use result for anything
}