Help me understand this SWEP

Since I do not know how to use Lua, but I am determined to learn how without first reading the massive amount of documentation that assumes I already understand how programming languages work, I will just look at this SWEP instead and try to learn Lua from it.

Some bits are fairly obvious to me. Some are mysterious and spooky, like Kriss Angel the Mind Freak!! Please help me to understand what they do.


brb learning lua makes me so excited I have to poop

You’re the greatest.

Thank you!

In retrospect, I realized the “weight” variable probably refers to the weapon’s rank in autoswitch, not actual weight.

NumShots is i believe the number of ‘bullets’ per shot. Kind of like a shotgun.

Icon letter refers to the character(s) in a font to use when you kill somebody with the weapon, but only because the of the line right below it that makes use of it.

Base is the weapon to, well, base it off. Oranges and Apples are sub-varieties of Fruit, so you could look at it as their Base is Fruit. This is called Inheritance.

Category is the label it gets in the Spawn Menu, like how all of the Counter Strike weapons are under “Counter-Strike.”

As was said, NumShots is how many bullets to shoot at once.

IronSightsPos and Ang refer to how the view model is moved when ironsights are enabled, which is right click on the default CSS weapons, and most other weapons.

I believe you are correct about the weight variable.

Note that this code is not really a lua “script” but merely a table of values the game will use on top of the specified weapon base to modify it’s properties. That weapon base is what holds the code that makes a swep shoot and animate.

That being said I would like to suggest you at least take a look at the Lua starter tutorial in the Lua Tutorial Series of the Lua section, on the wiki. :smile: It is absolutely as simple as they get and you don’t need any kind of programming knownledge.

As for learning Garry’s Mod specific stuff there’s not really a better method then what you’re doing, opening scripts and messing with them until you understand why you broke them (because you will) and eventually how to make them do what you want. :wink:

Yo th89 I sorta know this stuff hit me up on msn

Aw you guys are the best. hugz n kisses (no homo)


Ohh, question! Does the “primary ammo = smg1” line determine the behavior of the actual projectiles, or does it only refer to what kind of ammo you can pick up?

Pick up yeah, it defines what type of ammo will be depleted when you shoot.

In this script what actually changes the behavior is properties like NumShots and Delay. It’s simple like that because it’s based on a weapon scripted to shoot bullets. It’s just telling it how it should shoot said bullets. If you wanted to make a melee weapon out of it you would need either a different base or a proper SWEP:PrimaryFire() function.

Okay, makes sense.

So I have a firing sound effect that I want to put in in place of the CS:S one, but I’m not sure how to do that. I see this:

SWEP.Primary.Sound			= Sound("weapon_M4A1.Single")

But what does that refer to? With no folder structure I’m kind of lost–it doesn’t even have a .wav extension.

In this case it refers to a game sound, so only the name of the sound is needed. You can replace all of it with a string that is the path to your sound :

SWEP.Primary.Sound			= "path/to/my/sound.wav"

The root folder is sounds, so no need to mention it.

Oh and about the SERVER and CLIENT stuff, that file is called shared.lua as is common for SWEPS and is read both time by the game, both by the server and by every client’s machine. The server will simply skip everything inside CLIENT blocks and vice-versa.

Well that’s odd. Does source only support certain types of wavs, maybe?

Maybe you do need to put sounds\ at the start of the path. You do with models.

weapon_M4A1.Single is actually a reference to a particular sound.

Use Sound(“weapons/ksar5/ksar5_single.wav”) for your custom sound.

Tried that as well, no dice. Very weird.


I’ve been using just the string, but putting Sound() on it doesn’t seem to work either. Still says it can’t load the sound.

Try playing the sound to yourself first. In the console type
play <path>

If it doesn’t work and you’re certain the path is correct try changing maps or restarting gmod if you’ve done neither of those after having installed the sound.

Still nothing D:

I feel like this might be a problem with the sound file, but I can’t find any pages detailing Source’s requirements for sound files

Try doing

play weapons/ksar5/ksar5_single

in your console and see if it says anything.

I did, it gave the same error

Well I guess you could try opening a sound you know works, and use it’s sample rate and file format on yours. I’m not certain that would help though. :smile: