Game Maker: selecting multiple instances
I would like to show a simple method to work around a limitation of the Game Maker collision functions. This method works with all the collision functions that select an instance of an object, such as instance_place, instance_position, collision_line, etc..
These functions return the id of the object being collided if they succeed, otherwise return a negative value. But what about multiple collisions? The functions return only the id of the first object found.
So here is a trick to select multiple instances. The solution is very simple: just move the first instance found outside the room: at the next iteration, the collision function will return the next instance in the same position. At the end, just move the instances at their original positions.
1 2 3 4 5 6 7 8 9 10 11 |
while (true) { i=instance_place(x,y,Obj); if (i==noone) break; // do things i.xi=i.x; i.x+=10000; } with (Obj) { if (x>9999) x=xi; } |
Moreover, here is a function that returns an array containing the objects found, or -1 if no object was found. You can call it “instance_place_multiple”; the arguments are the same as instance_place.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
var ar,i; ar=-1; i=0; while(true) { var nnn=instance_place(argument0,argument1,argument2); if (nnn==noone) break; ar[i]=nnn; i+=1; nnn.xi=nnn.x; nnn.x+=10000; } with (argument2) { if (x>9999) x=xi; } return ar; |
Eloise
on12 January 2023 at 11:36 says:
Very interesting subject, thank you for posting.