1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Regenerating HP

Discussion in 'Programming' started by Sabotage, Jan 16, 2018.

  1. Sabotage

    Sabotage Member

    Joined:
    Jan 16, 2018
    Posts:
    7
    Hello,

    I'm an artist who is attempting to create a game, and this is my first time delving into the world of coding. I've been at this for about 2 weeks, and started off with some open-source code which was already doing some of the things I wanted to accomplish.
    All that to say, I know nothing of complicated coding practices but am growing more and more familiar with GML and simpler code.

    So where I'm at now is that I've got collision working (via a script and parent object.)
    I've got an HP bar. I've got health deterioration and I've got player death.

    What I want to do is create a system alike to Halo or Call of Duty (i think?), where they have a delayed regeneration after not receiving damage for a time.
    At the moment I'm struggling to understand alarm events and wonder if there is an easier way to do this or if someone could explain / post the code to make this work.

    Currently I have something which is completely wrong but might show you where my mind is at;

    takedamage = currentHP-- (this variable is in the create event)

    //wait to check if HP is still going down
    if takedamage == false
    {
    alarm[0] = 3*room_speed;
    HPregen = true
    }

    //initialize regen
    if currentHP < maxHP
    {
    currentHP += HPregen
    }

    What I want to happen is this; While the player is receiving damage (health is actively deteriorating) there is no HPregen. When the player is no longer receiving damage, wait 3 seconds, activate HPregen (until max health.)
    This would need to be interruptable via receiving damage again.
     
  2. ForesterPL

    ForesterPL Member

    Joined:
    Sep 23, 2017
    Posts:
    61
    Try this:
    Create another variable- alarmStarted.

    In step event:
    Code:
    if (takedamage == false && alarmStarted == false)
    {
        alarm[0] = 3*room_speed;
        alarmStarted = true;
    }
    
    if (takedamage == true)
    {
        alarmStarted = false;
        HPregen = false;
    }
    
    if (currentHP < maxHP && HPregen == true)
    {
        currentHP += 1;
    }
    
    In alarm 0 event:
    Code:
    HPregen = true;
    
     
  3. Sabotage

    Sabotage Member

    Joined:
    Jan 16, 2018
    Posts:
    7
    It's not working yet, but that may be something with the variables. What am I setting alarmStarted = to?
     
  4. sitebender

    sitebender Member

    Joined:
    Sep 13, 2016
    Posts:
    840
    I like to use...
    Code:
    if(hp <= 0)
    {
     /// you're dead
    }else
    {
    hpmax = 100
    hp += 5 / room_speed // 5 HP per second
    }
    hp = clamp(hp, 0, maxhp)
    Seems easy enough.
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,133
    Set an alarm in your Create event to however long between ticks hp should recover. In your alarm event add to the hp if it is less than max hp and then set rhe alarm again to the delay between ticks. Whenever the player takes damage, set the alarm to how long it takes until the player regenerates again.

    That's all you need to do.
     
  6. JFitch

    JFitch Member

    Joined:
    Sep 28, 2016
    Posts:
    428
    In the Create event, set healing to true or false. Since you'll probably start at full health, it shouldn't make a difference which one.

    Before the rest of your code, set a variable for previousHP to currentHP. This is best in your Begin Step event.
    Code:
    previousHP = currentHP;
    In your End Step event, check whether the HP dropped that step. If it did, stop the healing and set an alarm to start it again after a few seconds.
    Code:
    if (currentHP < previousHP) {
        healing = false;
        alarm[0] = 3 * room_speed;
        }
    In the Alarm event, set healing back to true.
    Code:
    healing = true;
    Anywhere in your Step event, put your code for healing.
    Code:
    if (healing) {
        currentHP = min(100, currentHP + 5 / room_speed);
        }
     
  7. Sabotage

    Sabotage Member

    Joined:
    Jan 16, 2018
    Posts:
    7
    My friend is much better at coding than I am and I think came up with the same solution.

    ///HP CODE - REGEN / DMG
    //wait to check if HP is still going down
    if (i**** == false && alarm[0] <= 0)
    {
    alarm[0] = 2*room_speed;
    canRegen = true
    }
    //Has Been hit
    if (i**** == true)
    {
    //Tick HP down
    currentHP--
    //Reset damage tick
    i**** = false
    //Reset Heal
    canRegen = false;
    }

    if (currentHP < maxHP && canRegen == true)
    {
    currentHP += HPregen;
    }

    Basically I needed more variables and now I've learned what boolean is capable of.
     
  8. Thunder Lion

    Thunder Lion Guest

    My idea is this:

    Create Event for player
    hp
    hp_full
    hp_regen=N
    hp_regen_on=false

    In step event
    If hp_regen_on=true {hp+=hp_regen}
    If hp=hp_full {hp_regen_on=false}
    If hp<hp_full {alarm[0]=10} //10 is the delay of the regen start

    In alarm 0 event
    hp_regeb_on=on

    Collision with damage
    hp-=damage
    hp_regen_on=false

    This will work but it will attempt to regenerate at all times after alarm0 unless a collision turns it off again
     
  9. Sabotage

    Sabotage Member

    Joined:
    Jan 16, 2018
    Posts:
    7
    I don't have a problem with that in theory but is it a waste of resources? Obviously a 2d game takes little resource regardless, but I would still try to be as efficient as possible.
     
  10. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    996
    All of these suggestions are convoluted except for TheouAegis's suggestion; go with that one.
     
  11. Sabotage

    Sabotage Member

    Joined:
    Jan 16, 2018
    Posts:
    7
    That's the one I ended up using! Works fantastic. Now I'm trying to implement a light engine but I'll either figure that out or make another thread...
     

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