GetPrimaryAmmoType() is no longer a thing?

So I’m making a custom HUD, and I’m trying to make an ammo display. My function



function GetAmmoForCurrentWeapon(ply)
    local wep = ply:GetActiveWeapon()
    if IsValid(wep) then return 0 end
    
    --print (ply:GetAmmoCount(wep:GetPrimaryAmmoType()))
    return ply:GetAmmoCount(wep:GetPrimaryAmmoType())
end


causes an error on the return line on wep:GetPrimaryAmmoType() saying that it’s a nil value. I checked the official wiki and even copied the line. In NP++ it shows up brown. Has it recently been removed? How do I get the primary ammo now?

Make sure wep is valid. do “!IsValid( wep )” instead of “not wep”.

Nope. Same result. I tried :Clip1() too but same result.

Post the exact error.




[ERROR] gamemodes/example/gamemode/cl_init.lua:157: attempt to call method 'GetPrimaryAmmoType' (a nil value)
  1. GetAmmoForCurrentWeapon - gamemodes/example/gamemode/cl_init.lua:157
   2. unknown - gamemodes/example/gamemode/cl_init.lua:88



EDIT: This is the same result for any ammo-getting function I find on the wiki. I’m using the default gmod weapons too, if this helps.
EDIT 2: Changed the error to match my code.

I don’t see Clip1 in your code. Post the code you are using and code where you call the function GetAmmoForCurrentWeapon.

Edited previous post.

Are you joking?

If you are not willing to cooperate, I will not be able to help you.

Oh sorry. I read the wrong thing. Here you go:



local ammo = GetAmmoForCurrentWeapon(ply)


is where it is called. Do you want the entire function?

[editline]4th June 2014[/editline]

ahem I found 1 mistake. If it is valid, return 0 end. I wanted if not. However as the code below is being run, it suggests the weapon is not valid.

What is ply?

[editline]4th June 2014[/editline]

Also, I updated the function on the wiki page you took it from.

Here is how you check to ensure everything is valid, that functions exist ( . ), etc…
[lua]hook.Add( “HUDPaint”, “MyWeaponTest”, function( )
local _p = LocalPlayer( );
if ( !IsValid( _p ) ) then return; end – Resolves error On Join…

local _w = _p:GetActiveWeapon( );
if ( !IsValid( _w ) ) then return; end -- Resolves error On Join and empty-handed...
if ( !_w.Slot || !_w.Clip1 || !_w.GetPrimaryAmmoType ) then return; end -- Resolves missing functions

print( _w:Clip1( ), _w:GetPrimaryAmmoType( ) );

if ( _w.Primary ) then
	print( _w.Primary.Ammo ); -- Ammo type. If this won't print use _w:GetTable( ).Primary.Ammo. The check would be _w.GetTable && _w:GetTable( ) && _w:GetTable( ).Primary && _w:GetTable( ).Primary.Ammo .... It would be wise to make helper vars.
end

end );[/lua]

ply is the player



local ply = LocalPlayer()


Also this has stopped the errors, but sets my HUD to 0. Here is the entire function:



function GM:HUDPaint()
    self.BaseClass:HUDPaint()
    
    local ply = LocalPlayer()
    --local plyTeam = ply:getTeam()
    
    local health = ply:Health()
    local ammo = GetAmmoForCurrentWeapon(ply)
    
    if health < 0 then health = 0 end
    
    healthTextColour = getTextColourDependingOnHealth(health)
    
    if !fontCreated then
        surface.CreateFont("healthFont", 
        {
        font = "Lucida Console",
        size = 100,
        weight = 1000,
        blursize = 0,
        scanlines = 0,
        antialias = true,
        underline = false,
        italic = false,
        strikeout = false,
        symbol = false,
        rotary = false,
        shadow = false,
        additive = false,
        outline = false,
        })
        
        surface.CreateFont("ammoFont", 
        {
        font = "Lucida Console",
        size = 70,
        weight = 1000,
        blursize = 0,
        scanlines = 0,
        antialias = true,
        underline = false,
        italic = false,
        strikeout = false,
        symbol = false,
        rotary = false,
        shadow = false,
        additive = false,
        outline = false,
        })
        
        fontCreated = true
    end
    
    --Health display
    surface.SetTextColor(healthTextColour[1], healthTextColour[2], healthTextColour[3], healthTextColour[4])
    surface.SetTextPos(20, ScrH()-100)
    surface.SetFont("healthFont")
    surface.DrawText(health)
    
    --Ammo display
    surface.SetTextColor(255, 217, 0, 200)
    surface.SetTextPos(ScrW()-150, ScrH()-75)
    surface.SetFont("ammoFont")
    surface.DrawText(ammo)
end


I’m sure it will be something stupid and simple…

Move the create-font OUT of the HUDPaint function…

I know you’re running a check, but its something ran very frequently, you want only the logic within that you want to run… It isn’t much to check that variable, but its still good to keep it outside of the HUDPaint. Put it into a [lua]hook.Add( “Initialize”, “InitFonts”, function( ) end );[/lua]

