• I need help with a lua HUD
    44 replies, posted
Thanks what about the Agenda? Can I make my own agenda template and place it in a different spot?
When it comes to HUD, think of every element as anchored to a corner of the screen. If your element is anchored to the... top left corner: Start at 0,0 top right corner: Start at ScrW(), 0 bottom left corner: Start at 0,ScrH() bottom right corner: Start at ScrW(),ScrH() Here is some code of some rectangles anchored in each of the 4 corners: [lua] local w,h = 100,50 //Size of rectangles local xoffset,yoffset = 90,35 //How far to draw the rectangles away from the edges //top left anchor draw.RoundedBox(4, xoffset, yoffset, w, h, Color(255,0,0)) //top right anchor draw.RoundedBox(4, ScrW() - xoffset - w, yoffset, w, h, Color(0,0,255)) //bottom left anchor draw.RoundedBox(4, xoffset, ScrH() - yoffset - h, w, h, Color(0,255,0)) //bottom right anchor draw.RoundedBox(4, ScrW() - xoffset - w, ScrH() - yoffset - h, w, h, Color(200,100,20)) [/lua] This code won't scale the rectangles' size, but it will scale the position which is usually good enough. If you're using derma, you can use PANEL:AlignTop/Bottom/Left/Right for this.
Ok I have 1 more question 1. How can I add a box around each part of the HUD. EX. [Health, Armor, Money etc.] with the spacing. -- If I have to rewrite the code I probably wont try it right away.--
You may have to increase the gap size, but basically just draw the box like this: [lua] local extraW = 10 local w = text_width --whatever you call it in your code. The result of surface.GetTextSize() draw.RoundedBox(4, x-extraW, y, w+extraW*2, h) [/lua]
I see what you are saying but kinda not. Here is an example of the first bit. [QUOTE] //LOCAL INPUTS local gap = 35 local smallgap = 15 local extraW = 10 local w = name local y = ScrH() / 220 local infoy = ScrH() / 250 local x = 0 local xbox = 2 local ply = LocalPlayer() //HUD BASE surface.SetDrawColor( black ) surface.DrawRect( 0, 0, ScrW(), ScrH() / 30 ) surface.SetFont("Font") // NAME local name = ply:getDarkRPVar("rpname") surface.SetTextColor( white ) surface.SetTextPos( x + smallgap, infoy ) surface.DrawText( name ) x = x + surface.GetTextSize( name ) // HEALTH local health = ply:Health() local printhealth = "Health - " .. health .. "%" surface.SetTextColor( red ) surface.SetTextPos( x + gap, infoy ) surface.DrawText( printhealth ) x = x + surface.GetTextSize( printhealth ) + gap[/QUOTE]
[QUOTE=xander1998;51478306]I see what you are saying but kinda not. Here is an example of the first bit.[/QUOTE] So you would do something like: [lua] //LOCAL INPUTS local gap = 35 local smallgap = 15 local extraW = 10 local w = name local y = ScrH() / 220 local infoy = ScrH() / 250 local x = 0 local xbox = 2 local ply = LocalPlayer() //HUD BASE surface.SetDrawColor( black ) surface.DrawRect( 0, 0, ScrW(), ScrH() / 30 ) surface.SetFont("Font") // NAME local name = ply:getDarkRPVar("rpname") local tw = surface.GetTextSize( name ) draw.RoundedBox(4, x-extraW, y, tw+extraW*2, h,color_black) surface.SetTextColor( white ) surface.SetTextPos( x + smallgap, infoy ) surface.DrawText( name ) x = x + tw // HEALTH local health = ply:Health() local printhealth = "Health - " .. health .. "%" tw = surface.GetTextSize( printhealth ) draw.RoundedBox(4, x-extraW, y, tw+extraW*2, h,color_black) surface.SetTextColor( red ) surface.SetTextPos( x + gap, infoy ) surface.DrawText( printhealth ) x = x + tw + gap [/lua]
and why am I getting an error like this? [QUOTE][ERROR] addons/darkrpmodification-master/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:63: bad argument #1 to 'DrawText' (string expected, got nil) 1. DrawText - [C]:-1 2. fn - addons/darkrpmodification-master/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:63 3. unknown - addons/ulib/lua/ulib/shared/hook.lua:110 [/QUOTE] [QUOTE]// NAME local name = ply:getDarkRPVar("rpname") surface.SetTextColor( white ) surface.SetTextPos( x + smallgap, infoy ) surface.DrawText( name ) -- ERROR IS HERE SAYS IM NOT SUPPOSED TO DO THIS x = x + surface.GetTextSize( name ) + gap[/QUOTE]
[QUOTE=xander1998;51479595]and why am I getting an error like this?[/QUOTE] Because your DarkRPVar name make no sense? Why do you have it as a DRPvar? You don't need to. Especially on the client. ply:GetNick() or GetName() or whatvever. *Edit Also your calculations for getting the size should come before you draw/set it.
Ah I see what you were saying there. Can you tell me what "attempt to concatenate local 'job'" means? was I not supposed to use DRPVar?
For job you can use team.GetName(ply:Team())
Where are you finding these or are you just coming up with them?
The Garry's Mod Wiki: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/team/GetName]team.GetName[/url]
WoW. I am making this a lot harder than it needs to be lol
[QUOTE=xander1998;51480967]WoW. I am making this a lot harder than it needs to be lol[/QUOTE] You'll find that everything you do as a new coder is exactly that. Don't worry though; as you learn the API you'll know these things by heart like us. :)
[B]Removed[/B]
Sorry, you need to Log In to post a reply to this thread.