Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Giddy Plant on Tue 05/02/2008 13:41:36

Title: performing an action on a subset of characters
Post by: Giddy Plant on Tue 05/02/2008 13:41:36
A simple question - is it possible to run a while loop over a subset of characters named cChar1, cChar2 etc. that do not have consecutive id numbers? Or am I stuck with using a custom property or similar and having to check against that whilst iterating over the whole set?

Thanks
Title: Re: performing an action on a subset of characters
Post by: GarageGothic on Tue 05/02/2008 14:44:06
Well, you could set up an array of the character ID's used by cChar1, cChar2 and so on, and then iterate through that.

Thus instead of doing:

int n;
while (n < 10) {
   character[n].DoStuff();
   n++;
   }

etc.

You would declare:

int mydudes[10];

globally, and then in game_start you could define:

mydudes[0] = cChar1.ID; //or just the plain number, you get the point
mydudes[1] = cChar2.ID;
mydudes[2] = cChar3.ID;
//...
mydudes[9] = cChar10.ID;


then in the function, do this:

int n;
while (n < 10) {
   character[mydudes[n]].DoStuff();
   n++;
   }
Title: Re: performing an action on a subset of characters
Post by: Radiant on Tue 05/02/2008 15:08:51
Make a custom property called "next", then rather than doing "n++" in your loop, do this:


int n = SOME_CHARACTER
while (n != 0) {
  do stuff
  n = character[n].next;
}


Title: Re: performing an action on a subset of characters
Post by: Giddy Plant on Wed 06/02/2008 13:12:25
Ooooh I like that linked list approach. Thanks a bunch.