Ryan Mick posted on November 29, 2010 14:11

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.
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));
}
}
}
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));
}
}
//----------------------------------------------------------------------------- //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));
//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";
};
//----------------------------------------------------------------------------- // 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";
};
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);
}
//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!