• Help with making a HUD
    9 replies, posted
I'll start off with saying, I recently started to mess with lua, so I mainly been playing with others peoples codes(It helps to learn easier). Currently I have draw.RoundedBox and draw.SimpleText into action where droundbox changes colors according to the team, and the text changes according to the team name. and to where the bar increases to fit with the text name then some. Here's bits from it: [lua] local teamcolor= team.GetColor(LocalPlayer():Team()) draw.RoundedBox(8,posx + 225, posy, surface.GetTextSize(client.DarkRPVars.job)+40, 26, teamcolor) draw.SimpleText(client.DarkRPVars.job, "CustomFont", posx + 245, posy, teamcolor) [/lua] But what I'm trying to figure out now is how to make a specific sized box. Which changes colors according to the team color still, but stay the same size. All while the Text is centered and sized to fit in the box and any given text length, instead of the box changing its size for the text. Sorry if it's a simple fix/way to do it. I'm just a bit lost. Any help is appreciated.
You'd probably be better off using derma than draw functions. But, if you prefer this way, consider this. Anything you put in HUDPaint is updated every frame. This means that if you do draw.SimpleText(blahblahblah, getTeamColor(team), blah) that if the player's team changes, so too will the color. The same goes for the size. If you set the size to a variable, all you have to do is change that variable for the size/shape of the box to change with it.
EDIT-Okay, scratch everything I said so far. I am making it to complicated and to overdone for something simple. So what I need to know though is how can I go about using draw.simpletext to be centered with an draw.roundbox. Instead of it being like two overlays just lapping over each other.
[QUOTE=zynga1;40577158]EDIT-Okay, scratch everything I said so far. I am making it to complicated and to overdone for something simple. So what I need to know though is how can I go about using draw.simpletext to be centered with an draw.roundbox. Instead of it being like two overlays just lapping over each other.[/QUOTE] Things draw on top of each other. The first one you do is under everything you do after that. So draw.roundedbox first, then draw.simpletext. As for centering it in the box, You're going to want to do something like this: [LUA] local txt = WHATEVER_YOU_WANT_IT_TO_SAY surface.SetFont(your_font) local len = surface.GetTextSize(txt)--this finds the physical length of the text. draw.SimpleText(txt,your_font,(x+w/2)-len/2,y,Color(255,255,255)) [/LUA] Where x is the box's x, y is the box's y, and w is the box's width. That should put the text halfway across the box, minus one half the text width, equaling centered text.
Thanks, looks like that will work. I'll have to test it tomorrow, I'll post results.
So I ended up getting that part adjusted and working. But now I'm trying to allow the mainbackground of the hud to change colors, depending on the current event. [lua] function mainbackground() if GetConVarNumber("DarkRP_Lockdown") == 1 then draw.RoundedBox( 8, 660, 1035, surface.GetTextSize(client.DarkRPVars.job)+480, 45, Color( 204, 0, 0, 250 ) ) or if LocalPlayer().DarkRPVars.Arrested then draw.RoundedBox( 8, 660, 1035, surface.GetTextSize(client.DarkRPVars.job)+480, 45, Color( 0, 102, 204, 250 ) ) else draw.RoundedBox( 8, 660, 1035, surface.GetTextSize(client.DarkRPVars.job)+480, 45, Color( 51, 51, 51, 250 ) ) end end [/lua] Lockdown =red , Arrested = blue, neither = gray. It didn't work, and I'm unsure why. Once again I'm still learning, so I'm probably awful.
You have an 'or' in the wrong spot. I think it should be an elseif [lua]function mainbackground() if GetConVarNumber("DarkRP_Lockdown") == 1 then draw.RoundedBox( 8, 660, 1035, surface.GetTextSize(client.DarkRPVars.job)+480, 45, Color( 204, 0, 0, 250 ) ) elseif LocalPlayer().DarkRPVars.Arrested then draw.RoundedBox( 8, 660, 1035, surface.GetTextSize(client.DarkRPVars.job)+480, 45, Color( 0, 102, 204, 250 ) ) else draw.RoundedBox( 8, 660, 1035, surface.GetTextSize(client.DarkRPVars.job)+480, 45, Color( 51, 51, 51, 250 ) ) end end[/lua]
Alright that makes more sense, but after testing it out it still doesn't display the box at all. I even went ahead and made it check if the player was alive to display the last box, but that didn't even work. But if I remove everything except draw.roundedbox it works once more.
Have you actually hooked mainbackground() to HUDPaint or are you calling it in a HUDPaint hooked function ? Here's an example: [lua]hook.Add( "HUDPaint", "mainbackground", mainbackground )[/lua] or [lua]function HeadsUp() mainbackground() end hook.Add( "HUDPaint", "HeadsUp", HeadsUp )[/lua] Please correct me if I am wrong, I haven't played Garry's Mod for about 3 weeks now and I've been programming in other languages, so the syntax could be wrong.
[QUOTE=McDunkable;40592895]Have you actually hooked mainbackground() to HUDPaint or are you calling it in a HUDPaint hooked function ? Here's an example: [lua]hook.Add( "HUDPaint", "mainbackground", mainbackground )[/lua] or [lua]function HeadsUp() mainbackground() end hook.Add( "HUDPaint", "HeadsUp", HeadsUp )[/lua] Please correct me if I am wrong, I haven't played Garry's Mod for about 3 weeks now and I've been programming in other languages, so the syntax could be wrong.[/QUOTE] Aww Shucks, that's what I completely forgot. It works now, I feel stupid now though. Thanks for the help everyone, I appreciate it, I learned a few things. :smile:
Sorry, you need to Log In to post a reply to this thread.