• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Windows Finding nearest that meets requirements

I'm pretty basic at GMS2 script and I'm trying to learn new stuff every day but I struggle to get my head around stuff I haven't dabbled with before at all and would like some help (please annotate what the code does if you post any as I may not understand its purpose or how to manipulate it, thank you).

So I am attempting for an instance to check nearby instances and to return the nearest that meets requirements, all of the instances in the code will be the same type of instance but will have different values to their variables. example:

obj_animal wants to form a list of all obj_animal that meet the following requirements:
var range = distance to other or var range > distance to other
var targetsize = other.size or > other.size
var targetthreat = other.threat or > other.threat

Then it will return the nearest object that meets those 3 criteria.

I imagine it will be either using a list, map or an array to store the instances but I don't know how to order them exactly and I'm sure there is probably a simple way of grouping all of these into a list that can be deleted once used.


I don't know any built-in way to find / sort with constraints in gml, except for perhaps you can use script_execute to call constraint scripts (would love to have a way without script_execute).
I'd just a "with (obj_animal)" to loop through all instances and keep the most fitted one.