GameMaker: collision_* into array

While working with GameMaker, you might have noticed that all collision_ functions return only first instance to intersect given collision shape, while in some cases you'd want to get all instances that collide with one.
This set of simple scripts adds such missing functionality...

GMS2 note: There are now built-in functions such as collision_line_list, making this script unnecessary.

So, a separate script looks like this:

// collision_line_r(x1, y1, x2, y2, obj, prec, notme)
//    argument ->   0   1   2   3   4    5     6
// Idential to collision_line, but will add matching
// instances to result[] array. Zero-index element will
// indicate length of array.
// Returns: if anything was found
global.result[0] = 0;
with (argument4) {
    // handle 'notme' argument:
    if (argument6) if (id == other.id) continue;
    // skip to next instance if not colliding:
    if (!collision_line(argument0, argument1, argument2,
        argument3, id, argument5, false)) continue;
    // add to results:
    global.result[0] += 1;
    global.result[global.result[0]] = id;
}
return (global.result[0] > 0);

Having all those comments, it should be rather easy to tell how to modify this script to use it with other collision functions. But just in case it is not, you can download all these along with example here:

Download GMK

Related posts:

5 thoughts on “GameMaker: collision_* into array

    • Fixed example link and post.
      For some reason the uploaded version was missing a “globalvar” declaration for result, but that is not considered a good practice nowadays anyway, so it is prefixed with “global.” now instead.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.