[SOLVED] Referencing Specific ID of Object in Fast Collision

D

DKR_87

Guest
I have 3 enemies in the room. When I kill one of them, they ALL die. In my bullet End Step event, I check a collision line for fast bullet collision. My goal, I presume, is to decrease the "hp" variable of the specific ID of the enemy object that the bullet is colliding with. I've done some searching and tried some ideas I came up with. No luck so far. Any help?

Bullet END STEP
Code:
if collision_line(xprevious,yprevious,x,y,obj_enemy,1,0)     
    {     
    instance_destroy();

    // Decrease Enemy Health
    obj_enemy.hp -= obj_player.damage;
    }
Obviously, this code decreases ALL instances of obj_enemy by the player's damage. I've learned quite a bit in the last few years using GM Studio. Admittedly, working with object ID's has been the last confusing thing to me.

Thanks for any help,
Dustin
 

JackTurbo

Member
Instead of referring to the enemies by object name you should use the keyword "other" to refer to the instance in collision


Code:
if collision_line(xprevious,yprevious,x,y,obj_enemy,1,0)   
    { 
    instance_destroy();

    // Decrease Enemy Health
    other.hp -= obj_player.damage;
    }
As for objects and instances, I find it easiest to think of objects like a template.

Imagine you're making cookies. You have a cookie cutter which is like a template for your cookies and defines their shape, but each cookie can also be a little bit different because you can choose to put different types of icing/frosting on them.

The cookie cutter is like an object while the cookies themselves are instances and the icing is the attributes that you can change to make them different (variables, sprites, x/y values etc).

It might be a weird metaphor but I hope that makes sense?
 
Last edited:
D

DKR_87

Guest
That was the first thing I tried. I have a weapon system that changes the player's damage based on the player's current weapon. It is confirmed to be working properly when I switch weapons. And it worked just fine while I was having trouble with ALL enemies dying when their health was decreased appropriately by the weapon's damage.

Now that I use "other.hp", it's health simply does not decrease when the bullet hits it. I am drawing text on the screen, showing the enemy's current "hp" (removed 2 enemies in room and only 1 now for testing), and it properly decreases when I simply use "obj_enemy.hp". But when I use "other.hp", it simply does not decrease that "hp" variable when the bullet collides with it.

In obj_enemy's CREATE EVENT I literally just have:
hp = 15;

Even if I change the END STEP collision event to simply:

other.hp -= 1;

It STILL doesn't decrease the obj_enemy "hp" variable.
 

JackTurbo

Member
Weird, I'd have thought that wouldve worked.

Is there a reason you're using collision line over the other collision functions?

Anyway, collision_line returns the id of the instance in collision right? So you could try something like this:
Code:
var collisionCheck = collision_line(xprevious,yprevious,x,y,obj_enemy,1,0) ;
if (collisionCheck != noone)
   {
    instance_destroy();
   // Decrease Enemy Health
   collisionCheck.hp -= obj_player.damage;
   }
 
D

DKR_87

Guest
!!!!!!!!! Holy cow. That works. I'm confused though. What's so different about it? I would have thought just using "other.hp" would have worked. Well, my friend. You worked magic for me. Little prototype GIF below. (placeholder Doom sprites :) )

 

GMWolf

aka fel666
other only works in a collision event.

when doing the check "manually" using collision functions, you have to get the ID returned by the collision function.
 
Last edited:
Top