Scripted Numerical Health GUI
by Tim Heldna · 01/31/2007 (2:39 pm) · 12 comments
There are quite a few numerical health GUI resources available. This one will work across a network, is completely scripted, supports pulsing, does not assume a maxHealth of 100, works for both players & vehicles, will not render on death and is quick & easy to install.
Installation:
server/scripts/player.cs
Add the following code to the Armor::onAdd function:
Add the following code to the Armor::onDisabled function:
server/scripts/commands.cs
Add the following code:
client/scripts/client.cs
Add the following code:
client/ui/playGui.gui
Add the following code before the last closing brace };
That's it.
If you read through the code, you'll notice that the "updateHealth" function is continuously looped for each client whilst they are alive. The loop breaks on death and restarts again when the player respawns. This means that the text health GUI will always be updated, no matter what is influencing your health to increase or decrease in your game. This abolishes the need to create a function to update the health display in every circumstance that our health changes.
The health GUI will display a numerical health value for both players and vehicles or any other type of control object (turrets etc). The health GUI is automatically updated upon entering / exiting a vehicle.
The health will not render on death or if health is at a value of 0. It will render again on respawn or when health is > 0.
The pulsing I threw in for fun, feel free to disable it.
I hope you find this useful, enjoy!
Installation:
server/scripts/player.cs
Add the following code to the Armor::onAdd function:
// Numerical Health GUI %obj.client.updateHealth = true; %obj.updateHealth(%obj); // Numerical Health GUI
Add the following code to the Armor::onDisabled function:
// Numerical Health GUI %obj.client.updateHealth = false; // Numerical Health GUI
server/scripts/commands.cs
Add the following code:
//-----------------------------------------------------------------------------
// Numerical Health GUI (Server)
// %player = player server ID
//-----------------------------------------------------------------------------
function Player::updateHealth(%player)
{
// Calcualte player health
if(!%player.getControlObject())
{
%maxDamage = %player.getDatablock().maxDamage;
%damageLevel = %player.getDamageLevel();
%damagePercent = %player.getDamagePercent();
%curHealth = %maxDamage - %damageLevel;
%curHealth = mceil(%curHealth);
%curDamagePercent = getSubStr(%damagePercent, 0, 4);
}
// Calculate vehicle (or any other object that we are mounted to) health
else
{
%maxDamage = %player.getControlObject().getDatablock().maxDamage;
%damageLevel = %player.getControlObject().getDamageLevel();
%damagePercent = %player.getControlObject().getDamagePercent();
%curHealth = %maxDamage - %damageLevel;
%curHealth = mceil(%curHealth);
%curDamagePercent = getSubStr(%damagePercent, 0, 4);
}
// Send the objects current health level and percentage to the client,
// where it will update the numericalHealth GUI.
// This entire function will continue to loop and pass our current health
// on to the client whilst we are alive.
commandToClient(%player.client, 'setNumericalHealthGUI', %curHealth,
%curDamagePercent);
if(%player.client.updateHealth)
%player.schedule(100, updateHealth);
}
//-----------------------------------------------------------------------------client/scripts/client.cs
Add the following code:
//-----------------------------------------------------------------------------
// Numerical Health GUI (Client)
// %curHealth = player's current health level
// %curDamPercent = player's current health percentage
//-----------------------------------------------------------------------------
function clientCmdSetNumericalHealthGUI(%curHealth, %curDamPercent)
{
// The server has sent us our current health, display it on the GUI
numericalHealthGUI.setValue(%curHealth);
// The pulseThreshold is stored in and read from the
// "numericalHealthGUI" GuiTextCtrl (inside of playGui.gui)
%pulseThreshold = numericalHealthGUI.pulseThreshold;
// Even though this is mathematically incorrect, we need to subtract 0.01
// from our numericalHealthGUI pulseThreshold in order for pulsing to be
// triggered in sync with the stock health bar (GuiHealthBarHud).
// This is due to an inaccuracy within the GuiHealthBarHud source code.
%pulseThreshold = %pulseThreshold - 0.01;
// Pulse every 1.5 seconds
%pulseTime = 1500;
// Determine the percentage of health player is currently at
%curDamPercent = 1 - %curDamPercent;
// If health = 0 we must be dead, hide the GUI
if(!%curHealth)
numericalHealthGUI.setVisible(false);
// Ensure the GUI is set to visible whilst we have health / are alive
if(%pulseThreshold <= 0)
{
if(%curHealth)
{
numericalHealthGUI.setVisible(true);
}
else
numericalHealthGUI.setVisible(false);
}
// Ignore pulsing if none specified
if(%pulseThreshold <= 0)
return;
// If pulseThreshold is less than our current percentage of health,
// do not pulse
if(%pulseThreshold < %curDamPercent)
{
if(%curHealth)
numericalHealthGUI.setVisible(true);
}
// If pulseThreshold is greater than or equal to our current percentage
// of health, start pulsing
if(%pulseThreshold >= %curDamPercent)
{
if(numericalHealthGUI.isVisible())
numericalHealthGUI.schedule(%pulseTime / 2, setVisible, false);
if(!numericalHealthGUI.isVisible())
{
if(%curHealth)
numericalHealthGUI.schedule(%pulseTime / 2, setVisible, true);
}
}
}
//-----------------------------------------------------------------------------client/ui/playGui.gui
Add the following code before the last closing brace };
new GuiTextCtrl(numericalHealthGUI) {
Profile = "GuiTextProfile";
HorizSizing = "right";
VertSizing = "bottom";
position = "88 744";
Extent = "18 18";
MinExtent = "8 8";
Visible = "1";
maxLength = "255";
pulseThreshold = "0.25"; // Enter desired pulse threshold here
// Should be between 0 & 1, 0 for no pulse
};That's it.
If you read through the code, you'll notice that the "updateHealth" function is continuously looped for each client whilst they are alive. The loop breaks on death and restarts again when the player respawns. This means that the text health GUI will always be updated, no matter what is influencing your health to increase or decrease in your game. This abolishes the need to create a function to update the health display in every circumstance that our health changes.
The health GUI will display a numerical health value for both players and vehicles or any other type of control object (turrets etc). The health GUI is automatically updated upon entering / exiting a vehicle.
The health will not render on death or if health is at a value of 0. It will render again on respawn or when health is > 0.
The pulsing I threw in for fun, feel free to disable it.
I hope you find this useful, enjoy!
About the author
Recent Blogs
• BCS Street props• Character Pack - Vince
• Recent Artwork
• Progress of our Weapon Pack
• Digital Speedometer
#2
It is set to true on spawn in the onAdd method and set to false in the onDisabled method.
02/06/2007 (5:42 pm)
Yes. I store the updateHealth bool on the client (not the player/control object). It is set to true on spawn in the onAdd method and set to false in the onDisabled method.
%obj.client.updateHealth = false;
#3
Armor::OnRemove function instead of disabled
is there a way to make this bigger and change the color without modifying the SDK
06/16/2007 (7:42 pm)
This worked in 1.5.2 no problemArmor::OnRemove function instead of disabled
is there a way to make this bigger and change the color without modifying the SDK
#4
06/16/2007 (11:13 pm)
Just create your own text profile with any type of font type, color and size that you like.
#5
thanks Tim for this , any suggestion on actually making the numerical numbers flash.
06/18/2007 (3:59 pm)
After going back through my book I found that this can be quite useful instead of using the health bar, I followed Tim's advice, and it took me a bit I read both books over the winter and still had to refrence it up.thanks Tim for this , any suggestion on actually making the numerical numbers flash.
new GuiControlProfile(fontColorNumHealth) {
fontType = "Arial Bold";
fontSize = 35;
fontColor = "245 0 41" ;
};
new GuiTextCtrl(numericalHealthGUI) {
canSaveDynamicFields = "0";
Profile = "fontColorNumHealth";
HorizSizing = "right";
VertSizing = "bottom";
position = "912 8";
Extent = "100 29";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
hovertime = "1000";
text = "100";
maxLength = "255";
};
#6
08/26/2007 (7:33 am)
I think i know an easy sollution for making it a non-textual gui, ie a bar instead. Im gonna try it out and report if i am able to contribute anything good.
#7
08/26/2007 (12:05 pm)
Ted, just use the gui that comes with the Starter.fps
#8
P.S. the numbers in my resource do flash, if installed correctly. ;)
08/29/2007 (8:20 am)
Aye, the reason I posted a numerical health GUI is because Torque already features a health bar.P.S. the numbers in my resource do flash, if installed correctly. ;)
#9
10/26/2007 (2:32 pm)
Thanks Tim!
#10
08/08/2008 (8:35 am)
Its the way I want it...
#11
12/15/2008 (11:40 am)
doesnt work for me
#12
Thanks
01/20/2009 (1:41 pm)
I am using TGEA 1.8 and there are no console errors but the numbers do not show up in the gui.Thanks

Torque Owner James Laker (BurNinG)
is correct?