Moveto closest actor

Talk about making games.

Moveto closest actor

Postby Troodon » Thu Dec 21, 2006 12:24 pm

Hi,
How can I make my actor to move to the closest "tree" actor?
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby makslane » Fri Dec 22, 2006 12:52 am

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
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby Troodon » Fri Dec 22, 2006 10:26 am

I'll try if I can get it work with those link. Thanks
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby Troodon » Tue Jan 02, 2007 12:23 am

I couldn't get it work. :cry:
Could someone make a example of the moveto closest actor?

EDIT: Happy new year!
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby Troodon » Thu Jan 04, 2007 9:18 pm

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.
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby makslane » Fri Jan 05, 2007 1:27 pm

Can you post the code?
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby Troodon » Fri Jan 05, 2007 3:41 pm

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...
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby makslane » Fri Jan 05, 2007 4:31 pm

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", "");
}
Attachments
move_closest_actor.zip
Project sample
(16.5 KiB) Downloaded 312 times
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby Troodon » Fri Jan 05, 2007 4:58 pm

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?
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby makslane » Fri Jan 05, 2007 5:04 pm

In this sample I put in the 'Mouse Button Down' event of the sensor actor
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby Troodon » Fri Jan 05, 2007 5:07 pm

Ok, and because my monster checks always which is the closest of my actors; I put it in the draw actor of the filled.
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby Troodon » Sun Jan 07, 2007 11:54 am

Sorry to distract, but how do I edit this line:
Code: Select all
MoveTo(closest->clonename, 0, 0, 5, "player", "");
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby makslane » Sun Jan 07, 2007 12:43 pm

What you need?
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby Troodon » Sun Jan 07, 2007 12:46 pm

Well, I don't completely understand the code but I want to make the "player" to move towards the closest "tree" actor.
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby makslane » Sun Jan 07, 2007 3:32 pm

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
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Next

Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest