The Game Maker engine executes every object event in order, and if more objects have the same event the overall execution order is due to the object's depth
.
The reason Red Car is never damaged is likely because Blue Car executes its collision code first, and it sets its own velocity to 0
in line 2:
len = 0 //Current speed
So, when it's up to Red Car executing the collision script, when computing the amount of damage:
if (len < obj_carBlue.len) { // My current speed is greater than Blue
myHP -= obj_carBlue.len/2
}
there will be always no damage for Red, because obj_carBlue.len
is already set to 0 before this code is executed.
Proposals
To solve this problem, you must set len
to 0 after computing amount of damage for both cars. A better option is create another object in charge of computing damage for each car and setting their speeds to 0 after updating their hp
values; most game engines rely upon a collision response separated from the detection, and acting stricly after the detection has happened.
A simpler fix
On the other hand, if you want to stick with the Collision Event alone you can change your code as follows:
obj_CarBlue
's Collision Event with obj_CarRed
:
if (len < other.len) { // My current speed is greater than Red
myHP -= other.len/2
} else {
other.myHP -= len/2
}
len = 0
other.len = 0
First, we get rid of place_meeting()
because we are inside the Collision Event and the collision is certaintly happening. Then, we use the special keyword other
to make a reference to the very Red Car we are colliding with instead of a generic one: if we reference obj_RedCar.something
we are trying to read the variable something
of any instance among all Red Cars. Inside the Collision Event the keyword other
tells us what is the particular instance the collision occured with.
The code above deals with the collision at once for both cars, you can copy/paste it for Blue and Red cars and it will still work because after the first execution both cars' velocities will be set to 0, so that the code is duplicated but its effects aren't.