Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: monkey0506 on Tue 05/04/2005 23:29:34

Title: While loop error (array index out of bounds) (SOLVED)
Post by: monkey0506 on Tue 05/04/2005 23:29:34
I have two while loops that are causing me some heart-ache:

  while ((i > -1) && (dlgnum[dialog].optstate[i] != 0)) { i--; }
  while ((j < 30) && (dlgnum[dialog].optstate[j] != 0)) { j++; }


The problem is that the game appears to be checking the whole while statement, even if the first half proves to be false.  I need to run through these two variables where they are within the bounds defined (i > -1 and j < 30) and the optstate variable with index i/j does not equal zero.  If either statement proves false I have to break immediately.  Any help greatly appreciated.

BTW, AGS 2.7 Beta...
Title: Re: While loop error (array index out of bounds)
Post by: strazer on Tue 05/04/2005 23:49:00
http://www.adventuregamestudio.co.uk/tracker.php?action=detail&id=509
Title: Re: While loop error (array index out of bounds)
Post by: monkey0506 on Wed 06/04/2005 00:21:49
So...I'm basically screwed until this gets fixed?
Title: Re: While loop error (array index out of bounds)
Post by: strazer on Wed 06/04/2005 00:25:54
Try something like


  while (i > -1) {
    if (dlgnum[dialog].optstate[i] != 0) i--;
    else i = -1;
  }


Edit:

Or, since I reckon you need "i" later on, something like this:


  int break = 0;
  while ((i > -1) && (break == 0) {
    if (dlgnum[dialog].optstate[i] != 0) i--;
    else break = 1;
  }
Title: Re: While loop error (array index out of bounds)
Post by: monkey0506 on Wed 06/04/2005 13:46:54
Actually I was thinking about it last night and I think I'm going to try this:

short break = 0;
short i = dlgVariable(0) - 1;
while ((dlgnum[dialog].optstate[i] != 0) && (break == 0)) { /* option i not off by scrolling */
  if (i > -1) i--;
  else break = 1;
  }
if (break == 1) break = 0;
short j = dlgVariable(1) + 1;
while ((dlgnum[dialog].optstate[j] != 0) && (break == 0)) { /* option j not off by scrolling */
  if (j < 30) j++;
  else break = 1;
  }


I'm actually looking for the first member of the optstate array (starting at the top shown option - 1 and going backwards (for i) and starting at the bottom shown option + 1 and going forwards (for j)) that is set to 0.  Actually, now that I read your second bit of code I think they would do the same thing...  Thanks for the help.