GM:Studio 1,Collision array?

Discussion in 'Legacy GameMaker Community Tech Support' started by andulvar, Apr 22, 2017.

  1. andulvar

    andulvar Member

    Joined:
    Jun 24, 2016
    Posts:
    128
    I'm looking for a way to check a collision rectangle and return an array of all instance ids that fall within that collision area. Is there anything built-in to do this easily?

    Edit: I should clarify, that using a with() function is not an option in this particular case as there will be thousands of instance ids to check and I do not want to iterate through them all.

    Posted in the wrong section. Seems you can't delete your own threads?
     
    Last edited: Apr 23, 2017
  2. Joshua Allen

    Joshua Allen Member

    Joined:
    Jun 20, 2016
    Posts:
    46
    You can try something like this:
    Code:
    /// instances_get(left, top, width, height);
    
    var left    = argument0;
    var top     = argument1;
    var width   = argument2;
    var height  = argument3;
    
    instance_deactivate_region(left, top, width, height, false, true);
    var OUT_ARRAY = 0;
    var OUT_ARRAY_INDEX = 0;
    with(all) {
        OUT_ARRAY[OUT_ARRAY_INDEX++] = id;
    }
    instance_activate_all();
    
    return OUT_ARRAY;
    or this:
    Code:
    /// instances_get(x1, y1, x2, y2, obj, prec, notme);
    
    var x1    = argument0;
    var y1    = argument1;
    var x2    = argument2;
    var y2    = argument3;
    var obj   = argument4;
    var prec  = argument5;
    var notme = argument6;
    
    var out_array = 0;
    var out_array_index = 0;
    var point_array = 0;
    
    while(true) {
        var inst = collision_rectangle( x1, y1, x2, y2, obj, prec, notme );
        if (inst == noone) break;
        
        out_array[out_array_index] = inst;
        point_array[out_array_index, 0] = inst.x;
        point_array[out_array_index, 1] = inst.y;
        inst.x = -1000000;
        inst.y = -1000000;
        out_array_index++;
    }
    
    for(var i=0; i<array_length_1d(out_array); i++) {
        var inst = out_array[i];
        inst.x = point_array[i, 0];
        inst.y = point_array[i, 1];
    }
    
    return out_array;
     
    Last edited: Apr 23, 2017

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice