GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2008-12-19 21:32:12

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

instance_nearest_advanced

Expand/*
**  Usage:
**   id = instance_nearest_advanced(x,y,object, comparescript, maxd, internaluse);
**
**  Arguments:
**   x        x coord
**   y        y coord
**   obj     object to find
**   comparescript    A compare script to use to decide if the instance found is a match
**   maxd    maximum check range
**   internaluse, ignore this parameter, either ommit it or pass 0
**
**  Returns:
**   id       the id of the instance found or noone if none or self is found or instance found is beyond maxd
**
**  Notes:
**   This is a recursive function, it moves every instances out of the way,
**     like instance_nearest_notme to ensure a quick search.
**   The script calls itself over and over, moving instances that did not match
**     the search criteria out of the way untill an instance is found or maxd is reached
**
**   you MUST provide a compare script
**
**   id = instance_nearest_advanced(x,y,ShipsObj, GroupIsNotSame_scr, 300);
**
**  GMLscripts.com
*/

var ret; ret = noone;
var xx;
if(argument5)
{
    with(instance_nearest(argument0,argument1,argument2))
    {
        if(x = -10000000) return noone;
        if(point_distance(x,y,argument0, argument1)>=argument4) return noone;
        if(script_execute(argument3, argument5, id)) return id;
        xx = x;
        x = -10000000
        ret = instance_nearest_advanced(argument0, argument1, argument2, argument3, argument4,argument5)
        x = xx;
    }
}
else
{
    xx = x;
    x = -10000000
    ret = instance_nearest_advanced(argument0, argument1, argument2, argument3, argument4, id)
    x = xx;
}
return ret;

This is an example compare script

Expand/*
**  Example Script GroupIsNotSame_scr
**
**  Usage:
**   Called by instance_nearest_advanced
**
**  Arguments:
**   argument0 is the instance performing the search
**   argument1 is the found instance
**
**  Returns:
**   0 if the creteria is no met
**   1 if the criteria is met
**
**  Notes:
**
**   This is only an example script you pass to instance_nearest_advanced.
**   Don't expect it to work in your game
**   It returns true if the m_Group variables present in both instances
**     (searcher and found) are different, useful from having same types/class
**     of object instances seaching for instances that are not in the same group
**     in order to shoot at them.
**
**   You can fill a GLOBAL list with all instances matching the creteria, simply return 0 all the time
**     and add the id to a list if the creteria is met. The list will hold, from nearest to furthest
**     all instances (in range) that matches the criteria... This is usually faster than a with() statement
**     WHEN you have many instances in the room and they are not all in range
**
**  GMLscripts.com
*/

return (argument0.m_Group != argument1.m_Group)
//example list
//assumes result_list was created and emptied before the instance_nearest_advanced was called
if(argument0.m_Group != argument1.m_Group)
{
    ds_list_add(result_list, argument1)
}
return 0;

Last edited by icuurd12b42 (2012-11-20 00:47:42)

Offline

#2 2012-11-20 17:32:41

paul23
Member
Registered: 2007-10-17
Posts: 110

Re: instance_nearest_advanced

Hmm would split it in 2 scripts..

Also it is recursion, with the limited stack-function depth (128 I though was the limit in GM:HTML & GM8.1, mike commented and said it somewhere at the bug tracker and it surprised me that it was so low) it is risky. As it is tail recursion the functions can be easily changed to iterative approach.

Offline

#3 2013-12-17 06:35:32

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: instance_nearest_advanced

Be warned, this is now super slow in GM:Studio; it is best to use the most commonly known method
with(object_to_search) if in range and closest found, remember it... or add to a priority queue indexed on distance to return multiple instances

the cause of the slowdown is instance_nearest() which performs it's own internal with(object_to_search), obviously no aabb type search is used, so the end result using this method is multiple with(object_to_search) calls. as many as the number of instances in range... so you are better off doing your own one single pass through using your own code in a  with(object_to_search). with regular output, this method is 10 times slower than a with(). with YYC it is about 100 times slower than a with().

lastes result with 1000 instance, 100 in reange
this method
Regular Windows Output 10fps
YYC Output 15fps
with() method
Regular Output 130 fps
yyc output 1000+ fps

Last edited by icuurd12b42 (2013-12-17 06:40:00)

Offline

#4 2013-12-18 15:57:41

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: instance_nearest_advanced

That's a shame. It really bugs me when GM:S is sometimes very slow compared to older editions, even when compiled.

Offtopic: I'd love to work out how to predict when the compiler is going to choke. When Chrome's V8 Javascript engine handily outperforms native compilation, something seems terribly wrong with YYC.


Abusing forum power since 1986.

Offline

#5 2014-08-24 09:49:19

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: instance_nearest_advanced

I have not been around for a bit.... I think the problem is GM likely had some sort of aabb system where studio does not...

TMC SGS solves this issue with the grid system and Implemented a instance nearest for it as well

Offline

Board footer

Powered by FluxBB