Numeric Health Indicator
by Xavier "eXoDuS" Amado · 12/21/2001 (2:43 pm) · 8 comments
Ok first let's create the gui stuff. Add this code to the end of PlayGui.gui but before the last ending brace ( }; )
This is the gui that will display the ammo.
Now in PlayGui.cs add this to the end:
Add this to defaultGameProfiles.cs:
This is the client command that will be executed in the server side part of the code.
Now in player.cs preferably at the end add this function:
That establishes the connection with the client side and sends the value of what command to run clientside and what value to pass to it. In this case the command is SetHealthAmountHud, and the value is %amount, which is the players health and which is stated as %value inside the client command.
Now, still in player.cs look for Armor::OnDamage( ), you should see an if that looks like this: if (%delta > 0 && %obj.getState() !$= "Dead"), well before that if's closing brace put this line of code:
That actually sends the client the message with the value to display, in this case the value will be, the max health the player can have minus the damage that the player has.
Now, head to game.cs and look for this line: function GameConnection::createPlayer(%this, %spawnPoint), inside that function but at the end just before the closing brace add this line:
This a temporary hack, cause it would be better to use maxDamage instead of 100 but let me see how to get the var maxDamage from that part of the code first and then ill update the resource.
And for the last part we have the healthkit and healthpatch. Head to health.cs and inside the function HealthKit::onUse(%this,%user) and before the line %user.applyRepair(%this.repairAmount); add this code:
And inside the function HealthPatch::onCollision(%this,%obj,%col) and again before the applyRepair line add this code:
That's it, i advice you that this code is bullet-proof, so take it easy J
Btw, ill polish this tutorial and maybe submit it again, but lots of people were asking about this on IRC so i quickly typed this resource. Sorry for any spelling mistakes it might have, and happy coding. In a future version i'll add a getHealthLevel function to the C++ code instead of getting damage and doing the math.
Thanks a lot to Raistlin from IRC, dedicated to Is from IRC too :)
new GuiTextCtrl(HealthAmount) {
profile = "HealthPrintProfile";
horizSizing = "relative";
vertSizing = "relative";
position = "476 452";
extent = "108 24";
minExtent = "8 8";
visible = "1";
helpTag = "0";
maxLength = "255";
};This is the gui that will display the ammo.
Now in PlayGui.cs add this to the end:
function clientCmdSetHealthAmountHud(%value)
{
HealthAmount.setText("Health: " @ %value);
}Add this to defaultGameProfiles.cs:
new GuiControlProfile ("HealthPrintProfile")
{
opaque = false;
fillColor = "128 128 128";
fontColor = "0 255 0";
border = true;
borderColor = "0 255 0";
};This is the client command that will be executed in the server side part of the code.
Now in player.cs preferably at the end add this function:
function GameConnection::setHealthAmountHud(%client, %amount)
{
commandToClient(%client, 'SetHealthAmountHud', %amount);
}That establishes the connection with the client side and sends the value of what command to run clientside and what value to pass to it. In this case the command is SetHealthAmountHud, and the value is %amount, which is the players health and which is stated as %value inside the client command.
Now, still in player.cs look for Armor::OnDamage( ), you should see an if that looks like this: if (%delta > 0 && %obj.getState() !$= "Dead"), well before that if's closing brace put this line of code:
%obj.client.setHealthAmountHud(%this.maxDamage - %obj.getDamageLevel() );
That actually sends the client the message with the value to display, in this case the value will be, the max health the player can have minus the damage that the player has.
Now, head to game.cs and look for this line: function GameConnection::createPlayer(%this, %spawnPoint), inside that function but at the end just before the closing brace add this line:
%this.setHealthAmountHud(100);
This a temporary hack, cause it would be better to use maxDamage instead of 100 but let me see how to get the var maxDamage from that part of the code first and then ill update the resource.
And for the last part we have the healthkit and healthpatch. Head to health.cs and inside the function HealthKit::onUse(%this,%user) and before the line %user.applyRepair(%this.repairAmount); add this code:
if (( (100 - %user.getDamageLevel() ) + %this.repairAmount) > 100)
%user.client.setHealthAmountHud(100);
else
%user.client.setHealthAmountHud( (100 - %user.getDamageLevel() ) + %this.repairAmount);And inside the function HealthPatch::onCollision(%this,%obj,%col) and again before the applyRepair line add this code:
if (( (100 - %col.getDamageLevel() ) + %this.repairAmount) > 100)
%col.client.setHealthAmountHud(100);
else
%col.client.setHealthAmountHud( (100 - %col.getDamageLevel() ) + %this.repairAmount);That's it, i advice you that this code is bullet-proof, so take it easy J
Btw, ill polish this tutorial and maybe submit it again, but lots of people were asking about this on IRC so i quickly typed this resource. Sorry for any spelling mistakes it might have, and happy coding. In a future version i'll add a getHealthLevel function to the C++ code instead of getting damage and doing the math.
Thanks a lot to Raistlin from IRC, dedicated to Is from IRC too :)
#2
12/18/2001 (5:52 pm)
Yes, thanks !
#3
Instead of using the object's max damage level minus the object's current damage level, you could do:
%pcnt = %player.getDamagePercent();
%pcnt = %pcnt <= 0 ? 0 : %pcnt;
%val = mFloor(100 - (%pcnt * 100));
Then send %val. This way you'll get the percentage. It checks to make sure it's a positive number.
12/25/2001 (10:12 pm)
You would use %obj.getDataBlock().maxDamage if you're trying to get an object's max damage.Instead of using the object's max damage level minus the object's current damage level, you could do:
%pcnt = %player.getDamagePercent();
%pcnt = %pcnt <= 0 ? 0 : %pcnt;
%val = mFloor(100 - (%pcnt * 100));
Then send %val. This way you'll get the percentage. It checks to make sure it's a positive number.
#4
new GuiControlProfile ("HealthPrintProfile")
{
fontType = "Verdana";
fontSize = 16;
fontColor = "0 255 0";
fillColor = "128 128 128";
autoSizeWidth = true;
autoSizeHeight = true;
};
just thought i'd add that =)
- ian wheat
01/10/2002 (4:54 pm)
if you don't like the small size of the text (and the font it's in) you can change the part in defaultGameProfiles.cs to:new GuiControlProfile ("HealthPrintProfile")
{
fontType = "Verdana";
fontSize = 16;
fontColor = "0 255 0";
fillColor = "128 128 128";
autoSizeWidth = true;
autoSizeHeight = true;
};
just thought i'd add that =)
- ian wheat
#5
05/25/2006 (1:15 pm)
wow thanks, this is just what i need cheers guys!
#6
05/29/2006 (5:52 pm)
Very helpful. Thanks!
#7
I have a feeling it is something in this spawn player addition.
I fixed this problem by doing the following
06/27/2006 (8:58 pm)
For some reason it isn't displaying the health when you first enter the game, but after you take damage mine is showing the right information.I have a feeling it is something in this spawn player addition.
%this.setHealthAmountHud(%obj.getDataBlock().maxDamage);seems like to me that needs to pass a client ID for the function we added in player.cs
function GameConnection::setHealthAmountHud(%client, %amount)
{
commandToClient(%client, 'SetHealthAmountHud', %amount);
}I fixed this problem by doing the following
%player.setHealthAmountHud(%obj.getDataBlock().maxDamage);
#8
09/30/2006 (8:56 am)
I had a problem where the health was sometimes 45.674 I dont think decimal places are good so I did this:function GameConnection::setHealthAmountHud(%client, %amount)
{
%ramount = round(%amount, 0, 3);
commandToClient(%client, 'SetHealthAmountHud', %ramount);
}function round(%a, %start, %end)
{
return getSubStr(%a, %start, %end);
} 
Ian Wheat
thanks for the dedication =D