sfml health bar

Started by
5 comments, last by Elit3d 8 years, 12 months ago

My graphical health bar goes from the whole bar to nothing: Heres a gif to properly show it http://gyazo.com/7e55d938a8e2cae0bd87a7b46d688251

Heres my code:


healthBarSprite.setScale(m_playerHealth / maxHealth, 1);

Is there a better way to do this?

Advertisement

What you're hitting is that you (very likely) hit the classic first time problem of integer division.

The SFML setScale() method accepts a floating point value - but if your m_playerHealth and maxHealth values are of integral type this will basically only return 0 if m_playerHealth < maxHealth and 1 otherwise.

To fix this you can simply cast one of the values (or both) before doing the division to a floating point value.


healthBarSprite.setScale(static_cast<float>(m_playerHealth) / maxHealth, 1);

This should fix it - beware that of course the floating point values may not be 100% exactly the same as your integer values but for your UI it should totally be fine ;)

What you're hitting is that you (very likely) hit the classic first time problem of integer division.

The SFML setScale() method accepts a floating point value - but if your m_playerHealth and maxHealth values are of integral type this will basically only return 0 if m_playerHealth < maxHealth and 1 otherwise.

To fix this you can simply cast one of the values (or both) before doing the division to a floating point value.


healthBarSprite.setScale(static_cast<float>(m_playerHealth) / maxHealth, 1);

This should fix it - beware that of course the floating point values may not be 100% exactly the same as your integer values but for your UI it should totally be fine ;)

both maxHealth and m_playerHealth is an int

What you're hitting is that you (very likely) hit the classic first time problem of integer division.

The SFML setScale() method accepts a floating point value - but if your m_playerHealth and maxHealth values are of integral type this will basically only return 0 if m_playerHealth < maxHealth and 1 otherwise.

To fix this you can simply cast one of the values (or both) before doing the division to a floating point value.


healthBarSprite.setScale(static_cast<float>(m_playerHealth) / maxHealth, 1);

This should fix it - beware that of course the floating point values may not be 100% exactly the same as your integer values but for your UI it should totally be fine ;)

oh that actually worked though, mind explaining the static_cast<float> ?

static cast does just as it sounds it casts a value to a type. I.e. int number = static_cast<int>(3.14); will be 3 cause you are casting it to an int type. You can also alternatively do int number = (int)(3.14);

Here you can read an explanation of the differences http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast

both maxHealth and m_playerHealth is an int

...

oh that actually worked though, mind explaining the static_cast<float> ?

It doesn't seem like you understood marcjulian, so I'll try to rephrase it in a different manner:

The function "setScale" takes floats. If you try to do this: int health = 50 / 100;

You're not going to get 0.5, because an integer can only be whole numbers. So that is: 0, 1, 2, 3, 4, 5, 6...

Also, you could make a "healthRatio" function, like this:


float healthRatio() {
    return (float) m_playerHealth / (float) maxHealth;
}
healthBarSprite.setScale(healthRatio(), 1.0f);

I recommend reading marcjulian's post a few more times until you get it.

both maxHealth and m_playerHealth is an int

...

oh that actually worked though, mind explaining the static_cast<float> ?

It doesn't seem like you understood marcjulian, so I'll try to rephrase it in a different manner:

The function "setScale" takes floats. If you try to do this: int health = 50 / 100;

You're not going to get 0.5, because an integer can only be whole numbers. So that is: 0, 1, 2, 3, 4, 5, 6...

Also, you could make a "healthRatio" function, like this:


float healthRatio() {
    return (float) m_playerHealth / (float) maxHealth;
}
healthBarSprite.setScale(healthRatio(), 1.0f);

I recommend reading marcjulian's post a few more times until you get it.

ah i see, thanks :)

This topic is closed to new replies.

Advertisement