• 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!

GML Hit detection not working.

Coded Games

Member
So after taking a few month break from using GameMaker to using Java instead I'm having trouble figuring why objDamagable's hp is not being decreased. objDamagable DOES have hp defined in it's create event but for some reason, it's not going down.

Code:
//Trance Max Distance
while (distance < maxRange && !hit) {

    //Detect hit.
    var xx = x + lengthdir_x(distance, angle);
    var yy = y + lengthdir_y(distance, angle);
  
    if (collision_point(xx, yy, objDamagable, false, true)) {
        var target = instance_place(xx, yy, objDamagable); 
        target.hp -= damage;
        hit = true;
        break;
    }
  
    distance++;
}
It's giving me an error saying both target and target.hp are unknown variables.
 

NightFrost

Member
FYI, collision_point already returns an instance id so using instance_place is redundant. You could:
Code:
var target = collision_point(xx, yy, objDamagable, false, true);
if(target != noone){
    // hit logic here
}
 
J

JealousOfCrows

Guest
You want to check to make sure an instance of objDamageable exists before running that code. If this code is running first without the instance created then thelose variables won't exist. Also post the error.
 

Coded Games

Member
Already fixed. I just didn’t realize that collision_point returns an instance Id instead of a Boolean. Don’t need to even check if an instance exists.
 
Top