Child object not registering hit

D

Disgolaxe

Guest
The projectile I have named "contact" has this in it.

if instance_place(x,y,Actor) and Actor.hit {
Actor.HP -= 1;
instance_destroy()
}
else
if instance_place(x,y,Actor) and Actor.blockhit {
Actor.HP -= 1 - Actor.block;
instance_destroy()
}

"Actor" is the parent object.
"Enemy" is the child object.

I must be missing something crucial about how the parent-child function works in GML, because when I shoot at Enemy it doesn't register the collision with the projectile, whereas if I shoot the parent object it works as intended. Any advice as to why this is?
 
J

JFitch

Guest
object.variable does not work with parents, and it's only recommended if you have exactly one instance of that object.
 

FrostyCat

Redemption Seeker
What you're actually missing is the difference between an object and an instance.

NEVER access a single instance by object ID if multiple instances of the object exist. This includes attempts to reference or set object.variable (which is inconsistent across exports) and using with (object) to apply actions to it (this encompasses all instances of the object instead of just the one you want). Verbally, "Dog's colour" makes sense with one dog, but not with multiple dogs.
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_ all return instance IDs. Note that these functions return noone upon not finding a collision, so be sure to account for this value.
Combining both points:
Code:
var colliding_actor = instance_place(x, y, Actor);
if (colliding_actor != noone) {
  if (colliding_actor.hit) {
    colliding_actor.HP -= 1;
    instance_destroy();
  } else if (colliding_actor.blockhit) {
    colliding_actor.HP -= 1 - colliding_actor.block;
    instance_destroy();
  }
}
 
Top