Jump to content


Photo

Find closest instance, find next closest instance


  • Please log in to reply
3 replies to this topic

#1 StudioGilliam

StudioGilliam

    GMC Member

  • GMC Member
  • 55 posts
  • Version:GM8

Posted 09 June 2012 - 10:56 PM

I want to tell an object to look for the closest instance of another object whose vspeed is more than zero.

I imagine I'd use something like:

var nnn;
nnn = instance_nearest(x,y,object1);
if (nnn.vspeed > 0)
{
    // do stuff
}
else // it is travelling in the wrong direction, so find the next closest instance that is travelling correctly

I just don't know how to do it.

I have good understanding of GML and computer science.
I am using GM8 Pro.

Please help!

Many thanks.

Edited by StudioGilliam, 09 June 2012 - 11:35 PM.

  • 0

#2 creators124

creators124

    awesomeliciousmember

  • GMC Member
  • 866 posts
  • Version:GM8

Posted 09 June 2012 - 11:02 PM

I want to tell an object to look for the closest instance of another object whose vspeed is more than zero.

I imagine I'd use something like:

var nnn;
nnn = instance_nearest(x,y,object1);
if (nnn.vspeed > 0)
{
    // do stuff
}
else // it is travelling in the wrong direction, so find the next closest instance that is travelling correctly

I just don't know how to do it.

Please help!

Many thanks.

her's a script pre made:
instance_nth_nearest
but you'll need to configure it!

Edited by creators124, 09 June 2012 - 11:03 PM.

  • 1

#3 StudioGilliam

StudioGilliam

    GMC Member

  • GMC Member
  • 55 posts
  • Version:GM8

Posted 09 June 2012 - 11:13 PM

her's a script pre made:
instance_nth_nearest
but you'll need to configure it!


This is certainly a useful website.
Bookmarked!

Ok. So let me get my head around what this is doing.
I've not used lists before, y'see.
So this script creates a list of instances of type object and then returns the nearest one, am I right?
So to find the nearest one with a particular vspeed, I'll need to check 'nearest' and if it's not travelling the way I want, I'll need to go to the next object on the list.
I don't know how to move to the next object on the list, though.
How's that done?

Do they work in the same way as arrays?
Could I just differentiate between them like - list[x]

Edited by StudioGilliam, 09 June 2012 - 11:24 PM.

  • 0

#4 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 17 June 2012 - 06:38 AM

recursive call

script FindThatThing(x,y,object_index)
//find nearest
var nnn;
nnn = instance_nearest(argument0,argument1,argument2);
//found
if(nnn)
{
    //matches, return it
    if (nnn.vspeed > 0)
    {
        return nnn;
    }
    //nope, try again, but stop if the one found was processed
    else if(nnn.x != -1000000) 
    {
        //the one found is not going right,
        var r;
        //move it away
        nnn.___x = x
        nnn.x = -1000000 
        //try again
        r = FindThatThing(argument0,argument1,argument2);
        //move it back
        nnn.x = nnn.___x;
        //return what was found... or noone, whatever the recursive call found
        return r;
    }
    //found a processed one, nothing found
    else return noone;
}
//nothing found, no instances
return noone;

  • 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users