GML Player Health Not Working

L

lauralie

Guest
Hi! I'm adding player health hearts in my game. When the players health goes down the hearts go down. Right now I have it so that the number of hearts correctly correspond to the number of the players health. If I manually change the players health variable it correctly displays in the game. Now I'm trying to make it so that when they player is touched by an enemy they lose a heart/1 health.

But when they get touched they loose all their health and die immediately.

This is my code for when the player touches an enemy. The variable for the global health is set to 3. Not sure what's making them die still upon being touched?

GML:
if place_meeting(x,y,oEnemy)
{
    global.playerHealth--;
      
    if (global.playerHealth <= 0)
    {
        with (oGun) instance_destroy();
        instance_change(oPDead,true);

        direction = point_direction(other.x,other.y,x,y);
        hsp = lengthdir_x(6,direction);
        vsp = lengthdir_y(4,direction)-2;
        if (sign(hsp) != 0) image_xscale = sign(hsp);
    }
}
 

Nidoking

Member
It sounds like you want some invulnerability period during which the player won't take further damage. Otherwise, it's subtracting one health every step, and there are probably 30 or 60 steps per second.
 
@Nidoking is right.
GML:
if place_meeting(x,y,oEnemy)
{
    global.playerHealth--;
...
This means that as long as your character is in contact with the ennemy, drop the health by 1. You may want to add a timer and a Invincible variable for x number of seconds after the hit so that the player does not continuously go through your health drain. Something like this:
GML:
if place_meeting(x,y,oEnemy)
{
    if (!bInvincible) {
        global.playerHealth--;
        bInvicible = true;
        // Set your timer interval code here which will set bInvincible back to false
    }
...
 
L

lauralie

Guest
@Nidoking is right.
GML:
if place_meeting(x,y,oEnemy)
{
    global.playerHealth--;
...
This means that as long as your character is in contact with the ennemy, drop the health by 1. You may want to add a timer and a Invincible variable for x number of seconds after the hit so that the player does not continuously go through your health drain. Something like this:
GML:
if place_meeting(x,y,oEnemy)
{
    if (!bInvincible) {
        global.playerHealth--;
        bInvicible = true;
        // Set your timer interval code here which will set bInvincible back to false
    }
...
Oh I see, thank you! How should I do the timer? Should I set that up with an alarm?
 
Oh I see, thank you! How should I do the timer? Should I set that up with an alarm?
Alarms are indeed, the easiest way to do that. That's how I did it in my Tile Based Platform Engine in GMS 1.4 back then. It would also be good to put a transparence on your sprite or at least a little indicator for the player that he/she is incincible for a short period of time. But yes, alarms. If I remember correctly, it's alarm[AlarmID] = NumberOfMiliseconds and in the alarm, you put your bInvinicible = false. It's been a while I have not been in GMS.

Happy coding :)
 
Top