How to: HUD

From GMod Wiki

Jump to: navigation, search
Lua: How to create a HUD.
Page white text.png Description:How to create a HUD. (Posted by lemming77 on the FP forums.) Edited by Carnag3
link=User:Esik1er Original Author:Esik1er
Group.png Contributors:Roelof
Calendar.png Created:December 19, 2006

Contents

How to Make a health bar HUD

I don't think there are any tutorials on the subject. I'll try to explain it for you... Whenever I write a hud, I write a function to draw it. This function can then be easily handed to the game to be drawn on every frame.

function myhud()

Defining "client"

I usually set up a variable to represent the player, then check to see whether the player is dead or not. If the player is dead, it ends the function. The player is referred to as 'client' for the rest of the script. Here we can also make the HUD hide if you have your camera out and also as an extra insurance policy tell it that if you have no weapon at all (e.g you are dead) then end here.

local client = LocalPlayer()
if not client:Alive() then return end
if (client:GetActiveWeapon() == NULL or client:GetActiveWeapon() == "Camera") then return end
 

Drawing the HUD

With that done, I start drawing the hud. A way I like to start with this is with a particular command...

draw.RoundedBox(3, 5, 5, 200, 100, Color(51, 58, 51, 255))

How to place the HUD

This draws a box on the screen. It has rounded corners, which looks nicer than a sharp corner. The first argument is how round the corners are. 0 is a sharp corner. The next two are coordinates, telling it where to put the box. The next two are the width and height. Lastly is the colour. Just your standard Red, Green, Blue and Alpha values. Next thing I usually do is add the actual information to the hud.

draw.SimpleText(client:Health() .. "%", "ScoreboardText", 100, 50, Color(86, 104, 86, 255), 0, 0)

Drawing the text

This command draws text to the screen. First argument is the actual text. Second is the font. The next two are coordinates for the position. Fifth is the colour. The last two are X Align and Y Align. I don't know what these do. Alternatively, you could use client:Armor() to get the player's armor, but that doesn't work right now. It will do in a future update, hopefully. Ammunition in your weapons can be obtained too. I'll try to explain these in the comments.

local mag_left = client:GetActiveWeapon():Clip1() // How much ammunition you have inside the current magazine
local mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) // How much ammunition you have outside the current magazine
local secondary_ammo = client:GetAmmoCount(client:GetActiveWeapon():GetSecondaryAmmoType())// How much ammunition you have for your secondary fire, such as the MP7's grenade launcher

Ending the function

That's the code for the hud explained. Now to make sure it's drawn. We want to end the function, before we do anything else.

end

Making sure the HUD is drawn

Next, we want to make sure our function is used to draw the hud.

hook.Add("HUDPaint", "myhud", myhud)


Hiding the default hud

Nearly done, now... You'll want this in the .lua file somewhere. It hides the health display, battery display, ammo display and secondary ammo display. If you don't want it to hide any of these things, just remove it from the table.

local tohide = { -- This is a table where the keys are the HUD items to hide
	["CHudHealth"] = true,
	["CHudBattery"] = true,
	["CHudAmmo"] = true,
	["CHudSecondaryAmmo"] = true
}
local function HUDShouldDraw(name) -- This is a local function because all functions should be local unless another file needs to run it
	if (tohide[name]) then     -- If the HUD name is a key in the table
		return false;      -- Return false.
	end
end
hook.Add("HUDShouldDraw", "How to: HUD Example HUD hider", HUDShouldDraw)

End Code

Here is your end result if you did it as stated above.

function myhud()
	local client = LocalPlayer()
	if !client:Alive() then return end
	if(client:GetActiveWeapon() == NULL or client:GetActiveWeapon() == "Camera") then return end
	draw.RoundedBox(3, 5, 5, 200, 100, Color(51, 58, 51, 255))
	draw.SimpleText(client:Health() .. "%", "ScoreboardText", 100, 50, Color(86, 104, 86, 255), 0, 0)
	local mag_left = client:GetActiveWeapon():Clip1() // How much ammunition you have inside the current magazine
	local mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) // How much ammunition you have outside the current magazine
	local secondary_ammo = client:GetAmmoCount(client:GetActiveWeapon():GetSecondaryAmmoType())// How much ammunition you have for your secondary fire, such as the MP7's grenade launcher
end
hook.Add("HUDPaint", "myhud", myhud)
 
local tohide = { -- This is a table where the keys are the HUD items to hide
	["CHudHealth"] = true,
	["CHudBattery"] = true,
	["CHudAmmo"] = true,
	["CHudSecondaryAmmo"] = true
}
local function HUDShouldDraw(name) -- This is a local function because all functions should be local unless another file needs to run it
	if (tohide[name]) then     -- If the HUD name is a key in the table
		return false;      -- Return false.
	end
end
hook.Add("HUDShouldDraw", "How to: HUD Example HUD hider", HUDShouldDraw)

This code may be placed anywhere that is clientside. This usually means cl_init.lua or any file included by it if you're making a sent/swep/gamemode or placing it in a file in lua/autorun/client (You might have to create the folder).

See Also

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox