Percentages of a variable

I’m making a RPG game for a friend. The health bar needs to change based on the percentage of health. I have two global variables, HP_player and HP_player max. The max changes with each level, however there’s around 400 characters who can be played as, all with different HP per level, so it seems inefficient to make the HP change over 400 times, different for each level, which can go up to 100 (That’s over 40,000 times). So… Say the HP_Player_Max is 40. If the character takes 10% damage (-4), the health bar should drop a pixel. How do I code it to drop a pixel for every 10% of the max health?

why not just set the width of the health bar to 100/maxHealth*currentHealth?

By this, the health bar will only be 10 pixels in size. The formula you’ll want is (currentHealth/maxHealth)*10


Figured I would under explain something and overexplain something else lol. It’s a Pokémon health bar, so I meant that the bar would change to a frame based on that percentage. (Dropping a pixel meant that the health bar looked like it was going down a pixel). I have

so far, but It doesn’t seem to want to work. The frame of the health bar isn’t changing. I thought maybe it wasn’t working since 10/44 (my test numbers based off the character I had been playing) wasn’t a whole number, so I tried trunc(). I also tried 10 and 20 as the multiplier, no frame change there either.

image This is the health bar in question :slight_smile:

i have a few questions here:

  1. So you have 10 frames for the health bar? If so, the formula I gave earlier is what you want, but truncated or rounded (try each one and see which one suits your needs)
  2. Why are you using globals, and not instance variables? Or will there only ever be 1 character with a health bar per the scene?
  3. Where are you updating the frames to the health bar?
  1. I have 50 frames for the health bar.

  2. I’m using globals, since I need a lot of the variables to carry over to other scenes, and the variables can’t be attached to one object. A lot of text in other scenes is updated based on the values, and I use different objects for these different scenes, so the object variables wouldn’t carry over, I’d assume. (I haven’t used this for long, so I don’t know a great deal about it.)

  3. I’m not sure what you’re asking

Why do you have 50 frames?

You do not need to have static animations for the health bar. Just make the “fill” part of the bar a separate object (9-patch/panel sprite) and shrink/stretch as needed. Take a look at how I did it in the Not a vania example that’s in the engine.

Globals should work fine for your HP, not for enemy HP. No reason to track enemy HP for globals if you are.

Why, if a 10% drop is 1 pixel movement? That only needs 10 pixels (to reach 100%)

You may want to look at using external layouts. You have one scene, one script/set of events, and add objects from the external layout. It sounds like it’d be more suitable for you than multiple scenes.

You wrote previously

You set the health bar frame at the start of the scene. But where are you updating it, as health increases or decreases?

The bar has 50 pixels, so 50 frames for every downgrade, but your way seems smarter lol. I’ll also move enemy HP off globals. I’ll look into external layouts. Any reason why it would have benefit for me? Also, I wasn’t trying to update it in that moment, I just wanted to get it to work at all. :slight_smile: I was going to have it update every time an NPC attack is done, so that in goes in order with the animations of the sprites.

In my mind, External Layouts will help you out for a few reasons:

  1. You only have to set up your UI once. You can import the UI layout every scene you need it
  2. Once you have your UI set as you want it, it won’t clutter up your scene editor while you’re working on the rest of the game.

I’ll try an external layout for the next UI I need to set up. Thanks :slight_smile:

Using external layouts will probably require a slight refactor of how you currently are doing the scenes. Instead of starting up a new scene, you’ll need to remove the objects that aren’t needed in the current scene, and replace them with the new scene’s objects.

To add to what @Silver-Streak wrote, the benefits are:

  1. You only need one set of events/code behind. No need to copy or link.
  2. Scene variables aren’t lost.
  3. You can merge multiple scenes into one.

Changed it into a panel sprite, used "Change width of Health_Bar to (currentHealth/maxHealth)*180" and it wont show up in the previews. I tried 10, 20, 50, and 100 for the last number as well. Any ideas? I can see it when looking at it in the editor.

Run it via the debugger and verify the values of currentHealth and maxHealth. And to confirm, you are using “Variable(currentHealth)/Variable(maxHealth)” (or GlobalVariable if they’re still globals)?

(currentHealth/maxHealth) doesn’t mean anything to the engine. :smiley:

You need to actually use the variable expressions. If they’re global variables, it’s GlobalVariable(yourvariablehere), if they’re scene variables, it’s Variable(yourvariablehere), if they’re object variables, it’s Object.Variable(yourvariablehere).

1 Like

This is exactly what it looks like so there’s no confusion. Debugger says the width of the sprite is 0.

HP__current has a double underscore. I assume this is incorrect, and will account or the 0 width if so.

If not, what are the values of HP__current and HP_max_current in the debugger?

Fixed the underscore, says HP_max_current is 44, HP_current is 10. Any chance the fact that its type is a string on HP_current a problem?

Changed it to a number, didn’t change anything :((