Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Lt. Smash on Fri 07/11/2008 15:09:18

Title: [suggestion] loop codes 'break' & 'continue'
Post by: Lt. Smash on Fri 07/11/2008 15:09:18
what would be the best approach to convert similar things like this into ags code:


String input (String s) {
int i;
char c;

while (i < s.Length -1) {
  if(s.Chars[i] != 'R')
    continue;   //return to top of loop, i++;
  if(s.Chars[i] == 'B')
    break; //continue after while loop
  if(i == 0 || c == '^')
  {
     s = "blabal";
     continue;
  }
  if(c == 'x')
     s = "blabalba";
}
   //break would continue here
...
}

Title: Re: loop codes 'break' & 'continue'
Post by: Gilbert on Fri 07/11/2008 15:44:09
To break a while loop, just set the running index to something that will stop the loop from running.

For example, in your case, just set the value to s.Length-1 or greater.

Of course those "if(i == 0 || c == '^') blah blah: codes will still be executed in your example, you may mess with those if's so that it won't do that.
Title: Re: loop codes 'break' & 'continue'
Post by: Pumaman on Fri 07/11/2008 16:50:32
To convert a "continue", just reverse the "if" logic and put the rest of the code inside the "if" for the rest of the loop.

For example, your "continue"s can be be converted like this:

while (i < s.Length -1) {
  if(s.Chars[i] == 'R')
  {
    if(s.Chars[i] == 'B')
      break; //continue after while loop
    if(i == 0 || c == '^')
    {
       s = "blabal";
    }
    else
    {
     if(c == 'x')
       s = "blabalba";
     }
  }
}
Title: [suggestion] loop codes 'break' & 'continue'
Post by: Lt. Smash on Sat 15/11/2008 10:29:23
I changed this thread into a suggestion and everyone can post in here, who would find continue and break support for loops useful.
Maybe we can get CJ to think about an implementation. maybe


I know its not on his priority list but CJ said some time ago that he would like to implement it. Maybe the time has come.  :)
Title: Re: [suggestion] loop codes 'break' & 'continue'
Post by: Snarky on Sat 15/11/2008 15:50:47
Throw in for-loops and switch/case, and I'd be a very happy camper indeed.
Title: Re: [suggestion] loop codes 'break' & 'continue'
Post by: Lt. Smash on Sat 15/11/2008 16:37:34
yes, I think 'for', 'switch', 'break' and 'continue' are the most often suggested ags language keywords. This should be taken into account.
Title: Re: [suggestion] loop codes 'break' & 'continue'
Post by: Shane 'ProgZmax' Stevens on Sat 15/11/2008 18:23:56
I'd definitely like to see switch/case.  That would make doing the custom dialogs much easier.  I wouldn't mind for loops, either.
Title: Re: [suggestion] loop codes 'break' & 'continue'
Post by: Trent R on Sat 15/11/2008 19:45:26
I personally have always like foreach loops, but implementing them depends on AGS works internally. I'm thinking of something like foreach Character* in Room1, blah blah do stuff. Or something like that.

~Trent
Title: Re: [suggestion] loop codes 'break' & 'continue'
Post by: Radiant on Wed 19/11/2008 13:00:30
The easiest way to use "break" effects is putting the loop in a function and using "return".