Alright so, I've been working on custom huds for something i'm working on, the last one I need to make is the custom ammo hud, and I can't seem to find any documentation on how it's done, or anything that is entirely helpful, sofar I threw this together, but how would I actually have the ammo draw on the hud, how would I have it display correctly? Help would be appreciated.
[CODE]
function ammo()
local client = LocalPlayer()
if !client:GetActiveWeapon():IsValid() then return end
local mag_current = client:GetActiveWeapon():Clip1() // How much ammunition you have inside the current magazine
local mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) // How much ammunition you have outside the current magazine
draw.AAText("Ammo: ", "Deathrun_SmoothBig", ScrW() - 165, ScrH() - 50, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
draw.AAText(client:GetActiveWeapon():Clip1() .. "", "Deathrun_SmoothBig", ScrW() - 105, ScrH() - 48, Color(255, 255, 255, 255), 0, 0 )
draw.AAText("/ ", "Deathrun_SmoothBig", ScrW() - 60, ScrH() - 50, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
draw.AAText(client:GetActiveWeapon():GetPrimaryAmmoType() .. "", "Deathrun_SmoothBig", ScrW() - 60, ScrH() - 48, Color(255, 255, 255, 255), 0, 0 )
end
[/CODE]
Alright, so I modified it to this, i'm trying to get it to not show if you don't have a gun/ammo and only show if you have a weapon/ammo With the line that is commented out, when it isn't it gives an error saying,
[ERROR] addons/ammo_ui/lua/autorun/ammo.lua:12: attempt to call method 'Clip1' (a nil value)
1. fn - addons/ammo_ui/lua/autorun/ammo.lua:12
2. unknown - addons/ulib/lua/ulib/shared/hook.lua:183
I'm a bit of a beginner with this stuff and help/pointing me in the right direction would be greatly appreciated.
EDIT: Changed it up again, It shows numbers now without error, but the problem is it shows -1 if the person doesn't have a proper weapon out, such as a crowbar.
Try adding some IsValid checks.
[CODE]
if !client:GetActiveWeapon():IsValid() then return end
[/CODE]
Something I noticed, is when it does work, it shows the value of the ammo to -1, I was wondering how I would make it not show the entire UI if the player isn't holding/equipping a weapon.
Not sure if this will work or not but you can try.
[lua]
function ammo()
local client = LocalPlayer()
if !client:GetActiveWeapon():IsValid() then return end
if client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) ~= -1 then
local mag_current = client:GetActiveWeapon():Clip1() // How much ammunition you have inside the current magazine
local mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) // How much ammunition you have outside the current magazine
draw.AAText(client:GetActiveWeapon():Clip1() .. "", "Deathrun_SmoothBig", ScrW() - 99, ScrH() - 30, Color(255, 255, 255, 255), 0, 0 )
draw.AAText("Ammo: ", "Deathrun_SmoothBig", ScrW() - 200, ScrH() - 50, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end
end
[/lua]
[CODE]
function ammo()
local client = LocalPlayer()
if !client:GetActiveWeapon():IsValid() then return end
if client:GetActiveWeapon():Clip1() ~= -1 then
local mag_current = client:GetActiveWeapon():Clip1() // How much ammunition you have inside the current magazine
local mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) // How much ammunition you have outside the current magazine
draw.SimpleText("Ammo: ", "default", ScrW() - 165, ScrH() - 50, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
draw.SimpleText(client:GetActiveWeapon():Clip1() .. "", "default", ScrW() - 105, ScrH() - 48, Color(255, 255, 255, 255), 0, 0 )
draw.SimpleText("/ ", "default", ScrW() - 60, ScrH() - 50, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
draw.SimpleText(client:GetActiveWeapon():GetPrimaryAmmoType() .. "", "default", ScrW() - 55, ScrH() - 50, Color(255, 255, 255, 255), 0, 0 )
end
end
hook.Add("HUDPaint", "ammo", ammo)
[/CODE]
This is what it currently is, the ammo hud only shows if you're actually holding a gun, but the problem I have seem to run into is that the PrimaryAmmoType, lets say I have.. 0 ammo, it shows a 4 for me. Help would be appreciated.
[editline]1st June 2014[/editline]
I seemed to have fixed it with changing somethong that has been stupidity on my party, help has been appreciated.. thanks :)
May I ask exactly how it was that you fixed it? For future reference
May i notify you that this post is 4 years old?
But the fix is using a IsValid
for the active weapon. Draw everything weapon related inside that if statement.
Yeah I know it's old, i just needed to know buddy sorry
Create a new thread in the future, do not bump old ones.
Sorry, you need to Log In to post a reply to this thread.