Generic Bar Hud Control
by Robert Geiman · 12/18/2005 (6:43 pm) · 47 comments
Download Code File
Description
The generic bar hud (GuiGenericBarHud) is similar to that of the GuiHealthBarHud except that it's not tied specifically to health/energy and gives the programmer much more control and flexibility, while offering some new features as well. This control can be used as a complete replacement for the GuiHealthBarHud, or to graphically display your own game specific values in bar form, such as armor, spell points, stamina, etc.
Since GuiGenericBarHud is not hard coded to display particular game value (such as health or energy) you must manually attach the GuiGenericBarHud to your game values. Luckily this is as simple as adding just a few lines of script!
Installation Instructions
1) Place the guiGenericBarHud.cc file in the proper folder as described below, include it in your project, and recompile Torque.
File placement for TGE 1.3
Place the guiGenericBarHud.cc file located in the TGE 1.3 directory of the zip file into the engine\games\fps folder.
File placement for TGE 1.4
Place the guiGenericBarHud.cc file located in the TGE 1.4 directory of the zip file into the engine\games\fps folder.
2) Add a GuiGenericBarHud control to your game GUI screen.
3) Simply add a few lines of script to attach your game values to GuiGenericBarHud!
Features of GuiGenericBarHud
* Ability to set/get the bar value
* Ability to set/get the min/max values of the bar (defaults to 0.0 - 1.0)
* Ability to, when changing the bar current value, have the hud slowly drain/fill to the specified value, rather than instantly changing the bar
* Ability to modify drain/fill speed settings
* Ability to display target value in a faded color, when slowly filling to a specified value (think Diablo health bar)
* Abillity to pulse the bar programatically, not just when reaching a specified threshold
* All the functionality of GuiHealthBarHud, such as vertical/horizontal bars, pulsing, background fill, flipping, etc
Supported TorqueScript GuiGenericBarHud Functions
void setValue(value);
void setMinValue(value);
void setMaxValue(value);
F32 getValue();
F32 getMinValue();
F32 getMaxValue();
void setFillSpeed(multiplier);
F32 getFillSpeed();
void setDrainSpeed(multiplier);
F32 getDrainSpeed();
void fill(delta value);
void fillTo(value);
void drain(delta value);
void drainTo(value);
void setPulseRate(rate);
S32 getPulseRate();
void setPulseThreshold(threshold);
F32 getPulseThreshold();
void setPulseAlways(true/false);
bool getPulseAlways();
void setShowBackgroundFill(true/false);
bool getShowBackgroundFill();
void setShowTargetFill(true/false);
bool getShowTargetFill();
Sample Scripts
Because GuiGenericBarHud is generic, some scripting must be done to hook up your game to the GuiGenericBarHud control. This can usually be done in just a few lines of script. Below are some sample scripts to show you how this is done:
This snippet shows how to use GuiGenericBarHud as a replacement for GuiHealthBarHud.
This snippet shows how to use the GuiGenericBarHud as a countdown timer, useful for displaying how long a player can stay underwater before drowning.
This snippet shows how to use the GuiGenericBarHud to display the player health when unmounted, and automatically switch to display vehicle health when the player is mounted on a vehicle.
Credits
Many thanks to Frank Carney who originally came up with the base code that is GuiGenericBarHud.
Updates
2/25/2006 - Added support so pulsing the GuiGenericBarHud will use the alpha value specified in the fill color.
Enjoy!
Description
The generic bar hud (GuiGenericBarHud) is similar to that of the GuiHealthBarHud except that it's not tied specifically to health/energy and gives the programmer much more control and flexibility, while offering some new features as well. This control can be used as a complete replacement for the GuiHealthBarHud, or to graphically display your own game specific values in bar form, such as armor, spell points, stamina, etc.
Since GuiGenericBarHud is not hard coded to display particular game value (such as health or energy) you must manually attach the GuiGenericBarHud to your game values. Luckily this is as simple as adding just a few lines of script!
Installation Instructions
1) Place the guiGenericBarHud.cc file in the proper folder as described below, include it in your project, and recompile Torque.
File placement for TGE 1.3
Place the guiGenericBarHud.cc file located in the TGE 1.3 directory of the zip file into the engine\games\fps folder.
File placement for TGE 1.4
Place the guiGenericBarHud.cc file located in the TGE 1.4 directory of the zip file into the engine\games\fps folder.
2) Add a GuiGenericBarHud control to your game GUI screen.
3) Simply add a few lines of script to attach your game values to GuiGenericBarHud!
Features of GuiGenericBarHud
* Ability to set/get the bar value
* Ability to set/get the min/max values of the bar (defaults to 0.0 - 1.0)
* Ability to, when changing the bar current value, have the hud slowly drain/fill to the specified value, rather than instantly changing the bar
* Ability to modify drain/fill speed settings
* Ability to display target value in a faded color, when slowly filling to a specified value (think Diablo health bar)
* Abillity to pulse the bar programatically, not just when reaching a specified threshold
* All the functionality of GuiHealthBarHud, such as vertical/horizontal bars, pulsing, background fill, flipping, etc
Supported TorqueScript GuiGenericBarHud Functions
void setValue(value);
void setMinValue(value);
void setMaxValue(value);
F32 getValue();
F32 getMinValue();
F32 getMaxValue();
void setFillSpeed(multiplier);
F32 getFillSpeed();
void setDrainSpeed(multiplier);
F32 getDrainSpeed();
void fill(delta value);
void fillTo(value);
void drain(delta value);
void drainTo(value);
void setPulseRate(rate);
S32 getPulseRate();
void setPulseThreshold(threshold);
F32 getPulseThreshold();
void setPulseAlways(true/false);
bool getPulseAlways();
void setShowBackgroundFill(true/false);
bool getShowBackgroundFill();
void setShowTargetFill(true/false);
bool getShowTargetFill();
Sample Scripts
Because GuiGenericBarHud is generic, some scripting must be done to hook up your game to the GuiGenericBarHud control. This can usually be done in just a few lines of script. Below are some sample scripts to show you how this is done:
This snippet shows how to use GuiGenericBarHud as a replacement for GuiHealthBarHud.
function UpdateHealthBar(%player)
{
HealthBar.FillTo(1.0 - %player.GetDamagePercent());
schedule(100, 0, UpdateHealthBar, %player);
}This snippet shows how to use the GuiGenericBarHud as a countdown timer, useful for displaying how long a player can stay underwater before drowning.
BreathBar.setValue(1.0); // Set the bar value to the max value
UpdateBreathTimerBar(10); // Start the 10 second countdown before breath runs out
...
function UpdateBreathTimerBar(%seconds)
{
BreathBar.Drain(100 / (%seconds * 1000));
schedule(100, 0, UpdateBreathTimerBar, %seconds);
}This snippet shows how to use the GuiGenericBarHud to display the player health when unmounted, and automatically switch to display vehicle health when the player is mounted on a vehicle.
function UpdateHealthBar(%client)
{
%obj = %client.getControlObject();
if (!(%obj.getType() & $TypeMasks::vehicleObjectType)) %obj = %client.player;
HealthBar.FillTo(1.0 - %obj.GetDamagePercent());
schedule(100, 0, UpdateHealthBar, %client);
}Credits
Many thanks to Frank Carney who originally came up with the base code that is GuiGenericBarHud.
Updates
2/25/2006 - Added support so pulsing the GuiGenericBarHud will use the alpha value specified in the fill color.
Enjoy!
#2
I have 32 bars but I had to rip them from the energy bar in the engine.
I use them for experience points.
Nice resource!
Ari
12/24/2005 (3:36 am)
Where were you 6 months ago? :pI have 32 bars but I had to rip them from the energy bar in the engine.
I use them for experience points.
Nice resource!
Ari
#3
12/30/2005 (10:43 am)
Perfect!! :))
#4
01/07/2006 (4:52 am)
Perfect, thanks for this resource. Works like a beauty.
#5
01/13/2006 (8:09 pm)
can someone please mention where these snippets should be located?
#6
been a while since i coded torque :)
01/13/2006 (8:19 pm)
my apologies, what i needed to do was name a Gui Element (The generic bar) "HealthBar"been a while since i coded torque :)
#7
01/24/2006 (3:00 am)
Excellent work, Bob! Well done!!
#8
Edit: Lol stupid lowercase letters. Fixed it, capitalization issues.
02/14/2006 (3:21 pm)
Could you make the small modifications to make this compatible with TSE? I've attempted to change the dgl calls to GFX calls, and change the include files to match the new directories, but I still get errors. :(Edit: Lol stupid lowercase letters. Fixed it, capitalization issues.
#9
Down the road when TSE is closer to completion I'll probably buy it, at which point I can modify this resource to support it, but until then there's nothing I can do. Sorry.
02/14/2006 (5:05 pm)
C2, if I had TSE, I would, but unfortunately I don't.Down the road when TSE is closer to completion I'll probably buy it, at which point I can modify this resource to support it, but until then there's nothing I can do. Sorry.
#10
02/15/2006 (3:14 pm)
Read my edited post, lol. I fixed it. Now I just need to figure out how to work it as a replacement for my health bar, so I can use the added features.
#11
fps/server/scripts/player.cs (774): Unknown command UpdateHealthBar.
Object (1690) Player -> ShapeBase -> GameBase -> SceneObject -> NetObject -> SimObject
I put the function UpdateHealthBar in player.cs, and then in the Armor::onAdd and Armor::onDamage functions I put %obj.UpdateHealthBar() in. I can't get this to work as my health bar. :(
02/23/2006 (11:23 am)
I need some help, lol. I keep getting an error in the console:fps/server/scripts/player.cs (774): Unknown command UpdateHealthBar.
Object (1690) Player -> ShapeBase -> GameBase -> SceneObject -> NetObject -> SimObject
I put the function UpdateHealthBar in player.cs, and then in the Armor::onAdd and Armor::onDamage functions I put %obj.UpdateHealthBar() in. I can't get this to work as my health bar. :(
#12
02/23/2006 (11:27 am)
C2, try calling UpdateHealthBar(%obj) instead of %obj.UpdateHealthBar().
#13
And this in the ::damage function:
Probably wrong, but it appears to work somewhat, despite a console message of:
02/23/2006 (12:55 pm)
I got it working differently, by putting this modified function:function Player::UpdateHealthBar(%this, %obj)
{
HealthBar.FillTo(1.0 - %this.GetDamagePercent());
schedule(100, 0, UpdateHealthBar, %this);
}And this in the ::damage function:
%obj.UpdateHealthBar();
Probably wrong, but it appears to work somewhat, despite a console message of:
UpdateHealthBar: Unknown command. UpdateHealthBar: Unknown command.each time I get damaged. But there are glitches, and the color is dark green. How do I change the color scheme?
#14
As for that error message, it's because you changed UpdateHealthBar to be a class function of Player. That schedule line in UpdateHealth bar is expecting to call UpdateHealthBar as a free function, not a class function.
I'm not sure the correct syntax to make schedule call a class function, nor am I even certain that's possible. Unless you know how, you may be better off keeping it as a free function like in the Sample Scripts of this resource.
02/23/2006 (1:10 pm)
C2, you can change the fill color by going into your GUI editor and selecting the GuiGenericBarHud control. The property box in the lower right allows you to modify the fill color, the background fill color, and the frame color.As for that error message, it's because you changed UpdateHealthBar to be a class function of Player. That schedule line in UpdateHealth bar is expecting to call UpdateHealthBar as a free function, not a class function.
I'm not sure the correct syntax to make schedule call a class function, nor am I even certain that's possible. Unless you know how, you may be better off keeping it as a free function like in the Sample Scripts of this resource.
#15
What other "glitches" are you experiencing?
02/23/2006 (1:13 pm)
Scratch that, I see what you're doing now. By placing UpdateHealthBar inside the Player class you can actually REMOVE that schedule line from UpdateHealthBar. This should fix your "unknown command" error.What other "glitches" are you experiencing?
#16
Glitches, well, I used to use the repairRate to make my health bar (shields) recharge, so that was making things odd. Fixed that.
Anyway, I'd like the health bar to slowly recharge to full health, somehow.
A problem though, I have, is why does the fill color slowly unfill when the health bar is decreased.
Situation:
100% Health, take 90% damage: Health bar decreases to 10% instantly, background fill color decreases smoothly slowly to the 10%. Why?
02/23/2006 (1:26 pm)
Ah, yes, that did it.Glitches, well, I used to use the repairRate to make my health bar (shields) recharge, so that was making things odd. Fixed that.
Anyway, I'd like the health bar to slowly recharge to full health, somehow.
A problem though, I have, is why does the fill color slowly unfill when the health bar is decreased.
Situation:
100% Health, take 90% damage: Health bar decreases to 10% instantly, background fill color decreases smoothly slowly to the 10%. Why?
#17
The first line of UpdateHealthBar in my SampleScripts retrieves the players current health and changes GuiGenericBarHud to display this value. The second line, the schedule line, just calls UpdateHealthBar after 100 milliseconds have passed. The end result is that UpdateHealthBar is called 10 times per second to keep your GuiGenericBarHud in sync with your actual player health.
In your modified version, you are notified when a player is hurt via the Player::Damage function being called. So you removed the schedule call and instead only call UpdateHealthBar when the player is actually damaged.
So the glitch that you are seeing is the result of not calling UpdateHealthBar when your health INCREASES.
Try changing your UpdateHealthBar function to look like this:
What this should do, is cause your version of UpdateHealthBar to be called 10 times a second, just like in the sample scripts, but still keeps it a part of the Player class. What you'll need to do next is REMOVE the line in ::damage that calls UpdateHealthBar, because if you leave it in it will just cause UpdateHealthBar to be called 10 MORE times a second than it already is. Thus if you're damaged 10 times it will call UpdateHealthBar 100 times a second, increasing each time your damaged.
So instead of calling that in ::damage, you just need to call UpdateHealthBar ONE time, when the player class is first created. So in your Player::OnAdd or similar function, just call %player.UpdateHealthBar(); and you should be set.
02/23/2006 (1:44 pm)
Ok, let me explain then what my original UpdateHealthBar function in the Sample Scripts does, compared to your new version that exists inside the Player class.The first line of UpdateHealthBar in my SampleScripts retrieves the players current health and changes GuiGenericBarHud to display this value. The second line, the schedule line, just calls UpdateHealthBar after 100 milliseconds have passed. The end result is that UpdateHealthBar is called 10 times per second to keep your GuiGenericBarHud in sync with your actual player health.
In your modified version, you are notified when a player is hurt via the Player::Damage function being called. So you removed the schedule call and instead only call UpdateHealthBar when the player is actually damaged.
So the glitch that you are seeing is the result of not calling UpdateHealthBar when your health INCREASES.
Try changing your UpdateHealthBar function to look like this:
function Player::UpdateHealthBar(%this)
{
HealthBar.FillTo(1.0 - %this.GetDamagePercent());
%this.schedule(100, 0, UpdateHealthBar);
}What this should do, is cause your version of UpdateHealthBar to be called 10 times a second, just like in the sample scripts, but still keeps it a part of the Player class. What you'll need to do next is REMOVE the line in ::damage that calls UpdateHealthBar, because if you leave it in it will just cause UpdateHealthBar to be called 10 MORE times a second than it already is. Thus if you're damaged 10 times it will call UpdateHealthBar 100 times a second, increasing each time your damaged.
So instead of calling that in ::damage, you just need to call UpdateHealthBar ONE time, when the player class is first created. So in your Player::OnAdd or similar function, just call %player.UpdateHealthBar(); and you should be set.
#18
02/23/2006 (1:57 pm)
The logic makes sense, unfortunately the scheduling doesn't appear to work right. If I put it as you have it, the health bar does not update further than the %obj.UpdateHealthBar(); I have in Armor::onAdd. (Which sets the health bar full when the player spawns/enters the game).
#19
02/23/2006 (2:03 pm)
C2, it should call itself every 100 ms once this function is called for the very first time (which should probably be done as soon as the game is started). When UpdateHealthBar is called for the first time the schedule will have this same function called in 100ms, at which point it will then schedule itself to be called again in 100ms, etc.
#20
02/23/2006 (2:09 pm)
Edited 
Torque Owner Shon Gale
Shon Gale
ATOMIX Productions
www.theatomizer.com