Page 1 of 2

Moveto closest actor

PostPosted: Thu Dec 21, 2006 12:24 pm
by Troodon
Hi,
How can I make my actor to move to the closest "tree" actor?

PostPosted: Fri Dec 22, 2006 12:52 am
by makslane
You can use the getAllActorsInCollision to get the actors that you want to test, and use the distance function the get the smaller distance to your player.


Look more here:http://game-editor.com/docs/script_reference.htm#distance
http://game-editor.com/docs/script_refe ... ncollision
http://game-editor.com/examples/collisiongroups.zip

PostPosted: Fri Dec 22, 2006 10:26 am
by Troodon
I'll try if I can get it work with those link. Thanks

PostPosted: Tue Jan 02, 2007 12:23 am
by Troodon
I couldn't get it work. :cry:
Could someone make a example of the moveto closest actor?

EDIT: Happy new year!

PostPosted: Thu Jan 04, 2007 9:18 pm
by Troodon
Can someone help?
I still can't get it work...

I have an actor called monster and I would like it to move towards nearest actor called sini.
I readed all that the documentation is saying about distance and trying to understand the logic of the getAllactorsinCollision but I can't still figure how should I make it.

PostPosted: Fri Jan 05, 2007 1:27 pm
by makslane
Can you post the code?

PostPosted: Fri Jan 05, 2007 3:41 pm
by Troodon
I haven't got any code of moveto closest actor because I can't understand the way it works. (I just tried different things but it said that there were errors in code) If there could be some kind of tutorial/demonstration or something...

PostPosted: Fri Jan 05, 2007 4:31 pm
by makslane
Use a sensor actor (Filled Region) to act like a sensor.
Get all actors that collides with the sensor and calculate the distance.

Here is the code:

Code: Select all
int n;
double mindist = 10000; //Initialize min distance with some high value
Actor *actors, *closest = NULL;

//Get all actors that collides with the sensor actor
actors = getAllActorsInCollision("Event Actor", &n);


if(actors)
{
  int i;
  for(i = 0; i < n; i++)
  {
    if(strcmp(actors[i].name, "player") != 0) //Ignore the player actor
    {
       double d = distance(player.x, player.y, actors[i].x, actors[i].y);
       if(d < mindist)
       {
           //Hold the closest actor
           closest = (actors + i);
           mindist = d;
       }
    }
  }
}

//Move the closest actor, if any
if(closest)
{
    MoveTo(closest->clonename, 0, 0, 5, "player", "");
}

PostPosted: Fri Jan 05, 2007 4:58 pm
by Troodon
I couldn't open the example ged but I'll try that code. I haven't yet never used so complicated looking code in GE! :shock:
I write that in the filled region's draw actor?

PostPosted: Fri Jan 05, 2007 5:04 pm
by makslane
In this sample I put in the 'Mouse Button Down' event of the sensor actor

PostPosted: Fri Jan 05, 2007 5:07 pm
by Troodon
Ok, and because my monster checks always which is the closest of my actors; I put it in the draw actor of the filled.

PostPosted: Sun Jan 07, 2007 11:54 am
by Troodon
Sorry to distract, but how do I edit this line:
Code: Select all
MoveTo(closest->clonename, 0, 0, 5, "player", "");

PostPosted: Sun Jan 07, 2007 12:43 pm
by makslane
What you need?

PostPosted: Sun Jan 07, 2007 12:46 pm
by Troodon
Well, I don't completely understand the code but I want to make the "player" to move towards the closest "tree" actor.

PostPosted: Sun Jan 07, 2007 3:32 pm
by makslane
Change the line:

Code: Select all
if(strcmp(actors[i].name, "player") != 0) //Ignore the player actor


To:

Code: Select all
if(strcmp(actors[i].name, "tree") == 0) //On tree actors