Hi, I'm kinda new to LUA coding but I wanted to get better at it, so I tried to create a simple armor/health bar hud for darkrp.
I get no errors but it ends up looking like [URL="http://imgur.com/YrX5PtG"]this[/URL]
Heres my HUD Code:
[CODE]function hudPaint()
local ply = LocalPlayer()
local HP = ply:Health()
local Armor = ply:Armor()
--Background
draw.RoundedBox(4, 0, ScrH()-55, 1360, 60, Color(0, 0, 0, 212))
--HP/Armor Background
draw.RoundedBox(4, 260, ScrH()-44, 220, 40, Color(0, 0, 0, 226))
draw.RoundedBox(4, 21, ScrH()-45, 220, 40, Color(0, 0, 0, 226))
--HP/Armor
draw.RoundedBox(4, 260, ScrH()-44, math.Clamp( HP, 0, 200 )*2, 40, Color(4, 0, 255, 255))
draw.RoundedBox(4, 22, ScrH()-46, math.Clamp( Armor, 0, 200 )*2, 40, Color(255, 0, 0, 255))
--Text
draw.DrawText(ply:Health(), "Arial24", 131.5, 730, Color(255, 255, 255, 255))
draw.DrawText(ply:Armor(), "Arial24", 378.5, 728, Color(255, 255, 255, 255))
end
hook.Add("HUDPaint", "DarkRP_Mod_HUDPaint", hudPaint)[/CODE]
If you can fix this for me, please add me on steam [URL="http://steamcommunity.com/profiles/76561198132133006"]here.[/URL]
Change this
[QUOTE=BastionTTT;47199225]
[code]
draw.RoundedBox(4, 260, ScrH()-44, math.Clamp( HP, 0, 200 )*2, 40, Color(4, 0, 255, 255))
draw.RoundedBox(4, 22, ScrH()-46, math.Clamp( Armor, 0, 200 )*2, 40, Color(255, 0, 0, 255))
[/code]
[/QUOTE]
to this
[code]
draw.RoundedBox(4, 22, ScrH()-44, math.Clamp( HP, 0, 200 )*2, 40, Color(4, 0, 255, 255))
draw.RoundedBox(4, 260, ScrH()-46, math.Clamp( Armor, 0, 200 )*2, 40, Color(255, 0, 0, 255))
[/code]
You had your Armor and HP bars reversed
[QUOTE=Zachy;47199364]Change this
to this
[code]
draw.RoundedBox(4, 22, ScrH()-44, math.Clamp( HP, 0, 200 )*2, 40, Color(4, 0, 255, 255))
draw.RoundedBox(4, 260, ScrH()-46, math.Clamp( Armor, 0, 200 )*2, 40, Color(255, 0, 0, 255))
[/code]
You had your Armor and HP bars reversed[/QUOTE]
I got the same problem, just the armor and HP bar are reversed.
Can someone please help me?
[editline]23rd February 2015[/editline]
Can someone atleast try and help? I'm pretty new to LUA and I don't know why its not working right.
[QUOTE=BastionTTT;47200198]Can someone please help me?
[editline]23rd February 2015[/editline]
Can someone atleast try and help? I'm pretty new to LUA and I don't know why its not working right.[/QUOTE]
Rate me dumb, hate me or whatever but stop saying "LUA"
First, convert your health and armor values to a 0-1 value. Next, use the modifier you just created to multiply against the max-width of the bar, there's that. So, the back-ground bar, if you use one, will be width max-width while the colored portion will be max-width * HealthModifier or ArmorModifier.
Proper HUD Creation: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/proper_hud_creation.lua.html[/url]
How to set up a HUD to scale properly: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/understanding_hardcoding_of_screensizes.lua.html[/url]
Some examples:
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/basic_healthbar.lua.html[/url] - Notice this one uses _w * _modifier; that's what I talked about above.. This way you isolate segments of the code to make it easier to modify later.. Say you don't like the width, modify the width and that's it..
You're hard-coding values instead of using simple tricks which makes it harder to change later on...
You could look into polys: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/creating_shapes_using_poly.lua.html[/url]
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/tilted_rectangle_poly_as_health_meter.lua.html[/url] - notice I create a helper function which simplifies the poly creation process by setting it so I only need to define x, y, w, h and tilt instead of awkward vars such as start x, start y, end x, end y, etc...
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/simplified_circles_with_poly.lua.html[/url]
If you do use poly, I'd suggest creating them once outside of the hook ( to prevent needing to create a new table each time it draws ) then drawing them in the hook.. Update the table if the resolution changes.
Hope this helps!
You are passing [i]HP[/i] to the armour bar and [i]Armor[/i] to the health bar. It should be this:
[code]
--HP/Armor
draw.RoundedBox(4, 260, ScrH()-44, math.Clamp( Armor, 0, 200 )*2, 40, Color(4, 0, 255, 255))
draw.RoundedBox(4, 22, ScrH()-46, math.Clamp( HP, 0, 200 )*2, 40, Color(255, 0, 0, 255))
[/code]
Acecool is right, though; you should follow his advice.
[QUOTE=Netheous;47201899]Rate me dumb, hate me or whatever but stop saying "LUA"[/QUOTE]
Sorry, I'm calling it by its name but yanno I guess I'm a noob for doing that
[QUOTE=BastionTTT;47205416]Sorry, I'm calling it by its name but yanno I guess I'm a noob for doing that[/QUOTE]
A lot of people get confused and think it's an acronym. Lua is actually the Portuguese word for "Moon", and is where the name for the language came from. :downs:
[QUOTE=thefreeman193;47205257]You are passing [i]HP[/i] to the armour bar and [i]Armor[/i] to the health bar. It should be this:
[code]
--HP/Armor
draw.RoundedBox(4, 260, ScrH()-44, math.Clamp( Armor, 0, 200 )*2, 40, Color(4, 0, 255, 255))
draw.RoundedBox(4, 22, ScrH()-46, math.Clamp( HP, 0, 200 )*2, 40, Color(255, 0, 0, 255))
[/code]
Acecool is right, though; you should follow his advice.[/QUOTE]
Thank you for this It worked.
[editline]24th February 2015[/editline]
[QUOTE=Acecool;47202297]First, convert your health and armor values to a 0-1 value. Next, use the modifier you just created to multiply against the max-width of the bar, there's that. So, the back-ground bar, if you use one, will be width max-width while the colored portion will be max-width * HealthModifier or ArmorModifier.
Proper HUD Creation: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/proper_hud_creation.lua.html[/url]
How to set up a HUD to scale properly: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/understanding_hardcoding_of_screensizes.lua.html[/url]
Some examples:
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/basic_healthbar.lua.html[/url] - Notice this one uses _w * _modifier; that's what I talked about above.. This way you isolate segments of the code to make it easier to modify later.. Say you don't like the width, modify the width and that's it..
You're hard-coding values instead of using simple tricks which makes it harder to change later on...
You could look into polys: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/creating_shapes_using_poly.lua.html[/url]
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/tilted_rectangle_poly_as_health_meter.lua.html[/url] - notice I create a helper function which simplifies the poly creation process by setting it so I only need to define x, y, w, h and tilt instead of awkward vars such as start x, start y, end x, end y, etc...
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/simplified_circles_with_poly.lua.html[/url]
If you do use poly, I'd suggest creating them once outside of the hook ( to prevent needing to create a new table each time it draws ) then drawing them in the hook.. Update the table if the resolution changes.
Hope this helps![/QUOTE]
Next time I make a HUD, I'll most likely follow these instructions. Thanks alot for the help dude I really appreciate all of this!
[QUOTE=thefreeman193;47205541]A lot of people get confused and think it's an acronym. Lua is actually the Portuguese word for "Moon", and is where the name for the language came from. :downs:[/QUOTE]
Do you know where I could put images to use in my HUD? like where in the folders?
Stick them in gamemodes/your_gamemode/content/materials/your_server_or_gamemode_name/whatever_you_want/to/image.png
You can also use addons/your_addon_name/materials/........
If you want a system to make all of your resource files be downloaded by the client ( or at least resource.AddSingleFile each one so it can be downloaded from FastDL or ServerDL[ not recommended from ServerDL ] ) then take a look at this: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/server_srcds_steamcmd/setting_up_downloads_using_recursive_resource_system.lua.html[/url]
Sorry, you need to Log In to post a reply to this thread.