GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2007-11-04 13:34:05

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

collision_rectangle_list - optimalization:

well I stumbled across this script and found a bug + room for optimizing:

first of all, the script uses a datastructure, but it doesn't delete the datastructure afterwards: this leads to memory leaking and should've been prevented..

secondly using a stack (and a queue) is slightly faster, and because you don't have to use a for loop then, but instead you can simply use a "simple repeat" (no indexing variable)  it will become a LOT faster (as shown in my perftest program):

/*
**  Usage:
**      collision_rectangle_list(x1,y1,x2,y2,obj,prec,notme)
**
**  Arguments:
**      x1,y1       first corner of the collision rectangle (filled)
**      x2,y2       opposite corner of the collision rectangle (filled)
**      obj         object to check for collision
**      prec        set to true for precise collision checking
**      notme       set to true to ignore the calling instance
**
**  Returns:
**      a ds_list id, or keyword noone if no instances are found
**
**  GMLscripts.com
*/
{
    var x1,y1,x2,y2,obj,prec,notme,dsid,i;
    x1 = argument0;
    y1 = argument1;
    x2 = argument2;
    y2 = argument3;
    obj = argument4;
    prec = argument5;
    notme = argument6;
    i = collision_rectangle(x1,y1,x2,y2,obj,prec,notme);
    if (i) {
        dsid = ds_stack_create();
        while (i) {
            ds_stack_push(dsid,i);
            instance_deactivate_object(i);
            i = collision_rectangle(x1,y1,x2,y2,obj,prec,notme);
        }
        repeat(ds_stack_size(dsid)) {
            instance_activate_object(ds_stack_pop(dsid));
        }
        ds_stack_destroy(dsid);
    }
    return dsid;
}

well the same problems happened for almost all other collision_*_list scripts..

Offline

#2 2007-11-05 17:16:49

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

Re: collision_rectangle_list - optimalization:

Ummm, it doesn't destroy the data structure because the script is specifically designed to create one for the user. What would be the point of creating a list of collisions if the user couldn't actually use it? Why would you want to return the id of a data structure that doesn't exist? And again, the same issue with the stack, if I pop everything off of it, the user doesn't actually get any data at the end.

Incidentally, all of these collision_*_list() scripts have been rewritten to eliminate all the deactivation/activation which turns out to be very slow. I haven't updated them online yet.


Abusing forum power since 1986.

Offline

#3 2007-11-05 18:55:09

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

Re: collision_rectangle_list - optimalization:

omg I don't know why I forgot that.. well it might be because I've worked too much lately with a few anoying data leaking scripts (gmwindows for example)...

Offline

#4 2007-11-07 14:54:20

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

Re: collision_rectangle_list - optimalization:

I know the feeling. smile  I have added a note to the script pages about the potential for a memory leak.

EDIT: The new scripts have been uploaded.
http://www.gmlscripts.com/script/collision_checking

Last edited by xot (2007-11-07 16:40:33)


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB