Odd HUD issue

Hello! I have a weird bug with a HUD i’m trying to fix. This is for the hitman variant of TTT. Where traitor have a target and must kill the target etc.

I (With the help of some friends) fixed the HUD to adjust size depending on the size of the target’s name. Here is the code for the HUD addition.

First this is what my name looks like when I am the target of a traitor: http://steamcommunity.com/sharedfiles/filedetails/?id=733158930

This is what one of my friend’s name looks like: http://steamcommunity.com/sharedfiles/filedetails/?id=733787402

However my name is bigger than his: http://steamcommunity.com/sharedfiles/filedetails/?id=733787604

My question is why is his name make the Target HUD insanely long. Here is the code:



    --for painting
    local x = 270
    local y = ScrH() - 130

    local w = 250
    local h = 120

    usermessage.Hook( "hitman_newtarget", function(um) hitman_targetname = um:ReadString() end)
    usermessage.Hook( "hitman_notarget", function(um) hitman_targetname = nil end)

    local function DisplayHitlistHUD()
        if hitman_targetname and LocalPlayer():Alive() and LocalPlayer():IsTraitor() then
            --basic box
            local len = string.len("KILL: " .. hitman_targetname)
            if len < 17 then -- 17 is probably a good number
                w = 250
            else
                w = 250 * len * 0.06 -- adjust the number until it will work fine
            end
            draw.RoundedBox(8, x, y, w, h, Color(0, 0, 10, 200))
            draw.RoundedBox(8, x, y, w, 30, Color(200, 25, 25, 200))
 
            --Didn't mind using BadKings ShadowedText. For some reason stuff doesn't properly import. Got to clean up the bloody code at some point anyway.
            -- 26th June 2015: Still haven't, should get my lazy ass to do it some day
            -- 18th October 2015: lmao I'll never do this part properly, will I? Well doesn't matter really, atleast the rest of the code gets de-garbaged
 
            --Target announcer
            draw.SimpleText("KILL: " .. hitman_targetname, "TraitorState", x + 2 + w/2, y+2, Color(0, 0, 0, 255), TEXT_ALIGN_CENTER)
            draw.SimpleText("KILL: " .. hitman_targetname, "TraitorState", x + 0 + w/2, y, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER)
            --Stats
            draw.SimpleText("Total Targets Killed: " .. hitman_targetkills, "HealthAmmo", x + 12, y +42, Color(0, 0, 0, 255))
            draw.SimpleText("Total Targets Killed: " .. hitman_targetkills, "HealthAmmo", x + 10, y +40, Color(255, 255, 255, 255))
 
            draw.SimpleText("Total Civilians Killed: " .. hitman_civkills, "HealthAmmo", x + 12, y + 62, Color(0, 0, 0, 255))
            draw.SimpleText("Total Civilians Killed: " .. hitman_civkills, "HealthAmmo", x + 10, y + 60, Color(255, 255, 255, 255))
			
			draw.SimpleText("Available Freekills: " .. hitman_targetkills - hitman_civkills, "HealthAmmo", x + 12, y + 82, Color(0, 0, 0, 255))
			draw.SimpleText("Available Freekills: " .. hitman_targetkills - hitman_civkills, "HealthAmmo", x + 10, y + 80, Color(255, 255, 255, 255))
        end
    end
    hook.Add("HUDPaint", "DisplayHitlistHUD", DisplayHitlistHUD);
    --Fetch stats
    usermessage.Hook( "hitman_killed_targets", function(um) hitman_targetkills = um:ReadShort() end)
    usermessage.Hook( "hitman_killed_civs", function(um) hitman_civkills = um:ReadShort() end)


I suspect it is the symbols/foreign characters is the culprit, but I have know idea how to fix it.

[lua]local len = string.len("KILL: " … hitman_targetname)
if len < 17 then – 17 is probably a good number
w = 250
else
w = 250 * len * 0.06 – adjust the number until it will work fine
end[/lua]
There’s your culprit. The code simply checks the length of characters and sets an arbitrary length the creator thought would fit, so I assume your friend’s name has many compact characters.
To correct this, try actually getting the length of the text with

surface.GetTextSize
[lua]surface.SetFont( “TraitorState” )
local w = surface.GetTextSize( hitman_targetname )
– Give the text a cozy 15px margin if it’s too long
w = w > 220 and ( w +30 ) or 250[/lua]

So the modified code is
[lua]–for painting
local x = 270
local y = ScrH() - 130

local w = 250
local h = 120

usermessage.Hook( “hitman_newtarget”, function(um) hitman_targetname = um:ReadString() end)
usermessage.Hook( “hitman_notarget”, function(um) hitman_targetname = nil end)

local function DisplayHitlistHUD()
if hitman_targetname and LocalPlayer():Alive() and LocalPlayer():IsTraitor() then
–basic box
surface.SetFont( “TraitorState” )
local w = surface.GetTextSize( hitman_targetname )
– Give the text a cozy 15px margin if it’s too long
w = w > 220 and ( w +30 ) or 250

       draw.RoundedBox(8, x, y, w, h, Color(0, 0, 10, 200))
       draw.RoundedBox(8, x, y, w, 30, Color(200, 25, 25, 200))

       --Didn't mind using BadKings ShadowedText. For some reason stuff doesn't properly import. Got to clean up the bloody code at some point anyway.
       -- 26th June 2015: Still haven't, should get my lazy ass to do it some day
       -- 18th October 2015: lmao I'll never do this part properly, will I? Well doesn't matter really, atleast the rest of the code gets de-garbaged

       --Target announcer
       draw.SimpleText("KILL: " .. hitman_targetname, "TraitorState", x + 2 + w/2, y+2, Color(0, 0, 0, 255), TEXT_ALIGN_CENTER)
       draw.SimpleText("KILL: " .. hitman_targetname, "TraitorState", x + 0 + w/2, y, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER)
       --Stats
       draw.SimpleText("Total Targets Killed: " .. hitman_targetkills, "HealthAmmo", x + 12, y +42, Color(0, 0, 0, 255))
       draw.SimpleText("Total Targets Killed: " .. hitman_targetkills, "HealthAmmo", x + 10, y +40, Color(255, 255, 255, 255))

       draw.SimpleText("Total Civilians Killed: " .. hitman_civkills, "HealthAmmo", x + 12, y + 62, Color(0, 0, 0, 255))
       draw.SimpleText("Total Civilians Killed: " .. hitman_civkills, "HealthAmmo", x + 10, y + 60, Color(255, 255, 255, 255))
 
 draw.SimpleText("Available Freekills: " .. hitman_targetkills - hitman_civkills, "HealthAmmo", x + 12, y + 82, Color(0, 0, 0, 255))
 draw.SimpleText("Available Freekills: " .. hitman_targetkills - hitman_civkills, "HealthAmmo", x + 10, y + 80, Color(255, 255, 255, 255))
   end

end
hook.Add(“HUDPaint”, “DisplayHitlistHUD”, DisplayHitlistHUD);
–Fetch stats
usermessage.Hook( “hitman_killed_targets”, function(um) hitman_targetkills = um:ReadShort() end)
usermessage.Hook( “hitman_killed_civs”, function(um) hitman_civkills = um:ReadShort() end)
[/lua]

Thank you so much, I’ll be able to test it soon! Also would you by chance know how to hide the hitlistHUD when traitors are using a scope? In previous testing the hitlistHUD would obstruct the bottom left coner of the scope when the name was really long.

EDIT: IT WORKED THANK YOU SO MUCH! On a side note: How can I hide the HUD when a traitor uses a scope? I.E. Sniper scope