Page 1 of 1

Adding Actors to an Array

PostPosted: Sun Apr 08, 2007 10:42 am
by jazz_e_bob
I want to make my own array of actors.

Please consider the following code:

Code: Select all
int n;  // actor count

Actor *allActors;
Actor *happyActors;

allActors = getAllActorsInCollision("gameArea.0", &n);

if(allActors)
{
    int i;
    for( i = 0; i < n; i++ )
    {
        if ( allActors[i].myMood == HAPPY )
        {
            // add to happy actor array
            // ???????????????????????? 
        }
    }
}


How do I add the happy actors to my array?

PostPosted: Sun Apr 08, 2007 12:54 pm
by makslane
Will need allocate an array of actor pointers:
Code: Select all
Actor *happyActors[32]; //for 32 actors max


And to add:
Code: Select all
happyActors[n] = allActors+i;


So, your code will look something like this:

Code: Select all
int n, nh = 0;

Actor *allActors;
Actor *happyActors[32]; //for 32 actors max


allActors = getAllActorsInCollision("gameArea.0", &n);

if(allActors)
{
    int i;
    for( i = 0; i < n; i++ )
    {
        if ( allActors[i].x == 0 )
        {
            // add to happy actor array
            happyActors[nh++] = allActors+i;           
        }
    }
}


//For access the data, use the operator ->
Code: Select all
happyActors[0]->x = 0;

PostPosted: Sun Apr 08, 2007 9:15 pm
by jazz_e_bob
Thanks mate!

:)