3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1]

Author Topic: Need help on comparators (while when for) + switch & break  (Read 548 times)

« on: April 24, 2011, 03:16:22 PM »
I want to learn how to use all these comparators and the switch & break, but I was reading through the script tutorial on these and I'm just not understanding them.. ???

I know I should have learned these long ago, but I have always got away with using if() statements.

Could any advanced scripters here post some .3dr projects showing how these work in their simplest form?

-the simpler, the better

jestermon

« Reply #1 on: April 24, 2011, 04:55:24 PM »
The best is to study the statements from a c / c++ reference since they are exactly the same as for Angelscript
Here's one of millions of good references
http://www.java2s.com/Tutorial/C/0120__Statement/Thewhileloop.php

Here's a quick overview of the for, and the while

For: The for loop is used to itterate over a series of instructions,
and uses a step value to increase the base counter

The base counter is a variable, usually an int, and the letter i is
ofter used, from the original c documentation

for(i=start; i < end; i = i + step){
   //do something
}

example:
for(i=0; i<20; i++){  //use i base, start counting from 0, while i is less than 20, and increase base i by step 1
}

example:
for(i=0; i<40; i+2){  //use i base, start counting from 0, while i is less than 40, and increase base i by step 2
}

example:
for(i=30; i<0; i--){  //use i base, start counting from 30, while i is greater than 30, and decrease base i by step 1
}

i++ means i += 1
i-- means i -= 1

If you use a break inside the for loop, it will exit the loop at that point
If you use a continue inside the for loop, it will skip the rest of the instructions in the
for loop, and start with the next step increment

example:
for(i=0; i<20; i++){
   n+=i;
   m = n*3;
   if(m == 16){
     break;       //the for loop will exit and terminate here, going onto the instruction after the close of the loop
   }
   p = m/(n+1);  //this will be skipped
   q = p+2;      //this will be skipped
}

for(i=0; i<20; i++){
   n+=i;
   m = n*3;
   if(m == 16){
     continue;  //the lines below will be skipped, and the loop will continue with the next step value if i
   }
   p = m/(n+1);  //this will be skipped
   q = p+2;      //this will be skipped
}

while: Is similar to for, except that it does not havea base or a step counter
You need to make sure that a condition is met in order to finalize the while loop

example:
int i = 0;       //start with some value
while(i < 10){   //while i is smaller than 10, do everything in the block, if i is = 10, then exit the loop
   i++;
   //do something
}

The break statement also works inside a while loop, and exits the loop when used

Refer to c documentation for more detail and info this and on on "switch-case"
« Reply #2 on: April 24, 2011, 09:37:19 PM »
So for the "for" comparator, it basically does every step in one script loop?

Thanks for the explanation btw. :) It's really helpful.

Quote
You need to make sure that a condition is met in order to finalize the while loop
If it doesn't get finalized, does 3DRad freeze?
I was messing with the while loop.. Syntax checker was alright, and started the project and 3DRad froze. ???

« Last Edit: April 24, 2011, 09:51:50 PM by Rush3Fan »

jestermon

« Reply #3 on: April 24, 2011, 10:04:35 PM »
A While is something that can create what is called a "deadly embrace", which means that if the condition is never met, it will run in a loop forever. And yes it will freeze any program that falls into this "trap". Be very careful with while loops, you need to think the logic through very carefully

No idea why the for loop froze 3D Rad, unless it was something like
for(i=0 ;i<10000000 ;i++) which can take forever on every frame..
Don't use loops that are too long (large end value), since it has to run every 1/6oth of a second, and scripts are not really that fast.

The for loop will repeat everything between the braces "{}" (only once for every step of the counter) until the base counter has reached the end value, no more, no less - unless you "break" from the loop.

loops are very handy, they can even write games (referring to "friend" loop with this pun) :D
« Last Edit: April 24, 2011, 10:12:40 PM by jestermon »
« Reply #4 on: April 25, 2011, 02:14:21 PM »
That makes alot more sense now..

I'm going to try become more familiar with these now and put them to use.

Quote
No idea why the for loop froze 3D Rad

Quote
I was messing with the
while
 loop..
« Last Edit: April 25, 2011, 02:16:45 PM by Rush3Fan »
Pages: [1]