Jump to content


Photo

Instance_nearest


  • Please log in to reply
2 replies to this topic

#1 L_programmer

L_programmer

    GMC Member

  • GMC Member
  • 108 posts
  • Version:Unknown

Posted 27 January 2010 - 03:14 AM

hello, i have X objects that are the same on a path like this

place=instance_nearest(x,y,cover)}
path_end()
mp_grid_path(global.BI_grid,mypath,x,y,place.x,pla
ce.y,true);
path_start(mypath,3,0,false);}

and one makes it ther first, i want the others to choose the next closest that is not occupied already
and create a new path and go ther, how would i do that, ive tried and im stumped

another question
how would i make them choose another cover is they cannot see a target with collsion line
from the one ther at?
  • 0

#2 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 19932 posts
  • Version:GM:Studio

Posted 27 January 2010 - 07:26 AM

For your first question you could use instance_nth_nearest from GMLScripts.com to get the second nearest...

And for your second, if you have a parent for all the objects that can block the view then itīs a simple case of...

if instance_exists(obj_Player)  //This prevents errors in the rest of the code if the player dies...
{
if collision_line(x, y, player.x, player.y, obj_Blocker_Parent, true, true) //Check for collisions...
	{
	//CODE FOR CHANGING HERE
	}
}

Edited by Mark13673, 27 January 2010 - 07:26 AM.

  • 0

#3 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 27 January 2010 - 07:53 AM

For the first one, you could use the script that Mark13673 suggested, or loop through instances to search for, and work backwards.
Create a script, and call it instance_next_nearest().
Then write following...
/*  instance_next_nearest()
**  by masterofkings
**  
**  General form: instance_next_nearest(x,y,inst,prevDis)
**
**  Argument list:
**	argument0: x position to look from
**	argument1: y position to look from
**	argument2: object to look for
**	argument3: distance to previous closest instance
**
**  Usage: Find the next nearest instance, which has a distance from the
**		 x and y provided, which is greater than the previous distance
**		 Note: In order, for this to work, the objects must work off of
**			   each other, to find the prevDis argument.
*/
var xx, yy, inst, prevDis;
xx=argument0;
yy=argument1;
inst=argument2;
prevDis=argument3;

var furInst, maxDis, foundInst;
furInst=instance_furthest(xx,yy,inst);
maxDis=(point_distance(xx,yy,furInst.x,furInst.y)+1);
foundInst=noone;

with (inst)
{
  var dis;
  dis=point_distance(xx,yy,x,y)
  if (dis < maxDis)
  {
	if (dis > prevDis)
	{
	  maxDis=dis;
	  foundInst=id;
	}
  }
}
return foundInst;
And call it like...
placeObj=instance_nearest(x,y,cover)
placeDis=point_distance(x,y,placeObj.x,placeObj.y)
place=instance_next_nearest(x,y,cover,placeDis)
//then use the distance to the above place, as the 4th argument in the script.

-MoK

Edited by masterofkings, 27 January 2010 - 07:57 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users