Ammo HUD Calculation Error

Hey, so I am making a HUD, and I’m having trouble getting the calculations correct for it always fill the bar 100% at full ammo.

The HUD takes the current ammo in the gun, and does a calculation so it can display the ammo on the HUD. However, where the max ammo for each gun changes, the calculation will be wrong, so the ammo will either go over the bar, or not fill it enough.

Does anyone know how I can fix this?

Here’s the code:




if Ammo_Primary >=1 then
	draw.RoundedBox( 4, 50, 860, 200, 40, Color( 40, 40, 40, 180 )) //Roundness, x, y, width of bar, height, RGBA
	draw.RoundedBox( 4, 50, 860, math.Clamp( Ammo_Primary, 0, 20 )*10, 40, Color( 108, 108, 255, 255 )) //Roundness, x, y, width of bar, height, RGBA. math.Clamp (var, min number, max number)

	surface.SetTextColor(255, 255, 255, 255) //RGB, Transparency
	surface.SetTextPos(60, 860) //Width x Height
	surface.SetFont("CustomFont")
	surface.DrawText("Ammo: ")
	surface.DrawText(tostring(Ammo_Primary))
	end



Any help is appreciated.

Use wep.Primary.ClipSize and such variables from SWEPs in your calcuation. Note, that those variables do not exist for default HL2 weapons.

I used this for my HUD:

surface.DrawRect( x, y, ( w / wep.Primary.ClipSize ) * wep:Clip1(), h )

Hi, thanks for the help so far, now I’m having a really weird bug.

The bar is now calculating correctly, however, it is getting a color value from somewhere.

The color is matching the hp color, and if I change the HP color, it changes the ammo color.

If I pick up armor, then it changes to the color of the armor, and when you run out of armor, it goes back to the color of the HP.

Am I missing something stupid here?




local HP = LocalPlayer():Health()
local Ammo_Primary = LocalPlayer():GetActiveWeapon():Clip1()
local ARM = LocalPlayer():Armor()
wep = LocalPlayer():GetActiveWeapon()

	draw.RoundedBox( 4, 50, 1000, 200, 40, Color( 40, 40, 40, 180 )) //Roundness, x, y, width of bar, height, RGBA
	draw.RoundedBox( 4, 50, 1000, math.Clamp( HP, 0, 200)*2, 40, Color( 220, 108, 108, 255 )) //Roundness, x, y, width of bar, height, RGBA. math.Clamp (var, min number, max number)

	surface.SetTextColor(255, 255, 255, 255) //RGB, Transparency
	surface.SetTextPos(60, 1000)
	surface.SetFont("CustomFont")
	surface.DrawText("Health")

	if ARM >=1 then
	draw.RoundedBox( 4, 50, 930, 200, 40, Color( 40, 40, 40, 180 )) //Roundness, x, y, width of bar, height, RGBA
	draw.RoundedBox( 4, 50, 930, math.Clamp(ARM, 0, 20 )*2, 40, Color( 108, 108, 255, 255 )) //Roundness, x, y, width of bar, height, RGBA. math.Clamp (var, min number, max number)

	surface.SetTextColor(255, 255, 255, 255) //RGB, Transparency
	surface.SetTextPos(60, 930) //Width x Height
	surface.SetFont("CustomFont")
	surface.DrawText("Armor ")
	end


	if Ammo_Primary >=1 then
	//draw.RoundedBox( 4, 50, 860, math.Clamp( wep:Clip1(), 0, 20 )*2, 40, Color( 108, 108, 255, 255 )) //Roundness, x, y, width of bar, height, RGBA. math.Clamp (var, min number, max number)
	//draw.RoundedBox( 4, 50, 860, 200, 40, Color( 40, 40, 40, 180 )) //Roundness, x, y, width of bar, height, RGBA
	surface.DrawRect( 50, 860, ( 200 / wep.Primary.ClipSize ) * wep:Clip1(), 40) 

	surface.SetTextColor(255, 255, 255, 255) //RGB, Transparency
	surface.SetTextPos(60, 860) //Width x Height
	surface.SetFont("CustomFont")
	surface.DrawText("Ammo: ")
	surface.DrawText(tostring(Ammo_Primary))
	end



surface.DrawSetColor()

Or you could use draw.RoundedBox:

draw.RoundedBox( 4, 50, 860, ( 200 / wep.Primary.ClipSize ) * wep:Clip1(), 40, Color( r, g, b ) )

Thanks!

I did try to convert it into a draw.RoundedBox, however, I was stupid and forgot to put the roundness value in…

Thanks to everyone who helped!