Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Knox on Tue 01/03/2011 19:22:56

Title: A "while" loop...deleting an item in that while loop count...**SOLVED**
Post by: Knox on Tue 01/03/2011 19:22:56
Hi,

Im running into a problem mostly due to my newbie-ness: Ive got a while loop using the number of inventory items in a certain "myInventoryWindow" window. Inside this loop, I run a script that does stuff + and eventually decides (depending on various factors) if that item "i" should be deleted from myInventoryWindow.

The problem is, if I delete an item in that window from inside the while loop, how in the crap am I supposed to update "iMyItemCount" so that it diminishes its amount?

Im guessing I have to use a dynamic array?


//Pseudo code:

 iMyItemCount = myInventoryWindow.ItemCount;
 int i = 0;
 while (i < iMyItemCount)
 {
       functionThatDeletesItemFromInvWindow(); //processes 1 item at a time, to see if it must be deleted
       i++;
 }
Title: Re: A "while" loop...deleting an item in that while loop count...and dynamic arrays?
Post by: Khris on Tue 01/03/2011 19:31:23
Hmm, I'd say this should do:

  if (item was deleted) iMyItemCount--;
  else i++;


The while condition is checked every loop, that's the whole point of "while". So if you've just removed InvWindow.ItemAtIndex[i], simply decrease iMyItemCount by one and don't increase i since the deletion moves the next item to the current i and the end one down.
Title: Re: A "while" loop...deleting an item in that while loop count...and dynamic arrays?
Post by: Knox on Tue 01/03/2011 19:45:06
oh wow, I did not know we could do that! Geeze, I was afraid I'd have to use global vars, dynamic arrays, etc...shows how noob I am! :P

You just saved me a crapload of work man...
Title: Re: A "while" loop...deleting an item in that while loop count...**SOLVED**
Post by: Calin Leafshade on Tue 01/03/2011 19:58:58
the best way to do this is to make your functionThatDeletesItemFromInvWindow(i) function return true or false depending on whether or not an item was deleted. That's nice and neat