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
...
}
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.
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";
}
}
}
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. :)
Throw in for-loops and switch/case, and I'd be a very happy camper indeed.
yes, I think 'for', 'switch', 'break' and 'continue' are the most often suggested ags language keywords. This should be taken into account.
I'd definitely like to see switch/case. That would make doing the custom dialogs much easier. I wouldn't mind for loops, either.
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
The easiest way to use "break" effects is putting the loop in a function and using "return".