Complicated Problem

I have an entity already such that when you press E on it, it disappears and gives you some ammo (it’s an ammo pickup).

I want to make something such that when a player dies, he drops whatever ammo (of a certain type) he had. I can’t simply spawn my ammobox entity upon a player’s death because it gives a set amount of ammo.

How can I make something such that the ammo it gives upon being “used” is dependent on how much ammo the player that dropped it had when he died?

For instance, bob had 47 pistol rounds when he got mauled by zombies. Jim comes along and picks up the little ammobox near bob’s body and receives 47 pistol rounds.

I hope that made sense.

In your entity, do this:



ENT.DefaultAmmo = 40 --Where 40 is whatever a default ammo entity should give you
ENT.AmmoType = "pistol" --Default ammo type

AccessorFunc( ENT, "DefaultAmmo", "DefaultAmmo" )
AccessorFunc( ENT, "AmmoType", "AmmoType" )

function ENT:Use( pl )
  pl:GiveAmmo( self:GetDefaultAmmo( ), self:GetAmmoType( ) )
  self:Remove( )
end


Now, when a player dies, create your box and do this:



box:SetDefaultAmmo( pl:GetAmmoCount( "pistol" ) )


This will make it so that when Bob dies, the ammo box he drops will have 47 pistol bullets in it ( assuming he had that many ).

Something along those lines is how I would do it.

Fantastic! Thanks!

I’ve never understood AccessorFuncs. (As in, I don’t know what they do, I should look over the code…)

[lua]/---------------------------------------------------------
AccessorFunc
Quickly make Get/Set accessor fuctions on the specified table
---------------------------------------------------------
/
function AccessorFunc( tab, varname, name, iForce )

    tab[ "Get"..name ] = function ( self ) return self[ varname ] end

    if ( iForce == FORCE_STRING ) then
            tab[ "Set"..name ] = function ( self, v ) self[ varname ] = tostring(v) end
    return end
   
    if ( iForce == FORCE_NUMBER ) then
            tab[ "Set"..name ] = function ( self, v ) self[ varname ] = tonumber(v) end
    return end
   
    if ( iForce == FORCE_BOOL ) then
            tab[ "Set"..name ] = function ( self, v ) self[ varname ] = tobool(v) end
    return end
   
    tab[ "Set"..name ] = function ( self, v ) self[ varname ] = v end

end[/lua]

Basically adds an entity.Get[var] function which returns the value of the variable.

Hey, even though this worked and all, I have an even more complicated problem that I think might be able to be solved in much the same way.
(I hope the guy who helped me before can see this)

I have an entity that uses the model of a weapon. When you press E on the entity, you get the corresponding weapon (and the entity removes itself).

The SWEP you receive has a DefaultClip of 0, meaning it has no ammo when you get it. I HAD to make the SWEP that way in order to avoid potential exploitation (because if the SWEP had ammo inherently, you could shoot it empty, drop it, pick it up, and it would have ammo again).

Whenever you drop the weapon (currently the only way is by dying), it re-creates the special weapon-giving entity on the ground near you.

Problem is, if you have a full magazine in your weapon or if you have ANY ammo (PrimaryClip) in your weapon when you drop it, the entity created will still give the next person to activate it an empty SWEP. In this way it is possible to lose ammunition into oblivion (which is something I want to avoid).

There must be SOME way to get the ammo count in someone’s PrimaryClip, store it somehow in the entity that is created when the weapon is dropped, and then, when the SENT is activated, implant it into the SWEP that’s given so that the person picking it up receives a SWEP with the same amount of ammo in its magazine as the SWEP the previous person had.

I hope that made sense.

Simple example:
Bob was on his last mag and had 2 rounds left in his pistol when he got mauled by zombies. When he died, his pistol dropped to the ground. Jim came along and picked up Bob’s pistol, which had 2 rounds left in the magazine. Jim fired the two shots, at which point the pistol was empty. Jim didn’t have any pistol rounds with him, so he dropped the empty pistol and continued on his way. Later, Frank saw the pistol on the ground and picked it up, but, much to his dismay, it was empty.

EDIT:
I can think of a way to do this, but it requires that I make a SHITLOAD more SWEPs, one SWEP for each unique number of rounds in the magazine (and the ent gives you the SWEP that corresponds to the number of rounds the SWEP had when it was dropped the last time). I REALLY don’t want to do that.

Your weapon entity:

[lua]
ENT.WeaponType = “weapon_pistol”
ENT.Clip1 = 0
ENT.Clip2 = 0

AccessorFunc( ENT, “WeaponType”, “WeaponType” )
AccessorFunc( ENT, “Clip1”, “Clip1” )
AccessorFunc( ENT, “Clip2”, “Clip2” )

function ENT:Use( pl )
pl:Give( self:GetWeaponType( ) )
pl:SelectWeapon( self:GetWeaponType( ) )
local wep

wep = pl:GetActiveWeapon( )

if wep:GetClass( ) == self:GetWeaponType( ) then
wep:SetClip1( self:GetClip1( ) )
wep:SetClip2( self:GetClip2( ) )
end

self:Remove( )
end
[/lua]

Your death code

[lua]
wep = pl:GetActiveWeapon( )

pickup:SetWeaponType( wep:GetClass( ) )
pickup:SetClip1( wep:Clip1( ) )
pickup:SetClip2( wep:Clip2( ) )
[/lua]

This will set the pickup bob dropped to whatever weapon he was carrying ( his pistol in this case ), and both clip1 and clip2 to the proper values ( clip1: 2, clip2: 0 ).

Um, I don’t need to use GetActiveWeapon because each weapon entity only ever gives or deals with one weapon. I learned from gmodwiki that I can use ply:GetWeapon() to get the entity of a particular weapon, so how can I incorporate that into here to make things simpler? Please note that when someone dies, he drops every weapon he had (every one of the special weapons coded in my pack), not just his active weapon. Each weapon entity independently creates itself when someone dies (if that someone had the weapon that that entity deals with).

EDIT:

Nevermind, I figured it out. Thank you so much.