I found several threads here discussing health bars (or health meters, call it whatever you want) with no satysfying answers on how to do this, so here is my answer.
The aim is to create a bar that will reflect the players health, ammunition or whatever you want, and not have dozens of actors to do that. I'm going to introduce the way I used in my upcoming space shooting game.
1)
It may sound a bit strange, but we will use one text actor for this. First, we need a bitmap "font" that will make the health bar.
Like this one that I have created:
2)
Now we have to create the actor in GE. Let's name it "health_bar". Don't add any animations. Click on the Text button in Actor Properties. Now click the New Font button. We find the file with our "font" and set it's properties. The initial character will be 0 and the number of characters is 3. You can write some text into the actor (but remember to use only 0, 1 and 2 characters) to see if it works fine.
3)
Now we need the bar to reflect the player's health. Create "g_PlayerHealth" variable in 'Variables' and make it Global Integer. We will write som simple functions in 'Global Code' to easily change the g_PlayerHealth value and show it via the health bar at the same time.
- Code: Select all
void UpdateHealthBar()
{
int i;
strcpy(health_bar.text,"");
for (i=0; i<g_PlayerHealth; i++)
{
if (i<=5) strcat(health_bar.text, "0");
if ((i>5)&&(i<=15)) strcat(health_bar.text, "1");
if (i>15) strcat(health_bar.text, "2");
}
}
void ChangeHealth(int modifier)
{
g_PlayerHealth += modifier;
if (g_PlayerHealth > 25) g_PlayerHealth = 25;
if (g_PlayerHealth < 0) g_PlayerHealth = 0;
UpdateHealthBar();
}
void SetHealth(int value)
{
g_PlayerHealth = value;
if (g_PlayerHealth > 25) g_PlayerHealth = 25;
if (g_PlayerHealth < 0) g_PlayerHealth = 0;
UpdateHealthBar();
}
We will now have a closer look on this three functions;
SetHealth function sets the player health to the value passed as it's parameter (int value). If the value is higher than maximum allowed (25 in my case), players health is set to the maximum. If it is lower that allowed minimum, it is set to the minimum (0 in my case). Then the UpdateHealthBar function is called.
ChangeHealth function works the same way the SetHealth function works, but instead of setting the g_PlayerHealth variable to have the value of the passed parameter (int modifier), it just adds the modifier to the current g_PlayerHealth value. Be the modifier -1, it substracts 1 point of health, be it 2, it adds 1 to the health value, but will never cross behind the minimum and maximum allowed values. Then UpdateHealthBar function is called.
UpdateHealthBar is the most important part, as it makes the health bar show the value of our g_PlayerHealth variable. You can see, there is a simple FOR cycle running g_PlayerHealth-times (so if we have g_PlayerHealth = 3, the loop will run 3 times). Just before the cycle, we erase everything in the text property of our "health_bar" actor. Than in each cycle, we write new character to the string so in the end, so be the g_PlayerHealth variable set to 3, we would have 3 characters in 'health_bar.text' string. As you can see, I use 3 different characters, so the bar will be red on one end, yellow in the middle and green on the other end, so it looks cool.
4)
We now have everything set up. In the game, we can just call ChangeHealth(modifier) or SetHealth(value) function and the health bar will react. Now, when the g_PlayerHealth variable has the value of 25, the bar looks like this:
Last words
Although my english is poor I hope you have understood the basics of creating a health bar. If not, just post here and I will try to explain it better.
I don't say that this is the best way how to do that, it's just the best I came up with.