• Making Size Relevant to Text
    4 replies, posted
[code] local function Money() local armor = LocalPlayer():Armor() local money = DarkRP.formatMoney(localplayer:getDarkRPVar("money")) if armor > 0 then surface.DrawOutlinedRect(string.len(tostring("Armor: " .. armor)) * 6 + 4 - 1 + 65, 0, string.len(tostring("Money: " .. money)) * 6 + 6, 20) draw.DrawText("Money: " .. money, BarHUDFont, string.len(tostring("Armor: " .. armor)) * 6 + 4 - 1 + 65 + 5, 3, Color(255, 255, 255), TEXT_ALIGN_LEFT) else surface.DrawOutlinedRect(65, 0, string.len(tostring("Money: " .. money)) * 6 + 6, 20) draw.DrawText("Money: " .. money, BarHUDFont, 70, 3, Color(255, 255, 255), TEXT_ALIGN_LEFT) end end [/code] So, as you can see, the sizes/positions of the surface.DrawOutlinedRect's and draw.DrawText's are really weird. They're based off of the texts I need, however, the method in which I did this is coded in such a way that is very confusing. I'll probably forget what all of these numbers are if I don't add a comment. So is there any way to do what I've done here, just better? Thanks for any help! PS - There's another check I have based on "weird" methods. That's the "armor > 0" check. Check the code out: [code] local function Armor() local armor = LocalPlayer():Armor() if armor > 0 then surface.DrawOutlinedRect(65, 0, string.len(tostring("Armor: " .. armor)) * 6 + 4, 20) draw.DrawText("Armor: " .. armor, BarHUDFont, 70, 3, Color(255, 255, 255), TEXT_ALIGN_LEFT) end end local function Money() local armor = LocalPlayer():Armor() local money = DarkRP.formatMoney(localplayer:getDarkRPVar("money")) if armor > 0 then surface.DrawOutlinedRect(string.len(tostring("Armor: " .. armor)) * 6 + 4 - 1 + 65, 0, string.len(tostring("Money: " .. money)) * 6 + 6, 20) draw.DrawText("Money: " .. money, BarHUDFont, string.len(tostring("Armor: " .. armor)) * 6 + 4 - 1 + 65 + 5, 3, Color(255, 255, 255), TEXT_ALIGN_LEFT) else surface.DrawOutlinedRect(65, 0, string.len(tostring("Money: " .. money)) * 6 + 6, 20) draw.DrawText("Money: " .. money, BarHUDFont, 70, 3, Color(255, 255, 255), TEXT_ALIGN_LEFT) end end [/code] Basically, the armor part of the HUD is hidden when there's no armor. With this being said, the money portion will be placed awkwardly when armor's portion is hidden. So, I compensated for the armor missing with the if/else statement in the Money() function. Is this the best way to do this?
Take a look at this [URL="http://wiki.garrysmod.com/page/Object_Oriented_Lua"]object oriented lua[/URL] guide on the wiki, and if you are not familiar with object oriented programming then look at some different guides and explanations of it online. As for your text sizing issues... Take a look at this, [url]http://wiki.garrysmod.com/page/surface/GetTextSize[/url].
[QUOTE=MrRalgoman;52079118]Take a look at this [URL="http://wiki.garrysmod.com/page/Object_Oriented_Lua"]object oriented lua[/URL] guide on the wiki, and if you are not familiar with object oriented programming then look at some different guides and explanations of it online.[/QUOTE] What would this be useful for regarding this code? (Not trying to be rude, sorry if it comes off that way)
[QUOTE=Wavie;52079333]What would this be useful for regarding this code[/QUOTE] I have no clue why he linked that, your coding looks fine to me- although I think you made a mistake with one of the variables: [CODE] local money = DarkRP.formatMoney(localplayer:getDarkRPVar("money")) [/CODE] Should be [CODE] local money = DarkRP.formatMoney(LocalPlayer():getDarkRPVar("money")) [/CODE] (I think) [editline]9th April 2017[/editline] Also, instead of this: [CODE] draw.DrawText("Money: " .. money, BarHUDFont, 70, 3, Color(255, 255, 255), TEXT_ALIGN_LEFT) [/CODE] I think you meant to make BarHUDFont a string: [CODE] draw.DrawText("Money: " .. money, "BarHUDFont", 70, 3, Color(255, 255, 255), TEXT_ALIGN_LEFT) [/CODE]
[QUOTE=MPan1;52079407]I have no clue why he linked that, your coding looks fine to me- although I think you made a mistake with one of the variables: [CODE] local money = DarkRP.formatMoney(localplayer:getDarkRPVar("money")) [/CODE] Should be [CODE] local money = DarkRP.formatMoney(LocalPlayer():getDarkRPVar("money")) [/CODE] (I think) [editline]9th April 2017[/editline] Also, instead of this: [CODE] draw.DrawText("Money: " .. money, BarHUDFont, 70, 3, Color(255, 255, 255), TEXT_ALIGN_LEFT) [/CODE] I think you meant to make BarHUDFont a string: [CODE] draw.DrawText("Money: " .. money, "BarHUDFont", 70, 3, Color(255, 255, 255), TEXT_ALIGN_LEFT) [/CODE][/QUOTE] Thanks man!
Sorry, you need to Log In to post a reply to this thread.