performing an action on a subset of characters

Started by Giddy Plant, Tue 05/02/2008 13:41:36

Previous topic - Next topic

Giddy Plant

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

GarageGothic

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:

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

etc.

You would declare:

Code: ags
int mydudes[10];


globally, and then in game_start you could define:

Code: ags
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:

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

Radiant

Make a custom property called "next", then rather than doing "n++" in your loop, do this:

Code: ags

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



Giddy Plant

Ooooh I like that linked list approach. Thanks a bunch.

SMF spam blocked by CleanTalk