Garry's Mod
Garry's Mod

Developing Simple SWEPs (Scripted Weapons)

Learn to develop simple SWEPs in Garry's Mod. Create custom weapons with Lua scripts, define properties, and implement primary/secondary fire actions.

Developing Simple SWEPs (Scripted Weapons)

Scripted Weapons, or SWEPs, are custom firearms and tools that You can wield in Garry's Mod. Developing your own SWEPs allows you to create unique gameplay mechanics, add new weapons to your servers, or simply experiment with Lua scripting. This section will introduce you to the fundamental concepts of creating a basic SWEP.

At its core, a SWEP is a Lua script that defines a weapon's behavior. When a player equips a SWEP, the game executes its associated script. To create a SWEP, you'll typically need to create a new Lua file within your `garrysmod/addons/your_addon_name/lua/weapons/` directory. The file should be named after your weapon (e.g., `my_awesome_gun.lua`).

Basic SWEP Structure:

A minimal SWEP script will include several key components:

-- Define the SWEP table
SWEP.Base = "weapon_base" -- Inherit from the base weapon class

-- Weapon Information
SWEP.PrintName = "My Awesome Gun"
SWEP.Author = "Your Name"
SWEP.Category = "My Custom Weapons"

-- Model and Animation Information
SWEP.ViewModel = "models/weapons/v_mygun.mdl"
SWEP.WorldModel = "models/weapons/w_mygun.mdl"
SWEP.ViewModelFlip = false

-- Weapon Properties
SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.Primary.ClipSize = 30
SWEP.Primary.DefaultClip = 60
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = "ammopack"

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"

-- Function to handle primary fire
function SWEP:PrimaryAttack()
 self:SetAnimation(PLAYER_ATTACK1)
 self:ShootBullet(10, 1, 0.05)
 self:TakePrimaryAmmo(1)
end

-- Function to handle secondary fire (optional)
function SWEP:SecondaryAttack()
 -- Implement secondary fire behavior here
end

Explanation of Key Variables:

  • SWEP.Base: Specifies the base weapon class to inherit from. `weapon_base` is the most common.
  • SWEP.PrintName: The name displayed in the spawn menu and HUD.
  • SWEP.Author: Your name or alias.
  • SWEP.Category: The category under which your weapon will appear in the spawn menu.
  • SWEP.ViewModel: The 3D model seen when You holds the weapon.
  • SWEP.WorldModel: The 3D model seen when the weapon is dropped on the ground.
  • SWEP.Primary.ClipSize: The maximum number of rounds in a magazine.
  • SWEP.Primary.DefaultClip: The number of rounds the weapon starts with.
  • SWEP.Primary.Automatic: Set to `true` for automatic weapons, `false` for semi-automatic.
  • SWEP.Primary.Ammo: The type of ammo the weapon uses.

Core Functions:

  • SWEP:PrimaryAttack(): This function is called when You performs a primary fire action (usually left-click).
  • SWEP:SecondaryAttack(): This function is called for secondary fire actions (usually right-click).
  • self:ShootBullet(damage, speed, inaccuracy): A helper function to simulate firing a bullet.
  • self:TakePrimaryAmmo(amount): Decreases the weapon's current ammo count.

Getting Started:

  1. Create the Folder Structure: `garrysmod/addons/my_first_swep/lua/weapons/`
  2. Create the Lua File: Save your script as `my_first_gun.lua` inside the `weapons` folder.
  3. Add Models: Place your custom weapon models (`v_mygun.mdl` and `w_mygun.mdl`) in `garrysmod/addons/my_first_swep/models/weapons/`.
  4. Launch GMod: Start the game, go to the "Spawn Menu" -> "Weapons" -> "My Custom Weapons" (or your chosen category), and spawn your SWEP.

This is a very basic introduction. Advanced SWEPs can involve custom animations, sound effects, unique damage types, special abilities, and much more. The Garry's Mod Wiki is an excellent resource for further learning.