Jump to content


Photo

Nearest instance, that is NOT equal to self.


  • Please log in to reply
8 replies to this topic

#1 thenoller

thenoller

    GMC Member

  • GMC Member
  • 208 posts

Posted 20 September 2010 - 07:23 AM

I'm not a novice user at all, but I felt that this belonged here.
Maybe I haven't written GML in way too long, or maybe this just has no simple solution..
How do I find the nearest instance of the same object as the one checking, but isn't that particular instance?! "distance_to_object(obj)" can't return the particular instance, and "instance_nearest(x,y,obj)" can't exclude "self" cause it operates from a point, not an instance. This is giving me a headache :wacko: any help is appreciated...!
-Nils.
  • 0

#2 Eva unit-01

Eva unit-01

    GMC Member

  • New Member
  • 771 posts

Posted 20 September 2010 - 07:34 AM

Did you try setting it up as a variable?
like
other_object=instance_nearest(x,y,obj)
  • 0

#3 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 20 September 2010 - 08:04 AM

/*  instance_nearest_ext()
**  by masterofkings
**
**  General form: instance_nearest_ext(x,y,obj,id);
**
**  Usage: Finds nearest instance of object, excluding self.
**         noone is none are found.
*/
var obj,dis,tDis;
obj=noone;
dis=1000000;
with (argument2) {
    if (id!=argument3) {
        tDis=point_distance(argument0,argument1,x,y);
        if (tDis < dis) {
            dis=tDis;
            obj=id;
        }
    }
}
return obj;

Then call as...
obj=instance_nearest_ext(x,y,obj,id);

Note: Tested. This code works (id must be used for the 4th argument; for some reason self doesn't work).

-MoK

Edited by masterofkings, 20 September 2010 - 08:19 AM.

  • 0

#4 thenoller

thenoller

    GMC Member

  • GMC Member
  • 208 posts

Posted 20 September 2010 - 08:42 AM

/*  instance_nearest_ext()
**  by masterofkings
**
**  General form: instance_nearest_ext(x,y,obj,id);
**
**  Usage: Finds nearest instance of object, excluding self.
**         noone is none are found.
*/
var obj,dis,tDis;
obj=noone;
dis=1000000;
with (argument2) {
    if (id!=argument3) {
        tDis=point_distance(argument0,argument1,x,y);
        if (tDis < dis) {
            dis=tDis;
            obj=id;
        }
    }
}
return obj;

Then call as...
obj=instance_nearest_ext(x,y,obj,id);

Note: Tested. This code works (id must be used for the 4th argument; for some reason self doesn't work).

-MoK


Now very efficient for the thing I was going to do (several uses each step), but I trust that this is the way to do it. Thanks so much :)
  • 0

#5 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 20 September 2010 - 09:01 AM

No, it isn't efficient; but I can't think of any other way to do it.

-MoK
  • 0

#6 ND4SPD

ND4SPD

    GMC Member

  • GMC Member
  • 2180 posts

Posted 20 September 2010 - 09:30 AM

No, it isn't efficient; but I can't think of any other way to do it.

-MoK

Not sure if this is more efficient, but you could use instance_nth_nearest() and get the 2nd instance?
GMLscripts.com :: instance_nth_nearest()
  • 0

#7 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 20 September 2010 - 09:33 AM

instance_nth_nearest() would use more memory and be no faster than that of the script I provided. Why? Because, it does exactly the same, but arranges instances based on distance. It still loops through them all.

-MoK
  • 0

#8 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 19925 posts
  • Version:GM:Studio

Posted 20 September 2010 - 09:36 AM

Easy... In the event you want...

var xx;
xx=x; //Temp variable to hold the current x-coord...
x-=1000000; //Move the instance 1 million pixels...
inst=instance_nearest(xx,y,object_index); //Check for the nearest instance to where it WAS...
if inst=id show_message("You found yourself!"); //if it finds itself show a message (debug only! can be deleted!)...
x=xx; //Return the object to itīs original position...

There you go. With this you will find the nearest instance to the same object unless either no other objects exists OR the distance between the same objects is greater than 1000000 pixels (un-likely!!!)...

Hope that helps!
  • 0

#9 ragarnak

ragarnak

    GMC Member

  • Retired Staff
  • 19468 posts
  • Version:GM8

Posted 20 September 2010 - 10:58 AM

How do I find the nearest instance of the same object as the one checking, but isn't that particular instance?!

By using the trick "masterofkings" showed you, but without the "with" loop (which I think is not needed) :
x-=10000
inst=instance_nearest(x+10000,y,object_index)
x+=10000
Hope that helps.

[edit]
Shucks ... Much too late, "Mark13673" already posted it.

Edited by ragarnak, 20 September 2010 - 11:00 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users