Page 1 of 1

Selecting a randomly-created actor using getactor

PostPosted: Tue Mar 14, 2006 2:39 pm
by plinydogg
(1) I use the following code to create 50 planets and to put the coordinates for each planet into arrays:

int i;
int PlanetIterator = 0;
for (i = 0; i < 50; i++)
{
int randX = (round(rand(720))) + 600; //random x coordinate
int randY = (round(rand(615))) - 465; //random y coordinate
planetX[planetIterator] = randx;
planetY[planetIterator] = randy;
CreateActor("planetSmallBlue", "neutral", "no parent", "no path",
randx, randy, true);
planetIterator++;
}

(2) I want to select one of the randomly created planets to be the home base for the player. How do I do this?

I think I somehow need to use getactor() with the x and y coordinates stored in the arrays (that's why I created the arrays) but I'm not sure how. I have looked at other threads, which talk about how to see if a particular actor is at a given place, but I need to somehow select the actor that is at that place (i.e., I need to make it the "Event Actor").

Any ideas? Thanks.

PostPosted: Tue Mar 14, 2006 3:22 pm
by twobob
You can control all of your created clones with a pointer to the clones when they are created.
Declare an array as follows in the global code editor:

Actor *myplanets[50];

When you create your planets do the following when creating:
myplanets[i] = CreateActor("planetSmallBlue", "neutral", "no parent", "no path", randx, randy, true);
(i is the counter in the for statement)

In your program you can now control any created actor by the following method:
getclone(myplanets[2]->clonename)->x = 234;
getclone(myplanets[2]->clonename)->y = -432;
change the 2 in brackets for the clone you want

To randomly select an actor you could use:
Actor *chosenPlanet;

chosenPlanet = myplanets[round(rand(50))];

Not sure if this is what you are after – I think there is also a function to get an actor at a chosen x, y coordinate.

PostPosted: Tue Mar 14, 2006 5:09 pm
by plinydogg
Thanks twobob! I'll give it a whirl and report back in a day or so!

PostPosted: Tue Mar 14, 2006 5:27 pm
by plinydogg
twobob,

I just did a preliminary run of what you suggested and most of it seems to work fine. However, I got some kind of illegal conversion message when I tried to add the following code:

chosenPlanet = planets[1];
ChangeAnimation(chosenPlanet, "yellow", NO_CHANGE);

The pointer array and the pointer to the particular planet (chosenPlanet) seem to initialize fine. The problem seems to be with the use of a pointer (chosenPlanet) instead of a string literal (e.g., "planetsActualName") with ChangeAnimation(). I suspect that I somehow have to dereference the pointer or use some kind of C string conversion command.

Any ideas...again? =)

PostPosted: Tue Mar 14, 2006 5:35 pm
by twobob
try:
ChangeAnimation(chosenPlanet->clonename, "yellow", NO_CHANGE);

PostPosted: Tue Mar 14, 2006 6:06 pm
by plinydogg
It worked! IT WORKED! Thanks twobob! Thanks a million!

PostPosted: Wed Mar 15, 2006 4:45 pm
by plinydogg
twobob,

I've got a follow up question if you don't mind.

How would you go about doing roughly the same thing from the opposite direction. For example: let's say the user clicks on one of the 50 planets. How can I determine which pointer points to that planet (since the planets will have been randomly placed, the planet next to myplanets[1] might be myplanets [2] or it might be myplanets[34].

Does that make sense?

Thanks for all your help!

PostPosted: Wed Mar 15, 2006 5:28 pm
by twobob
You can use the script editor on a mouse down event to get the clone name(or the index for the pointer array)
This event will be for any of your 50 planets
For example:
Add a mouse button down event(for all your planets) - add action - script editor.
When the user clicks on the planet you can either do whatever processing is to be done with that clone in the script editor, or you can copy the clonename into a text actor for further use outside the event actor script.

If you want to copy the clonename to a text actor in the script on the mouse down event do the following:
strcpy(copytext.text, clonename);//where copytext is a text actor

Then you can use this text actor in some other script :
getclone(copytext.text)->x = getclone(copytext.text)->x+10;
or
MoveTo(copytext.text, 0.000000, 0.000000, 1.000000, "Game Center");

You could also in the script save the cloneindex of the actor that has been clicked into a int variable and use that in the pointer array
myplanet[savedInt]->x = 234;

Hope it all makes sense and it has given you some ideas.

PostPosted: Wed Mar 15, 2006 7:14 pm
by plinydogg
It sure has given me some ideas! Thanks again. I've got to leave early tomorrow morning for a wedding but I'll let you know how things work out in a few days.