Simple HUD Tut

From GMod Wiki

Jump to: navigation, search

Hello, and welcome to mine (Minty Fresh's) first tutorial. First off, you might be thinking, "What is a hud?" Well, my answer to that is. A hud is a visual chart, which tells you. Your health, and ammo. But on other games, the hud might include a map. But on Garry'sMod, the hud is just your: Health, Armor (or "suit") and ammo.

CHAPTER 1

Contents

CHAPTER 1: Thesaurus

If you come across something throughought the tutorial, and think to yourself, "What the hell does that do/ mean." You can come to this thesaurus and find out!

    Thesaurus                 
LocalPlayer() -- This is YOU, the guy who's clientside we're currently running on.
local -- This defines a variable to be local, which means it will only be accessible in this lua file, and inside this "level of code block".
ScrW() -- This function, will get the width value of your game's resolution, this is normally used for the positioning of the hud!
ScrH() -- This is the same as ScrW() but instead of getting the WIDTH value, it gets the HEIGHT value!
draw.RoundedBox() -- Format: draw.RoundedBox(roundiness, X location (ScrW), Y Location (ScrH), Width, Height, Color( R,G,B) )
draw.SimpleText() -- Format: draw.SimpleText("Text", "Font", X Location, Y Location, Color( R, G B ))
end -- End will FINISH a function/expression

CHAPTER 2: Making a simple hud, and positioning

Ok, now we're going to MAKE a simple hud. What you're going to see in this hud is: (1) Health bar that indicates our health (2) Text that gives us a more detailed indication of our health. Now, I am going to go through positioning first up! So, i've put ScrH and ScrW in the thesaurus, now you need to know HOW to use them. Let's give you an example. NOTE: Don't do draw.RoundedBox(0,0, 0 because that won't work, i'm just explaining what to do, step by step.

draw.RoundedBox(0, 0 -- The FIRST 0 represents the roundiness, 0 meaning square like a brick, the SECOND 0 represents the very LEFT of the screen, now I don't recommend you using 1200 to get to the right side of your screen, it will move around when you change resolutions (Might not be SO far to the right.) If you want to use the right side of your screen, use: ScrW() - Width of whatever you've made - how far to the left, FROM the right of your screen, I do it like this, because it makes it really easy to understand. The box is drawn from it's top-left corner.
--Now, let's carry on. 
draw.RoundedBox(0, 0, 10 -- Just like 0 for the x co-ordinate, you can use 10 to make it 10 units down from the TOP, but if you want it at the bottom use ScrH() - HEIGHT of the thing you've made - how much you want it to go UP!
draw.RoundedBox(0, 0, 10, 20, 20, Color(255,0,0,255)) -- Ok, so 20, 20 = 20 wide and 20 high, Color(255,0,0,255) meaning, red = 255, green =  0, blue = 0, alpha / transparency = 255! 

Ok, so FIRST, we make our values (So we don't have to type the LONG version all the time!

 

NOW, We can start making our hud

function hud() -- Consider functions like a cyber button, and everything INSIDE the function turns on when this cyber button is pressed.
local health = LocalPlayer():Health()
draw.RoundedBox(0, 5, ScrH() - 15 - 20, health, 15, Color(255,0,0,255))
draw.SimpleText(health, "default", 10, ScrH() - 15 - 40, Color(255,255,255,255))
end 
hook.Add("HUDPaint", "MyHudName", hud) -- I'll explain hooks and functions in a second

This is what it SHOULD look like (NOTE: I have already removed the default gmod hud! read down for more information on doing so!) TestHUD.jpg

CHAPTER 3: Hiding the default hl2/gmod hud!

Now, there are special elements for the hud, which we need to locate, and turn off! Firstly, here are the elements we are turning off.

"CHUDHealth" -- Which is the health
"CHUDBattery" -- Which is the armor!
 

Now to HIDE the hud!

function hidehud(name)
	for k, v in pairs({"CHudHealth", "CHudBattery"})do
		if name == v then return false end
	end
end
hook.Add("HUDShouldDraw", "HideOurHud:D", hidehud)

And THIS, is the whole code.

function hud() -- Consider functions like a cyber button, and everything INSIDE the function turns on when this cyber button is pressed.
local health = LocalPlayer():Health()
draw.RoundedBox(0, 5, ScrH() - 15 - 20, health, 15, Color(255,0,0,255))
draw.SimpleText(health, "default", 10, ScrH() - 15 - 40, Color(255,255,255,255))
end 
hook.Add("HUDPaint", "MyHudName", hud) -- I'll explain hooks and functions in a second
 
function hidehud(name)
	for k, v in pairs({"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"}) do
		if name == v then return false end
	end
end
hook.Add("HUDShouldDraw", "HideOurHud:D", hidehud)

CHAPTER 4: Hooks and Functions

Ok, so. This is MY understanding on hooks and functions, I'm not GOOD at lua, huds and derma are the ONLY things I currently know. But my understanding for hooks and functions are: A function, is like a button, with NO wiring, just a button. This button has a whole load of data just waiting to get sent.

And a hook, is pretty much the wiring, once you set up the hook to the function, that button is turned ON, and all the data is being sent.

So pretty much, when we hooked our function hud(), our hud got sent to the game, and made the health bar and text!

This is partially correct. A function is a place where code is held, all this code can be run at once by just executing the function. You execute a function by putting the functionname in your code, along with a parenthesis after that contains all [u]arguments[/u] that should be used. Arguments are like Data you send over to the function, so it doesn't do same stuff every time it runs, but it adjusts itself to whatever you want.

A hook is like a execute function, but you could have your function execute on certain places you cant edit, such as in the source code for garrysmod. Hooks are therefor there to execute your function for you.

The end

Thank you for reading this tutorial, I hope I helped you in learning how to make a hud in lua. Thanks to my mate Remos for teaching me alot about huds, and special thanks to the Australian group "TacoNation" They have given me such a experience with them.

If you need help, add my steam "batteh" my username is "Minty Fresh". OR, join up at www.taconation.net and contact me there. Thankyou for reading!

Additional Info

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox