Instance Collision

kapaqw

Member
Is there anyway to find the specific instance that collided with a object and put it in a variable? Would you use instance_find?
 

FrostyCat

Member
See: What's the Difference: Objects and Instances
Collision events (the ones from the event selector, NOT collision checks in the Step event): other refers to the colliding instance in this event only. IMPORTANT NOTE: other is always equal to -2 and will lose context outside the event. If you need the actual instance ID later, you need to use other.id instead.
GML:
other.hp -= 5;
GML:
last_collided_enemy = other.id;
Collision-checking functions: instance_place() and instance_position() are the instance-ID-oriented analogues of place_meeting() and position_meeting(). Functions that start with collision_ but don't end in _list all return instance IDs. Save that instance ID into a variable, then use that as the subject to work with. Note that these functions return noone upon not finding a collision. Always account for this special case whenever you handle collisions this way.
GML:
var inst_enemy = instance_place(x, y, obj_enemy);
if (inst_enemy != noone) {
    inst_enemy.hp -= 10;
}
 

Yal

šŸ§ *penguin noises*
GMC Elder
In short: use other.id within a collision event.
To elaborate on this: you can use other for quick access during a collision event, but don't save other to a variable for later use (other is basically a volatile reference that is meaningless outside of a collision event or with-loop). other.id gives you the actual id of the involved instance.
 
Top