I'm having problem calculating the width of a bar depending on the amount of ammo available in Clip1() (xx/xxx clip)
This is solely because the bar is 118px, and you can't really subtract a clip of 45 by 118 (px)
anyway, does anyone know a solution/proper method to go about this? does it have something to do with the percentage of the clip or what? feedback would be greatly appreciated
I attemped:
[QUOTE]// intializing vars
local mag_left = client:GetActiveWeapon():Clip1() -- current clip
local mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) -- max clip
local ammoDiff = ( mag_left / mag_extra ) * 118[/QUOTE]
and then setting ammoDiff as the width var, but to no avail (by no avail it'll just cut off a chunk of your mag_left from the bar, this trick only works with static maximums (ie. mag_extra) )
any thoughts? I'm sure it has something to do with math more than anything - I'm falling towards interpreting the max clip as a percentage since it varies per weapon
[highlight](User was banned for this post ("Wrong forum" - Grea$eMonkey))[/highlight]
Set the bar's width to:
width * (currentValue / maxValue)
So if you want your bar to be a maximum of 118px, and reflect the number of bullets in a magazine:
width = 118 * (currentBullets / maxBullets)
A better example would be a health bar, max width of 200px:
width = 200 * (pl:Health() / pl.MaxHealth)
I did that but the problem is that each mag (89 in the picture) will take up half the bar on default instead of what it ought to be which is full (45-89). If I do something like width = 118 - currentMag it won't work out properly since currentMag isn't relative (ie. the maxmag would have to be the same as the currentMag and then subtract) to 118px. But yeah, I was aware of that trick
[IMG]http://skiddies.info/magupdate.png[/IMG]
I'll be honest, I don't even understand what you just said :v:
Can you post the code you're using now?
Because I tested the code I gave you and it works fine.
yeah, sorry it doesn't really make sense when I explain it but essentially:
[CODE]function cubHUD:drawAMMO()
local client = LocalPlayer()
if ( client:GetActiveWeapon() != NULL ) then // make sure we're equipping a weapon
local mag_left = client:GetActiveWeapon():Clip1()
local mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType())
local magWidth = (mag_left/ mag_extra) * 118
if mag_left > -1 then
cubHUD:drawBORDER( 4, 39, ScrH() - 67, 130, 30, colors.border )
cubHUD:drawBG( 4, 40, ScrH() - 66, 128, 28, colors.bg )
cubHUD:drawBORDER( 4, 44, ScrH() - 62, 120, 20, colors.border )
cubHUD:drawROUND( 4, 45, ScrH() - 61, 118, 18, colors.background )
if mag_left <= -1 then
cubHUD:drawROUND( 4, 45, ScrH() - 61, 118, 18, Color ( 255, 255, 255, 0 ) )
else
cubHUD:drawROUND( 4, 45, ScrH() - 61, magWidth, 18, colors.backgroundYellow )
end
cubHUD:drawSHADE( 4, 45, ScrH() - 61, 118, 8, colors.shade )
cubHUD:drawTEXT( "Ammo " .. mag_left .. "/" .. mag_extra, 70, ScrH() - 58, colors.text, "UiBold" )
end
end
end[/CODE]
this'll occur as well: [IMG]http://skiddies.info/magupdate2.png[/IMG] (mag_left > mag_extra)
So you're trying to make the bar's length represent the ratio of ammo in your magazine vs the total amount of bullets you possess?
[QUOTE=Kopimi;32705878]So you're trying to make the bar's length represent the ratio of ammo in your magazine vs the total amount of bullets you possess?[/QUOTE]
yes
[QUOTE=randomGuest;32706029]yes[/QUOTE]
That can't be represented with a bar.
If you want to have a bar showing a percentage, you need minimum and a maximum values to compare and get a percentage out of.
If you want to use a bar, you should be showing something like the current number of bullets you have vs the maximum amount of bullets you can carry, representing arbitrary values like these isn't a proper use of a bar.
sure it can, I mean represent it with width changes not percentages
but I've seen it done before and if so what would be the best way to represent a bar?
edit: i'll just try comparing minimum and maximum values to get a percentage out of with a width of 118px
[QUOTE=randomGuest;32706193]sure it can, I mean represent it with width changes not percentages
but I've seen it done before and if so what would be the best way to represent a bar?[/QUOTE]
You're really going to have to sketch this out because I'm not understanding what you're looking for :v:
you said that I was misusing bar somehow, but all I was trying to do was set the width relative to the clip size etc
anyway, basically my aim is just to represent the clip depending on length in a bar but I'll try to use percentages since width varies on px size etc
You aren't "misusing" bars, you're just trying to represent something that can't be represented.
You want the bar's width to reflect how many bullets you have left in your magazine vs the total number of bullets you have..
My head hurts, good luck :v:
[QUOTE=Kopimi;32706323]You aren't "misusing" bars, you're just trying to represent something that can't be represented.
You want the bar's width to reflect how many bullets you have left in your magazine vs the total number of bullets you have..
My head hurts, good luck :v:[/QUOTE]
no, only the current magazine
[QUOTE=randomGuest;32705802]yeah, sorry it doesn't really make sense when I explain it but essentially:
[CODE]function cubHUD:drawAMMO()
local client = LocalPlayer()
if ( client:GetActiveWeapon() != NULL ) then // make sure we're equipping a weapon
local mag_left = client:GetActiveWeapon():Clip1()
local mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType())
local magWidth = (mag_left/ mag_extra) * 118
if mag_left > -1 then
cubHUD:drawBORDER( 4, 39, ScrH() - 67, 130, 30, colors.border )
cubHUD:drawBG( 4, 40, ScrH() - 66, 128, 28, colors.bg )
cubHUD:drawBORDER( 4, 44, ScrH() - 62, 120, 20, colors.border )
cubHUD:drawROUND( 4, 45, ScrH() - 61, 118, 18, colors.background )
if mag_left <= -1 then
cubHUD:drawROUND( 4, 45, ScrH() - 61, 118, 18, Color ( 255, 255, 255, 0 ) )
else
cubHUD:drawROUND( 4, 45, ScrH() - 61, magWidth, 18, colors.backgroundYellow )
end
cubHUD:drawSHADE( 4, 45, ScrH() - 61, 118, 8, colors.shade )
cubHUD:drawTEXT( "Ammo " .. mag_left .. "/" .. mag_extra, 70, ScrH() - 58, colors.text, "UiBold" )
end
end
end[/CODE]
this'll occur as well: [IMG]http://skiddies.info/magupdate2.png[/IMG] (mag_left > mag_extra)[/QUOTE]
local magWidth = math.Clamp( ( mag_left / mag_extra ) * 118, 0, 118 )
maxBarWidth * ( clipLeft / clipSize ) clip size would be 45 in your pictures case..
Use math.Clamp if you don't want it to go over a certain limit, aka your maxBarWidth.
[QUOTE=Gfoose;32710860]local magWidth = math.Clamp( ( mag_left / mag_extra ) * 118, 0, 118 )[/QUOTE]
If a percentage ratio goes over 100% and it's not supposed to obviously the math is wrong.
Your problem is, is that GetPrimaryAmmoType() returns the total amount of ammo left and not the clip size. Usually the clip size is stored on the weapons table, but that is not the case with the HL2 weapons. The Clip size is either ClipSize or DefualtClip(Can't remember I think it's DefualtClip) like this on the table, Wep.Primary.ClipSize. But that won't work for the HL2 weapons. So I suggest this.
[LUA]
function cubHUD:drawAMMO()
local client = LocalPlayer()
local wep = client:GetActiveWeapon()
if ( IsValid(wep) ) then // make sure we're equipping a weapon
if(!wep.Primary.DefaultClip)then
if(wep:Clip1()!=-1)then
wep.Primary.DefaultClip = wep:Clip1()
else
wep.Primary.DefaultClip = 0
end
end
local bullets = wep:Clip1()
local mag_size = wep.Primary.DefaultClip
local magWidth = (bullets / mag_size) * 118
if bullets > -1 then
cubHUD:drawBORDER( 4, 39, ScrH() - 67, 130, 30, colors.border )
cubHUD:drawBG( 4, 40, ScrH() - 66, 128, 28, colors.bg )
cubHUD:drawBORDER( 4, 44, ScrH() - 62, 120, 20, colors.border )
cubHUD:drawROUND( 4, 45, ScrH() - 61, 118, 18, colors.background )
if bullets <= -1 then
cubHUD:drawROUND( 4, 45, ScrH() - 61, 118, 18, Color ( 255, 255, 255, 0 ) )
else
cubHUD:drawROUND( 4, 45, ScrH() - 61, magWidth, 18, colors.backgroundYellow )
end
cubHUD:drawSHADE( 4, 45, ScrH() - 61, 118, 8, colors.shade )
cubHUD:drawTEXT( "Ammo " .. bullets .. "/" .. mag_size, 70, ScrH() - 58, colors.text, "UiBold" )
end
end
end
[/LUA]
Let me see if I understand what you're trying to do here. You've got a bar that depletes to zero as you use the ammo in your magazine. Intuitive enough. But the size of that bar itself gets larger and more urgent as your reserve ammo depletes. Interesting idea, but I'd have to see it in action. Hence the graph, from a 10-round magazine with 30 extra bullets.
[img]http://i.imgur.com/Ffjr9.png[/img]
Sorry, you need to Log In to post a reply to this thread.