GMS 2 Issue On EnemyHit and HP

Discussion in 'Programming' started by Aegean, Aug 22, 2019.

  1. Aegean

    Aegean Member

    Joined:
    Aug 22, 2019
    Posts:
    3
    Hi guys, I am having a problem in my rpg game. I found some opensource code and I didn't understand the "hp-" expression. Because of that I couldn't code my enemies hp and my game crashes when I hit them.


    Code:
    //EnemyHit//
    var _damage = argument0;
    
    hp- = _damage
    flash = true;
    if(hp > 0)
    {
    state = ENEMYSTATE.HIT;
    hitNow = true ;
    }
    else {
    
    state = ENEMYSTATE.DEAD;
    })

    Code:
    // obj_enemy Create//
    state = ENEMYSTATE.FREE;
     enum ENEMYSTATE{
    
       FREE,
       HIT,
       DEAD
    }
     
  2. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    760
    it's actually ususally written hp -= _damage;
    it's a fancy way of writing
    hp = hp - _damage;

    they do the same thing but the first way is usally cleaner
     
    Last edited: Aug 22, 2019
    Aegean likes this.
  3. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,461
    The subtraction symbol goes with the = like this. When they go together, they cannot have a space in between.
    Code:
    hp -= _damage;
    
    This decreases the value of the variable hp by damage.
     
    Aegean likes this.
  4. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    760
    really they can't have a space? seems odd never knew that huh
     
    Aegean likes this.
  5. Aegean

    Aegean Member

    Joined:
    Aug 22, 2019
    Posts:
    3
    Thank you all for the answers I really appreciate it.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice