• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

getting id's of multiple instances in a collision

D

Dengar

Guest
I have several instances of the same object and I need to check for collisions within an area and get the id's of all the instances that were collided with.
I believe I need to cycle through all the instances of that object and have it check itself, but im not sure how this would work. I have a crude idea of counting the instances and then doing a loop that many times and somehow referencing there ids each time it loops to check them? I don't kno.
the number of instances of the object will vary.

im very lost on how to code it, but is this even the right way?
 

jo-thijs

Member
GameMaker has a with structure for executing code by other instances (and you can use other to refer to the variables the instance calling the with structure).
Code:
var result, n = 0;
with object_name
    if collision_code(..., other.id /*as object name*/, ...)
        result[n++] = id;
Alternatively, you could also deactivate instances and reactivate them afterwards with instance_(de)activate_object.
Deactivated instances are ignored by collision functions.

Not sure which method would be fastest.
 
D

Dengar

Guest
Code:
var result, n = 0;
with object_name
if collision_code(..., other.id /*as object name*/, ...)
result[n++] = id;
sry for sounding like a complete noob, but I don't get this part: if collision_code(..., other.id /*as object name*/, ...)
also does the with (object_name) loop through all instances of the same object? and result[n++] would be the list of id's. but using a '++' inside an array ? haven't seen that before, hows the work? does it increase before checking or after?
 
B

bojack29

Guest
You are correct. You'll have to cycle thru the collided objects to find them all. You could set up a list of all the collided objects.
Code:
l = ds_list_create();
with(obj_collides){
     if (place_meeting(x, y, other.id)){
          ds_list_add(other.l, id);
     }
}
Now your list "l" has the id's of everyone whom collided.
 

TheouAegis

Member
sry for sounding like a complete noob, but I don't get this part: if collision_code(..., other.id /*as object name*/, ...)
also does the with (object_name) loop through all instances of the same object? and result[n++] would be the list of id's. but using a '++' inside an array ? haven't seen that before, hows the work? does it increase before checking or after?
collision_code just means you take whatever collision function you want (e.g., place_meeting(), collision_rectangle(), position_meeting(), instance_position(), etc., etc.) and use other.id for the obj argument in that function.

The code array[n++] will set array[n] to whatever value you say and then increment n. It's the same as array[n]=whatever; n++; n++ will increment the variable after it has been read. Conversely, ++n will increment the variable before it has been read. So array[n++] will set array[n] to whatever value, but array[++n] will set array[n+1] to whatever value.
 
Top