• I need help with a lua HUD
    44 replies, posted
I am starting to learn HUD development. Im making it for darkrp and each job has different lengths in the words EX: "Civil Protection" - "Mayor" How would I make my hud make room if the words overlap? [url]http://pasteboard.co/3PMVK5Apd.png[/url]
You could go about it two ways I am sure of, one would be to scale the text down but I feel as though this wouldn't look good. Or another would be to just to clamp the width of the text to a number making it cut the rest of the text off.
I am using surface.GetTextSize("Some text") it will return the width and height of the text. Really good function. If you want to optimize the text position. This is my idea. [URL="https://wiki.garrysmod.com/page/surface/GetTextSize"]https://wiki.garrysmod.com/page/surface/GetTextSize[/URL] But only if you used surface.SetFont
Basically, set the x position of each one to the x position of the previous one plus the width of the previous one plus a gap. Example: [lua] local gap = 10 local y = 100 local x = 0 --assume a,b,c are labels. a:SetPos(x,y) a:SetText("Your text") a:SizeToContents() x = x + a:GetWide() + gap --Offset the next label. b:SetPos(x,y) b:SetText("Your second text") b:SizeToContents() x = x + b:GetWide() + gap --Offset the next label. c:SetPos(x,y) c:SetText("Text3") c:SizeToContents() [/lua] If you're using HUDPaint instead of derma, use surface.GetTextSize() instead of a:GetWide() or b:GetWide().
I don't really understand how to use surface.GetTextSize().. What do I do once I get the size? ALSO I get this error when I try to set money. [ERROR] gamemodes/darkrp/gamemode/modules/hud/cl_hudreplacement.lua:72: attempt to concatenate local 'money' (a nil value) 1. v - gamemodes/darkrp/gamemode/modules/hud/cl_hudreplacement.lua:72 2. unknown - lua/includes/modules/hook.lua:84
Are you using the localplayer:getDarkRPVar("money")) where money is placed?
local money = LocalPlayer():getDarkRPVar("money") draw.SimpleText("$".. money, "Font", 400 + gap, ScrH() - 1065, Color(0, 153, 0, 255), 0, 1 )
It's also mentioned on the DarkRP wiki that you should use the DarkRP modification addon when doing things such as this. You can go around with the way your doing - but it can help a lot doing modifications via the addon.
I have the hud under the modules for darkrpmodification..?
[QUOTE=xander1998;51453334]I don't really understand how to use surface.GetTextSize().. What do I do once I get the size?[/QUOTE] It gives you the width and height of the text. Using this, you can offset your NEXT text by that amount. So my example used this for DLabels: [lua] x = x + a:GetWide() + gap [/lua] Instead, for surface and draw functions, you would do: [lua] surface.SetFont("yourfont") x = x + surface.GetTextWidth("yourtext") + gap [/lua]
Will this work for like the job name because job names have different sizes and if I offset the size wont it just overlap again?
[QUOTE=xander1998;51453436]Will this work for like the job name because job names have different sizes and if I offset the size wont it just overlap again?[/QUOTE] If you do this for each label following the first, then each label will subsequently be offset by the previous label's width. Like this: [t]http://i.imgur.com/rQp4hvc.png[/t]
Im sorry, Im VERY new to lua lol. Do I need to make multiple local x =?
[QUOTE=xander1998;51454083][lua] local gap = 10 local y = 100 local x = 0 --assume a,b,c are labels. a:SetPos(x,y) a:SetText("Your text") a:SizeToContents() x = x + a:GetWide() + gap --Offset the next label. b:SetPos(x,y) b:SetText("Your second text") b:SizeToContents() x = x + b:GetWide() + gap --Offset the next label. c:SetPos(x,y) c:SetText("Text3") c:SizeToContents() [/lua][/QUOTE] no
[QUOTE] local gap = 100 local smallgap = 65 local y = 100 local x = 0 // NAME local name = LocalPlayer():getDarkRPVar("rpame") draw.SimpleText(name, "Font", 10, ScrH() - 1065, Color(255, 255, 255, 185), 0, 1) x = x + surface.GetTextSize("") + gap // HEALTH local health = LocalPlayer():Health() draw.SimpleText("Health -", "Font",10 + gap, ScrH() - 1065,Color(255,0,0),1, 1) draw.SimpleText(health.."%", "Font", 10 + gap + smallgap, ScrH() - 1065, Color(255, 0, 0), 1, 1)[/QUOTE] Gosh why am I not getting this....
Because you stil not using what they adviced. You must set the font too and get the text size. Also surface.GetTextSize("") return 2 value wide and height. You must use them perfectly. Also I see you not using any text inside the get function. Why? You wont get the size if there is no text. Try set the font and get the text size then before draw text. With the text you want. [URL="https://wiki.garrysmod.com/page/surface/GetTextSize"]https://wiki.garrysmod.com/page/surface/GetTextSize[/URL] It has examples. [LUA] local message = "Hello World" surface.SetFont( "Trebuchet24" ) local width = select( 1, surface.GetTextSize( message ) ) [/LUA] with that you only get the width.
[QUOTE=xander1998;51454117]Gosh why am I not getting this....[/QUOTE] [lua] local gap = 100 local y = 100 local x = 10 // NAME local name = LocalPlayer():getDarkRPVar("rpame") draw.SimpleText(name, "Font", x, ScrH() - 1065, Color(255, 255, 255, 185), TEXT_ALIGN_LEFT, 1) surface.SetFont("Font") x = x + surface.GetTextSize(name) + gap // HEALTH local health = LocalPlayer():Health() local txt = "Health - " .. health .. "%" draw.SimpleText(txt, "Font", x, ScrH() - 1065,Color(255,0,0),1, 1) surface.SetFont("Font") x = x + surface.GetTextSize(txt) + gap [/lua] Try that.
[QUOTE=bobbleheadbob;51455592][lua] local gap = 100 local y = 100 local x = 10 // NAME local name = LocalPlayer():getDarkRPVar("rpame") draw.SimpleText(name, "Font", x, ScrH() - 1065, Color(255, 255, 255, 185), TEXT_ALIGN_LEFT, 1) surface.SetFont("Font") x = x + surface.GetTextSize(name) + gap // HEALTH local health = LocalPlayer():Health() local txt = "Health - " .. health .. "%" draw.SimpleText(txt, "Font", x, ScrH() - 1065,Color(255,0,0),1, 1) surface.SetFont("Font") x = x + surface.GetTextSize(txt) + gap [/lua] Try that.[/QUOTE] I don't think this is because a lack of lua knowledge for him. It's just the lack of math. He has to look at it as a math problem, which he isn't. If you are reading this. Get grid paper bro. Make functions that dynamically resize for the jobs (like the code above) Btw, a quick tip, some job names might be so long (if someone else wants to use your code) that you might want to make a limit on it. I usually do it until 20-30 characters until it just goes "...". Unless you want to go super into it where it goes to a second line for the job name. Or anything really. Would require you to have a bit of knowledge with string manipulation. Just something to keep in mind moving forward. You don't really have to do this if you know all your job names will be short and sweet.
Thanks for the support but i have one more issue when I use the scroll wheel it duplicates the hud. I have fixed the spacing issue. [URL="Image"]http://pasteboard.co/4L6Waty8D.jpg[/URL]
That's weird as fuck. What are you drawing the HUD in? Show us some code.
[B]Removed[/B]
[QUOTE=xander1998;51460249][/QUOTE] Maybe instead of using "HUDShouldDraw" you can use this [B][U][URL="https://wiki.garrysmod.com/page/GM/HUDPaint"]GM:HUDPaint()[/URL][/U][/B]
[QUOTE=FlyPiggyBanks;51460313]Maybe instead of using "HUDShouldDraw" you can use this [B][U][URL="https://wiki.garrysmod.com/page/GM/HUDPaint"]GM:HUDPaint()[/URL][/U][/B][/QUOTE] Absolutely. HUDShouldDraw isn't for drawing HUD, it's for turning off HL2 HUD elements, such as the default ammo and health for example.
Wow. I have learned so much in these past few days. I really appreciate you guys helping me out.
Now the last thing I need to learn about huds is how to make multiple Resolutions work. I know I have to use something like ScrW(), ScrH(). But the way I have it it wont work since I am using ScrH() - 1065.
[QUOTE=xander1998;51461842]Now the last thing I need to learn about huds is how to make multiple Resolutions work. I know I have to use something like ScrW(), ScrH(). But the way I have it it wont work since I am using ScrH() - 1065.[/QUOTE] ScrH/W() / 2 (or whatever) use multiplication/divison and not plus/minus.
I can see what your saying but if I want the hud to be on the top of the screen I would have to do?
[QUOTE=xander1998;51461940]I can see what your saying but if I want the hud to be on the top of the screen I would have to do?[/QUOTE] Just make it 0?
I just divided by a high amount (30). Do you know where any tutorials are on how to make my own agenda / Above Player head HUD?
If you're on about text floating above a players head in-game, then you'll need to use cam3D2D.
Sorry, you need to Log In to post a reply to this thread.