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

collision problem

M

Meve

Guest
I'm having problems with my collisions, I want to subract 1 from the hp every time the monster hits the target until Nasu_obj destroys itself.

I've made a collision event with my monster_obj:
Code:
    --hpm;
in the creation code:
Code:
hpm = 3;
and in my step:
Code:
if hpm = 0
{
    
    instance_destroy();
    
}
But when I have a collision with my monster obj the program tells me
Unable to find any instance for object index '4' name 'Nasu_obj'
at gml_Object_monster_obj_1_Step_0 (line 64) - if ( x < Nasu_obj.x)

Code:
if ( x < Nasu_obj.x)

{
    image_xscale = 1;

}
else
if (x > Nasu_obj.x)

{
    image_xscale = -1

}
 

Bentley

Member
I think you're destroying the monster in the Collision Event. Then, when the Step Event runs, it's checking for that monster. Because it doesn't exists, you get an error. This might help:
Code:
if (instance_exists(obj_monster))
{
    if (x < obj_monster) // do something
}
I'd also be careful with your Collision Event. Collision events trigger every step they're colliding. So your monster is likely being destroyed instantly rather than just losing 1 hp.
 
Last edited:
R

rpsrosario

Guest
As a rule of thumb I tend to reference ''other' rather than a specific object index in the collision event. Also, as @Bentley stated, I'd double check the destruction of instances in your code so that you're sure they aren't destroyed before checking for them.
 
M

Meve

Guest
As a rule of thumb I tend to reference ''other' rather than a specific object index in the collision event. Also, as @Bentley stated, I'd double check the destruction of instances in your code so that you're sure they aren't destroyed before checking for them.
I think you're destroying the monster in the Collision Event. Then, when the Step Event runs, it's checking for that monster. Because it doesn't exists, you get an error. This might help:
Code:
if (instance_exists(obj_monster))
{
    if (x < obj_monster) // do something
}
I'd also be careful with your Collision Event. Collision events trigger every step. So your monster is likely being destroyed instantly rather than just losing 1 hp.
and how would you guys suggest I'd make sure that my instance doesn't destroy itself too fast? (sorry I'm a bit of a noob :confused:)
 
R

rpsrosario

Guest
and how would you guys suggest I'd make sure that my instance doesn't destroy itself too fast? (sorry I'm a bit of a noob :confused:)
I guess you could do a sort of a hit grace period (you saw that effect a lot in older games, not sure about recent ones). It is basically a timer that starts the first time you are hit by an enemy and grants you temporary resistance to damage until the timer reaches its end. This way only the first hit deals damage to the player and the player gets a little bit of time to get away from the enemy as well.
 
M

Meve

Guest
I guess you could do a sort of a hit grace period (you saw that effect a lot in older games, not sure about recent ones). It is basically a timer that starts the first time you are hit by an enemy and grants you temporary resistance to damage until the timer reaches its end. This way only the first hit deals damage to the player and the player gets a little bit of time to get away from the enemy as well.
I took your advice and I used this code in the collision event:
Code:
alarm[0] = room_speed * 20
--hpm;
it's still working the exact same, however. Do you maybe know what I did wrong? (sorry I'm a bit dense)
 

TheouAegis

Member
You have to check if alarm[0]<1 before applying the damage. And of course you also need an alarm event so that the alarm counts down.
 

Bentley

Member
I took your advice and I used this code in the collision event:
Code:
alarm[0] = room_speed * 20
--hpm;
it's still working the exact same, however. Do you maybe know what I did wrong? (sorry I'm a bit dense)
Code:
if (alarm[0] == -1) alarm[0] = something;
Alarms begin at -1, and when set, they countdown to -1. Because a collision event, like a step event, triggers every step there's a collision, that alarm is being set to "something" constantly. It will never countdown (while they're still colliding). A solution is to check:

if (alarm[0] == -1)

So think about it this way: GM goes through your collision event code. It checks the line: if (alarm[0] == -1). That condition is true (alarms default to -1). You then set alarm1 to "something". The next step GM goes through that collision event and checks: if (alarm[0] == -1), that condition is false, as you set it previously to "something".

Sorry if this is a bad explanation. Me != expert.
 
Last edited:
Top