Page 1 of 1

Using a Function to Control Several Actors

PostPosted: Fri May 26, 2006 5:14 pm
by plinydogg
Whoa people! I've got a tough question (tough for me anyway) and I'm not even sure how to describe it, but I'll do my best. Any help is greatly appreciated.

*Background:* I'm making a turn-based game in which there could be any number of spaceships in the air at any given time. When each is launched, a destination is selected by the player and the ship heads towards its destination at a predetermined speed. Ships are supposed to continue to approach their destinations when the player presses the "next turn" button.

*The problem:* How can I get each airborne ship to call the function that contains the code to move?

*Things I've Already Tried:*

--I created an int variable called shipCount, which is used to keep track of how many ships exist at any given time. Then, I create pointers to each ship as it is created. Something like this:

Actor *ships[30]; //in global code

void createShip()
{
ships[shipCount] = CreateActor("ship", "default", etc. etc.);
shipCount++;
}

Then I created a nextTurn() function that looked more or less like this:

void nextTurn()
{
int i;
for(i = 0; i < shipCount; i++)
{
ships[i]->shipMove(); //this line wouldn't work
}
}

This wouldn't work. So the problem remained: how can I iterate through a group of actors and get each actor to call a function?

After this attempt failed, I thought it might just be a syntax problem so I replaced the function call with the actual code contained in the shipMove() function. While GE allowed me to do this, it didn't work when I ran the game (i.e., the ships didn't move). However, I know that the code itself is good because I can get it to work properly on a mousedown event.

I apologize for the complex post and question and am deeply grateful for anyone's insight.

Plinydogg

PostPosted: Fri May 26, 2006 6:58 pm
by Novice
I dont know the right syntax for calling functions by pointers but you could try to do it this way. Create an aray that holds the state of your ships.
Code: Select all
int ship[30];

then when the turn is changed make the curents ship variable 1 for ex. shp[15]=1;
In the ship's script editor for draw actor put
Code: Select all
if (ship[15]==1)
{
ship[15]=0//to break out of the loop
shipMove();
}

Im in a real hurry now but i think this should work with some variables instead of ship nubmers and so on. Il get back at this as soon as im home.

PostPosted: Fri May 26, 2006 7:53 pm
by plinydogg
Novice,

Thanks a million for the advice! I think I understand what you're suggestion. I just remembered that I had a similar idea earlier on but had since forgotten it.

I think the idea was that I would create a global variable that counted the number of turns that had elapsed since the game began. Then I would create an actor variable on each ship when it was created and set it equal to the turns variable. Finally, on a draw actor event if the actor variable was less than the turns variable, the code would execute.

I'll probably be too busy for the rest of the afternoon/evening but I'll try you're idea first thing in the morning, if not before. Thanks again!

Re: Using a Function to Control Several Actors

PostPosted: Fri May 26, 2006 11:55 pm
by DilloDude
plinydogg wrote: ships[i]->shipMove(); //this line wouldn't work
Plinydogg

You could probably achieve this if you made the ship a parameter int the shipMove function:
Code: Select all
void shipMove(Actor *ship)
{
    ...
}

shipMove(ships[i]);

PostPosted: Sat May 27, 2006 7:01 pm
by plinydogg
Both of you, thanks for your help. Unfortunately, I won't be able to report success or failure yet because of an unrelated problem that I'm going to have to sort out first, but I will get back to you when I do. If you think of anything else in the meantime, I'm all ears (although I suspect both ways you've already suggested will work fine).