Displaying the count of health kits
by Ryan Mick · 01/15/2009 (5:56 pm) · 14 comments
So recently I had seen a post asking how to display to the player the number of health kits they had in inventory. I and others pointed the questioner to the Ammo Hud Tutorial by Time Newell but the tutorial is a little dated. So to help out I have created this tutorial using the Health Kit but it really can be adapted to any item easily. This is based off of Tim's initial tutorial so if you have seen it before most of this will look familiar to you. We are only going to modify 5 files so make sure you backup your project. Ok, here we go.
In server\scripts\health.cs replace the HealthKit::onUse function with.
In server\scripts\health.cs add the following after the HealthKit::onUse function.
Next in server\script\games.cs add this to the bottom of the GameConnection::createPlayer function.
Now in client\ui\defaultGameProfiles.cs add this to the bottom.
Almost done! Now in client\ui\playGui.gui add this as the last control.
And to finish, in client\scripts\playGui.cs add this at the bottom.
That's it. Save your files and run your project. You should now see text above the health bars with "HK: 0". Even though this used the health kits it's easy to adapt this to any other items you want. Let me know if you have any questions and enjoy!
In server\scripts\health.cs replace the HealthKit::onUse function with.
function HealthKit::onUse(%this,%user)
{
// Apply some health to whoever uses it, the health kit is only
// used if the user is currently damaged.
if (%user.getDamageLevel() != 0) {
%user.decInventory(%this,1);
%user.applyRepair(%this.repairAmount);
if (%user.client){
messageClient(%user.client, 'MsgHealthKitUsed', 'c2Health Kit Applied');
//Health Kit Hud
%user.client.setHealthKitAmountHud(%user.getInventory(%this));
}
}
}In server\scripts\health.cs add the following after the HealthKit::onUse function.
//-----------------------------------------------------------------------------
//Health Kit Display
//-----------------------------------------------------------------------------
//Provides a way to get the value from the client object on the server
function GameConnection::setHealthKitAmountHud(%client, %amount)
{
if(%amount $= "")
%amount = "0";
commandToClient(%client, 'SetHealthKitAmountHud', %amount);
}
//We just want to provide a way to intercept these so we can update the client
//All processing is still done in the Item namespace
function HealthKit::onThrow(%this,%user,%amount)
{
Parent::onThrow(%this,%user,%amount);
//Health Kit Hud
%user.client.setHealthKitAmountHud(%user.getInventory(%this));
}
function HealthKit::onPickup(%this,%obj,%user,%amount)
{
Parent::onPickup(%this,%obj,%user,%amount);
//Health Kit Hud
%user.client.setHealthKitAmountHud(%user.getInventory(%this));
}
//This is a way for the client to make a request to get the current inventory
function serverCmdGetHealthKitAmountForHud(%client){
if(%client){
%client.setHealthKitAmountHud(%user.getInventory(HealthKit));
}
}Next in server\script\games.cs add this to the bottom of the GameConnection::createPlayer function.
//Health Kit Hud - this will update the players display every time the player is created or respawns. %this.setHealthKitAmountHud(%player.getInventory(HealthKit));
Now in client\ui\defaultGameProfiles.cs add this to the bottom.
//-----------------------------------------------------------------------------
// Health Kit hud profile
new GuiControlProfile ("HealthKitPrintProfile")
{
opaque = false;
fillColor = "128 128 128";
fontColor = "255 255 255";
border = true;
borderColor = "0 255 0";
};Almost done! Now in client\ui\playGui.gui add this as the last control.
new GuiTextCtrl(HealthKitAmount) {
canSaveDynamicFields = "0";
Profile = "HealthKitPrintProfile";
HorizSizing = "right";
VertSizing = "top";
position = "15 261";
Extent = "61 30";
MinExtent = "8 8";
canSave = "1";
Visible = "1";
hovertime = "1000";
text = "HK: 2";
maxLength = "1024";
};And to finish, in client\scripts\playGui.cs add this at the bottom.
//Health Kit Hud
function clientCmdSetHealthKitAmountHud(%value)
{
HealthKitAmount.setText("HK: " @ %value);
}That's it. Save your files and run your project. You should now see text above the health bars with "HK: 0". Even though this used the health kits it's easy to adapt this to any other items you want. Let me know if you have any questions and enjoy!
About the author
#2
Ps. Is there any books you would recommend to a newbie like me to learn how to write code like you do????????
01/15/2009 (6:27 pm)
Ryan, your the man.....the only problem I get now is when I use a healthkit it doesn't subtract from my amount, however it does go to the correct amount when I pick up another one...............this I'm sure Ryan could fix in nothing flat........I hope you dont take this wrong, but I love ya man...........DonniePs. Is there any books you would recommend to a newbie like me to learn how to write code like you do????????
#3
if (%user.client)
messageClient(%user.client, 'MsgHealthKitUsed', '\c2Health Kit Applied');
with
if (%user.client){
messageClient(%user.client, 'MsgHealthKitUsed', '\c2Health Kit Applied');
//Health Kit Hud
%user.client.setHealthKitAmountHud(%user.getInventory(%this));
}
01/15/2009 (7:17 pm)
Doh! Knew I missed something. In the function HealthKit::onUse replace:if (%user.client)
messageClient(%user.client, 'MsgHealthKitUsed', '\c2Health Kit Applied');
with
if (%user.client){
messageClient(%user.client, 'MsgHealthKitUsed', '\c2Health Kit Applied');
//Health Kit Hud
%user.client.setHealthKitAmountHud(%user.getInventory(%this));
}
#4
01/15/2009 (7:44 pm)
Ryan, Once again you prove to us newbies why we must learn from every thing and everyone, we can get our hands on. The fix worked all the way and thank you so very much, I know you didn't have to help me, but you did and if there is anything that I can do for you, please ask. I was close but yet so far away, half the battle for me is the scripting, but with this and other resources, it is getting a tad easier......Thank you so much..............Donnie
#5
01/15/2009 (7:47 pm)
No problem, for the most part I am a lurker here and chime in once in a while to help.
#6
Ryan, thanks for posting this. There are a ton of uses I can think of for it. :-)
01/15/2009 (8:29 pm)
Donnie, there are several tutorials around this site in the resources area, but if you really want to learn torque script, Get eather Ken Finneys books or Ed. Maurina's books here on GG. I have all of them, and they are invaluable tools.Ryan, thanks for posting this. There are a ton of uses I can think of for it. :-)
#7
Too late! I think you got a nibble. Great job man!
01/15/2009 (8:47 pm)
ooooh. pure awesomeness. If you feed us with goodies like this, you know we will just come back for more. Did you not see the sign "Do not feed the game developers..?" or the one "Keep windows rolled up at all times, do not dangle arms and legs outside the vehicle."Too late! I think you got a nibble. Great job man!
#9
01/15/2009 (9:50 pm)
Finney's books are great. It's been awhile since I looked at Maurina's book, but I recall it was pretty good too. It's also relatively simple to get the Torque Script code in Finney's books working with TGEA.
#10
@Donnie: I also recommend Finney's and Maurina's books. They were written for older versions of Torque but are still relevant today if you take the time to learn from them.
01/16/2009 (12:14 am)
Nice display of adaptability! @Donnie: I also recommend Finney's and Maurina's books. They were written for older versions of Torque but are still relevant today if you take the time to learn from them.
#11
Thanks.
01/16/2009 (3:30 am)
I've been banging my head against my desk trying to do the same thing with ammo in the Starter FPS Kit. I was trying to use the example from the tutorial that comes with the SDK, but ammo is handled differently. Looking at your instructions here, I think I know what I've missed.Thanks.
#12
Again, thanks.
01/16/2009 (3:35 am)
And of course I missed the Ammo Hud Tutorial link... Haven't been able to find resources and of course didn't start trying to do this until after the site update... now if only I could have edited my original comment...Again, thanks.
#13
01/16/2009 (9:00 am)
Well, this little distraction was exaclty what I needed. I have been busy migrating my guiAdvBarHud to TGEA and was stuck on rendering an arc. After taking time to do this I was able to take a fresh look at the code and figured out my problem. Now I only have 1 item to fix and then test it and post it. So if you don't like plain boring color filled health bars stay tuned!
#14
11/29/2010 (7:19 pm)
With everything going on with Torque Powered I have made a copy of this on my site http://sacdeveloper.com/Projects/Torque.aspx. I have also moved my resources to this location to better support the different versions of the engine.
Torque Owner Ryan Mick
Red Witch Entertainment