SOLVED How to add the count of multiple IDs to a variable?

SGNQuen

Member
I'll just give an example similar to the example of collision_rectangle from the manual:

GML:
var inst;

inst = collision_rectangle(50, 50, 200, 100, obj_Ball, false, false;)

if (inst != noone)
   {

   (My question here)

   }
So if there are multiple instances of obj_Ball that have entered that collision_rectangle, how do I get the amount that are currently in collision, then add it to a variable named instAmount?
 

chamaeleon

Member
I'll just give an example similar to the example of collision_rectangle from the manual:

GML:
var inst;

inst = collision_rectangle(50, 50, 200, 100, obj_Ball, false, false;)

if (inst != noone)
   {

   (My question here)

   }
So if there are multiple instances of obj_Ball that have entered that collision_rectangle, how do I get the amount that are currently in collision, then add it to a variable named instAmount?
I'm going to go out on a limb as say that you don't do this using collision_rectangle() since it will only return a single instance (which one is unspecified). But you're in luck. You have collision_rectangle_list() at your disposal.
GML:
var instances = ds_list_create();
var count = collision_rectangle_list(50, 50, 200, 100, obj_Ball, false, false, instances, false);
instAmount += count;
for (var i = 0; i < count; i++) {
    // do something with instances[| i] if desired
}
ds_list_destroy(instances);
 

Amon

Member
I'm going to go out on a limb as say that you don't do this using collision_rectangle() since it will only return a single instance (which one is unspecified). But you're in luck. You have collision_rectangle_list() at your disposal.
GML:
var instances = ds_list_create();
var count = collision_rectangle_list(50, 50, 200, 100, obj_Ball, false, false, instances, false);
instAmount += count;
for (var i = 0; i < count; i++) {
    // do something with instances[| i] if desired
}
ds_list_destroy(instances);
Is it generally considered good practice to immediately destroy the list after that process? Is there an overhead to the constant creation/destruction of a list? Why not just pool (pool manager) it or repopulate?

Forgive me if this is a noob thing I've said. I came from a language called Monkey where creating and destroying lists of objects was more costly than just leaving the list there.
 

chamaeleon

Member
Is it generally considered good practice to immediately destroy the list after that process? Is there an overhead to the constant creation/destruction of a list? Why not just pool (pool manager) it or repopulate?

Forgive me if this is a noob thing I've said. I came from a language called Monkey where creating and destroying lists of objects was more costly than just leaving the list there.
Oh, sure, you can keep it around as an instance variable if you want. I tend to do creation and destruction myself, and especially for sample code that I try to keep as self-contained as possible. There's a cost. However, is it relevant compared with the computation of collision_rectangle_list() itself (or all the other collision related functions using such a list)? Depends on the game, I would think, but I'd hazard a guess the answer is no.
 
Top