Attaching a variable to a SWEP

Hello,
My question is, how would I attach a variable to a SWEP that can be read later?

What I have to do is set a variable on a SWEP when it is dropped. Then when someone picks it up again, they read the variable. I am doing this to set the person who picks it up’s ammo to what the ammo was when the person who used to have it dropped it.

Help would be appreciated.

And hey, where’d all our post icons go?!?!

YourWeapon.YourVariable = 123 ?

That does not appear to work, I tried that and setting a networked int too before posting.

Where do you set them?

In the init.lua of the game mode, I have this:

[lua]function weaponDrop(Pl, Txt)
if Txt == “drop” then
Pl:GetActiveWeapon().primeammo = 10
Pl:DropWeapon(Pl:GetActiveWeapon())
end
end
hook.Add(“PlayerSay”, “weaponDrop”, weaponDrop)[/lua]

and then in the init.lua for the base game mode which houses the SWEP:Equip() function which is run when a player or npc picks up a gun, I have this:

[lua]function SWEP:Equip( NewOwner )
if self:GetNWInt(“primeammo”) then print(self.primeammo) else print(“nwint not found”) end
end[/lua]

It prints nothing.

Try to set the variable after dropping it.

How would I do that? It’s not the PrimaryWeapon so what do I use to reference it.

Using weapon.variable does not set a networked variable. You should change the init.lua for the base gamemode to this:
[lua]function SWEP:Equip( NewOwner )
if self.primeammo then print(self.primeammo) else print(“nwint not found”) end
end [/lua]

I accidentally posted an old version of what I did. I have tried what you said already and it didn’t work.

Hey could you please extend on that? I knew they weren’t the same but technically speaking what can a NWVar of an object do a variable of an object can’t? How do I know I should use one or the other?

So do you guys have any other ideas?

NWVars are networked, which means you can use them both clientside and serverside if you set them on the server. Setting them clientside won’t send them to the server, though.
Using weapon.VAR will only have the variable on where it was set. E.g. if you set weapon.Bullets on the server, the client won’t be able to read it. That’s why most of the time you should use NWVars, only use weapon.VAR if you have temporary stuff or stuff the client doesn’t have to know about.

Dlaor answered it.

Thanks! :slight_smile:

[lua]function SWEP:OnDrop()
self.Weapon:SetNetworkedInt( “DroppedAmmo”, self.Weapon:Clip1() )
end

function SWEP:Equip()
self.Weapon:SetClip1( self.Weapon:GetNetworkedInt( “DroppedAmmo” ) )
end[/lua]

Is that what you need?
EDIT: Errr… doesn’t quite work, probably the script I have

No he didn’t. Neither SetNWInt nor doing an object variable or whatever it’s called works.

Try changing it to this:
[lua]function SWEP:Equip( NewOwner )
print( “Test test” )
end [/lua]
If you don’t see Test test in console, then the problem doesn’t lie in your code, but rather something else.

I put that in the swep base in the base gamemode, and that didn’t work, so I tried defining it in the shared swep file, and it worked, so I’ll go try that other stuff again.

[editline]08:10PM[/editline]

Massive fail.

You have to put the code in the swep’s code for it to work.

[lua]
function SWEP:Equip(newOwner)
print(self.ammo)
end

function SWEP:OnDrop()
self.ammo = 1
end[/lua]

??? :frowning: Of course! If it’s called SWEP:Something() it has nothing to do with the gamemode.

I was looking at this page:

and it said:
"As defined in “gamemodes\base\entities\weapons\weapon_base\init.lua”

So I thought if I modified that file then it would carry to the other weapons, but guess not. No need to rate me dumb.