https://dl.dropboxusercontent.com/u/26074909/tutoring/_utilities/runbenchmark.lua

https://dl.dropboxusercontent.com/u/26074909/tutoring/benchmarking_tips/benchmarking_hud_stuff.lua.html

[editline]4th June 2014[/editline]

[lua]local plyTeam = ply:getTeam()[/lua]

should be

[lua]local plyTeam = ply:Team()[/lua]

[lua]surface.SetTextColor( r, g, b, a )[/lua]

should be

[lua]surface.SetTextColor( Color( r, g, b, a ) )[/lua]

Ok this is interesting. The default pistol and magnum fail to display their ammo, the RPG display works fine, but the pulse rifle displays 30 all the time (I haven’t tested any other weapons)

I also changed my code as you said.

HL2 weapons don’t have a SWEP table; their ammo won’t show. You need to create a table for them.

This was posted somewhere on the forum, can’t recall where:

[lua] MaxAmmo={}
client = LocalPlayer()

MaxAmmo["weapon_crowbar"]=0
MaxAmmo[""]=0
MaxAmmo[""]=0
MaxAmmo[""]=
MaxAmmo[""]=0
MaxAmmo["weapon_357"]=
MaxAmmo[""]=
MaxAmmo[""]=30
MaxAmmo[""]=1
MaxAmmo["weapon_frag"]=-1
MaxAmmo["weapon_rpg"]=-1
MaxAmmo["weapon_shotgun"]=6

if client:GetActiveWeapon().Primarythen
    client:GetActiveWeapon().Primary.ClipSize
else
    MaxAmmo[client:GetActiveWeapon():GetClass()]
end[/lua]

You’ll need to do the same for the ammo type as is done for max ammo.

I’d recommend writing it in one table like

[lua]local HL2_WEAPONS_TABLE = {
weapon_crowbar = { MaxAmmo = 0; AmmoType = “Melee” };
weapon_physcannon = { MaxAmmo = 0; AmmoType = “PhysCannon” };
weapon_physgun = { MaxAmmo = 0; AmmoType = “PhysGun” };
weapon_pistol = { MaxAmmo = 18; AmmoType = “Pistol” };
gmod_tool = { MaxAmmo = 0; AmmoType = “Tool” };
weapon_357 = { MaxAmmo = 6; AmmoType = “Pistol” };
weapon_smg1 = { MaxAmmo = 45; AmmoType = “SMG1” };
weapon_ar2 = { MaxAmmo = 30; AmmoType = “Rifle”, SecondaryAmmoType = “CombineBall” };
weapon_crossbow = { MaxAmmo = 1; AmmoType = “Bolts” };
weapon_frag = { MaxAmmo = 1; AmmoType = “Grenade” };
weapon_rpg = { MaxAmmo = 1; AmmoType = “RPG” };
weapon_shotgun = { MaxAmmo = 6; AmmoType = “Shotgun” };
}

[/lua]

Sorry, another stupid question. Where does this go?

I don’t even know how you managed to get that code working…



function GetAmmoForCurrentWeapon(ply)
    local wep = ply:GetActiveWeapon()
    if !IsValid(wep) then return 0 end
    
    return ply:GetAmmoCount(wep:GetPrimaryAmmoType())
end

function GM:HUDPaint()
    self.BaseClass:HUDPaint()
    
    local ply = LocalPlayer()
    --local plyTeam = ply:getTeam()
    
    local health = ply:Health()
    local ammo = GetAmmoForCurrentWeapon(ply)
    
    if health < 0 then health = 0 end
    
    healthTextColour = getTextColourDependingOnHealth(health) // Should return Color() object
    
    if !fontCreated then
        surface.CreateFont("healthFont", 
        {
        font = "Lucida Console",
        size = 100,
        weight = 1000,
        blursize = 0,
        scanlines = 0,
        antialias = true,
        underline = false,
        italic = false,
        strikeout = false,
        symbol = false,
        rotary = false,
        shadow = false,
        additive = false,
        outline = false,
        })
        
        surface.CreateFont("ammoFont", 
        {
        font = "Lucida Console",
        size = 70,
        weight = 1000,
        blursize = 0,
        scanlines = 0,
        antialias = true,
        underline = false,
        italic = false,
        strikeout = false,
        symbol = false,
        rotary = false,
        shadow = false,
        additive = false,
        outline = false,
        })
        
        fontCreated = true
    end
    
    --Health display
    surface.SetTextColor(healthTextColour)
    surface.SetTextPos(20, ScrH()-100)
    surface.SetFont("healthFont")
    surface.DrawText(health)
    
    --Ammo display
    surface.SetTextColor(Color( 255, 217, 0, 200))
    surface.SetTextPos(ScrW()-150, ScrH()-75)
    surface.SetFont("ammoFont")
    surface.DrawText(ammo)
end

What do you mean?

It errors on so many lines.

Why are you such a condescending asshole to people you try to help?