HUD for different resolutions

When I change my screen resolution to a different number I can not see my HUD, I’ve come to the conclusion this is from the positions of the derma based on resolution size. How would I make it so the HUD will scale accordingly. The current code is working for 800x600 resolution.

[lua]

function MIAHud()

local client = LocalPlayer()
local Magazine = client:GetActiveWeapon():Clip1()
local Magazine2 = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType())
if !client:Alive() then return end
if(client:GetActiveWeapon() == NULL or client:GetActiveWeapon() == “Camera”) then return end

draw.RoundedBox(8, 0, ScrH() - 30, 200, 30, Color(50, 60, 50, 255))
draw.RoundedBox(8, 5, ScrH() - 25, 190, 20, Color(0, 240, 0, 255))
draw.SimpleText("Health: "..client:Health() .. "", "ScoreboardText", 70, ScrH() - 25, Color(0, 0, 0, 200), 0, 0)

draw.RoundedBox(8, 300, ScrH() - 30, 200, 30, Color(50, 60, 50, 255))
draw.RoundedBox(8, 305, ScrH() - 25, 190, 20, Color(0, 0, 240, 255))
if ( Magazine >= 0 ) then
	draw.SimpleText("Ammo: ".. Magazine .. "/".. Magazine2 .. "", "ScoreboardText", 365, ScrH() - 25, Color(0, 0, 0, 200), 0, 0)
end
if ( Magazine <= -1 ) then
	draw.SimpleText("Ammo: Unlimited", "ScoreboardText", 350, ScrH() - 25, Color(0, 0, 0, 200), 0, 0)
end

end
hook.Add(“HUDPaint”, “MIAHud”, MIAHud)
[/lua]

Calculate the width/height of the components, so you could make a box one fifth of the screen height and width for example.

ScrW() and ScrH() return the resolution’s width and height respectively. You can then multiply these to get the positions you see fit. For example, ScrW() * 0.5 would be 400 on an 800x600 resolution, or 512 on a 1024xwhatever resolution.

Going along Yobdren’s lines, if you wanted to make a panel always fill half the screen, you could so something like this:


draw.RoundedBox(0,0,0,ScrW()/2,ScrH()/2,color_white)

Well, I’m trying to make my HUD display in the bottom left of the screen on all resolution’s.

Okay, so do that.

Yea, I’ve been tinkering around with it and I seem to have it working, I’ll post if I need anymore help.

Here, I did the calculations for you and everything:
[lua]function MIAHud()

local client = LocalPlayer()
local Magazine = client:GetActiveWeapon():Clip1()
local Magazine2 = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType())
if !client:Alive() then return end
if(client:GetActiveWeapon() == NULL or client:GetActiveWeapon() == “Camera”) then return end

draw.RoundedBox(8, 0, ScrH()*0.95, ScrW()*0.25, ScrH()*0.05, Color(50, 60, 50, 255))
draw.RoundedBox(8, ScrW()*0.00625, ScrH()*0.96, ScrW()*0.2375, ScrH()*0.04, Color(0, 240, 0, 255))
draw.SimpleText("Health: "..client:Health() .. "", "ScoreboardText", ScrW()*0.0875, ScrH()*0.96, Color(0, 0, 0, 200), 0, 0)

draw.RoundedBox(8, ScrW()*0.375, ScrH()*0.95, ScrW()*0.25, ScrH()*0.05, Color(50, 60, 50, 255))
draw.RoundedBox(8, ScrW()*0.38125, ScrH()*0.96, ScrW()*0.2375, ScrH()*0.04, Color(0, 0, 240, 255))
if ( Magazine >= 0 ) then
	draw.SimpleText("Ammo: ".. Magazine .. "/".. Magazine2 .. "", "ScoreboardText", ScrW()*0.45625, ScrH()*0.96, Color(0, 0, 0, 200), 0, 0)
end
if ( Magazine <= -1 ) then
	draw.SimpleText("Ammo: Unlimited", "ScoreboardText", ScrW()*0.2375, ScrH()*0.96, Color(0, 0, 0, 200), 0, 0)
end

end
hook.Add(“HUDPaint”, “MIAHud”, MIAHud)[/lua]

Hook ‘MIAHud’ Failed: includes/modules/draw.lua:184: attempt to index local ‘color’ (a number value)

That error only happens with your calculations, which is odd because you didn’t change anything to do with color.

[lua]
color_darkgray = Color(50, 60, 50, 255) --Defining colors before hand in varaibles is a lot better than getting them every frame.
color_lightgreen = Color(0, 240, 0, 255)
color_lightblue = Color(0, 0, 240, 255)
color_lighttransblack = Color(0, 0, 0, 200)

function MIAHud()
local client = LocalPlayer()

if !client:Alive() || client:GetActiveWeapon() == NULL || client:GetActiveWeapon() == "Camera" then return end --This can all be in one statement.

local Magazine = client:GetActiveWeapon():Clip1() --You want to get these after you check if the weapon is NULL.
local Magazine2 = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType())

draw.RoundedBox(8, 0, ScrH() - 30, 200, 30, color_darkgray)
draw.RoundedBox(8, 5, ScrH() - 25, 190, 20, color_lightgreen)
draw.SimpleText("Health: "..client:Health().."", "ScoreboardText", 70, ScrH() - 25, color_lighttransblack, 0, 0)

draw.RoundedBox(8, 300, ScrH() - 30, 200, 30, color_darkgray)
draw.RoundedBox(8, 305, ScrH() - 25, 190, 20, color_lightblue)

if ( Magazine >= 0 ) then
	draw.SimpleText("Ammo: ".. Magazine .. "/".. Magazine2 .. "", "ScoreboardText", 365, ScrH() - 25, color_lighttransblack, 0, 0)
end
if ( Magazine <= -1 ) then
	draw.SimpleText("Ammo: Unlimited", "ScoreboardText", 350, ScrH() - 25, color_lighttransblack, 0, 0)
end

end
hook.Add(“HUDPaint”, “MIAHud”, MIAHud)
[/lua]

[editline]05:29PM[/editline]

My post wasn’t showing up for me for some reason, odd.

Fixed

Thank’s to both of you, I now have it working fine.

Yea but the statements are not, which is why you should use what I posted. If you use what he has it might start throwing errors if the weapon ever becomes NULL.

Yours isn’t compatible with multiple resolutions.

That is because he did not state whether or not he wanted the huds on top of each other, side by side, spaced or touching, ect. I fixed the code and left the positioning up to him now that he knows how.