How do I Make a "Universal" Health Bar?

0 favourites
  • 8 posts
From the Asset Store
Health, Weapon & Rage Bars for your Games. Easy to use & Customize.
  • Sorry if this is kind of a long request but every health bar tutorial I look up only tells you how to make a single health bar with a fixed maximum health.

    What I have now is a health bar sprite that's width is set to the health value of the unit.

    What I'd like to do is:

    1. Have it so the width value is set as a percentage of the unit's health, so that no matter how much maximum health the unit has, the health bar will always have the same maximum size.

    2. Be able to attach this health bar to a variety of different enemies, so that I don't have to make a new health bar for each different enemy I make.

    Sorry if I could have explained it better, that's the best way I can think of describing it.

    Thanks anyone for any help

  • Question one is not clear..

    You want the max health to be the maximum width of the healthbar?

    question 2 would be creating a healthbar for each enemy, setting an instance variable to the uid of that enemy and having the healthbar only react to the health of the referenced enemy..

  • Hey thanks for the response. I'll try and rephrase question 1.

    The way my health bar works now is that it's width is set to a unit's Health value. (100 Health = 100 Width)

    Later in the game as the unit gets stronger, lets say it's maximum health goes up to 500. I'm trying to make it so that instead of the Health bar being 500 units of maximum width, it would have the same maximum size all the time, so a unit with 100 maximum health would have the same size health bar as a unit with 500 maximum health.

    I guess I'm trying to make health bars that would represent a percentage of a unit's health rather than it's absolute number value.

    I hope that was more clear. Thanks again for the response.

  • Ok, so let's say the healthbar would be maximum 150 pixels in width.

    if the maximum health = 500 and the current health is 250 the bar would be (250/500)*150 = 75 pixels, which is half, so correct..

    so the bar should be set to (CurrentHealth/MaximumHealth)*HealthbarMaximumwidth..

    If your enemies have different maximumhealth and you would like them all to show their health relative to one another, setting the MaximumHealth to the MaximumHealth of the enemy with the highest MaximumHealth should do the trick..

  • I posted something then deleted it because I figured out what was happening lol. It seems to work fine thanks for your help. I'll try now to do what you suggested for my second question.

    Thanks again

  • Hey so I'm having a bit of trouble with what you said for my second question. How would I use a UID to attach the health bar to the enemies?

    To clarify I'm trying to make it so that I can spawn different enemies with different properties, but still use the same health bar to attach to them.

    So I could spawn a "Skeleton" enemy and spawn and attach a health bar to it.

    Then I could spawn a "Zombie" enemy and spawn and attach the same health bar to it.

    I obviously can't have 2 separate conditions that say "Always Set Health Bar to Health of Skeleton" and "Always Set Health Bar to Health of Zombie". So I'm not sure how to set this up.

    I hope that makes sense.

    Thanks for the help

  • easiest way would be to work with families and make a family called "enemies" and give the healthbar an instance variable EnemyUID

    on enemies created -

    system create healthbar at enemy.x, enemy.y+(the number of pixels to put it above or below your enemy)

    healthbar - set EnemyUID = enemies.UID

    system every tick

    for each enemies

    pick healthbar where enemies.UID = healthbar.EnemyUID

    healthbar set width = (enemies.CurrentHealth/enemies.MaximumHealth)*HealthbarMaximumwidth

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Hmm. Unfortunately I'm still using the free version, so I don't have the family function available. I was planning on getting the full version eventually, I guess I might as well get it then.

    Thanks for the help

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)