Garry's Example SWEP
From GMod Wiki
Lua: Example SWEP |
Description: | Hopes to explain how to code SWEPs. |
Original Author: | Garry |
Created: | 29th November 2006 |
So in our weapon folder we have 3 files.
You don't need all of these files.. if you only have a shared.lua it will use that.
cl_init.lua (This is the SWEP initialization on the clientside)
include('shared.lua') SWEP.PrintName = "Manhack Gun" SWEP.Slot = 3 SWEP.SlotPos = 1 SWEP.DrawAmmo = false SWEP.DrawCrosshair = false
As you can see, nothing very interesting. Setting the print name and the slot and other clientside stuff.
init.lua (Serverside SWEP init)
AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include('shared.lua') SWEP.Weight = 5 SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false
AddCSLuaFile adds the file so when the client connects it is sent to them (If they don't already have it cached).
shared.lua (Both files include this)
SWEP.Author = "Garry Newman" SWEP.Contact = "" SWEP.Purpose = "" SWEP.Instructions = "Left click spawns manhack, right click spawns Rollermine" SWEP.Spawnable = false SWEP.AdminSpawnable = true SWEP.ViewModel = "models/weapons/v_pistol.mdl" SWEP.WorldModel = "models/weapons/w_pistol.mdl" SWEP.Primary.ClipSize = -1 SWEP.Primary.DefaultClip = -1 SWEP.Primary.Automatic = false SWEP.Primary.Ammo = "none" SWEP.Secondary.ClipSize = -1 SWEP.Secondary.DefaultClip = -1 SWEP.Secondary.Automatic = false SWEP.Secondary.Ammo = "none" local ShootSound = Sound( "Metal.SawbladeStick" ) /*--------------------------------------------------------- Reload does nothing ---------------------------------------------------------*/ function SWEP:Reload() end /*--------------------------------------------------------- Think does nothing ---------------------------------------------------------*/ function SWEP:Think() end /*--------------------------------------------------------- PrimaryAttack ---------------------------------------------------------*/ function SWEP:PrimaryAttack() local tr = self.Owner:GetEyeTrace() if ( tr.HitWorld ) then return end local effectdata = EffectData() effectdata:SetOrigin( tr.HitPos ) effectdata:SetNormal( tr.HitNormal ) effectdata:SetMagnitude( 8 ) effectdata:SetScale( 1 ) effectdata:SetRadius( 16 ) util.Effect( "Sparks", effectdata ) self.Weapon:EmitSound( ShootSound ) self.BaseClass.ShootEffects( self ) // The rest is only done on the server if ( !SERVER ) then return end // Make a manhack local ent = ents.Create( "npc_manhack" ) ent:SetPos( tr.HitPos + self.Owner:GetAimVector() * -16 ) ent:SetAngles( tr.HitNormal:Angle() ) ent:Spawn() // Weld it to the object that we hit local weld = constraint.Weld( tr.Entity, ent, tr.PhysicsBone, 0, 0 ) undo.Create( "Manhack" ) undo.AddEntity( weld ) undo.AddEntity( ent ) undo.SetPlayer( self.Owner ) undo.Finish() end /*--------------------------------------------------------- SecondaryAttack ---------------------------------------------------------*/ function SWEP:SecondaryAttack() local tr = self.Owner:GetEyeTrace() if ( tr.HitWorld ) then return end self.Weapon:EmitSound( ShootSound ) self.BaseClass.ShootEffects( self ) local effectdata = EffectData() effectdata:SetOrigin( tr.HitPos ) effectdata:SetNormal( tr.HitNormal ) effectdata:SetMagnitude( 8 ) effectdata:SetScale( 1 ) effectdata:SetRadius( 16 ) util.Effect( "Sparks", effectdata ) // The rest is only done on the server if ( !SERVER ) then return end // Make a rollermine local ent = ents.Create( "npc_rollermine" ) ent:SetPos( tr.HitPos + self.Owner:GetAimVector() * -16 ) ent:SetAngles( tr.HitNormal:Angle() ) ent:Spawn() // Weld it to the object that we hit local weld = constraint.Weld( tr.Entity, ent, tr.PhysicsBone, 0, 0 ) undo.Create( "Rollermine" ) undo.AddEntity( weld ) undo.AddEntity( ent ) undo.SetPlayer( self.Owner ) undo.Finish() end
Originally posted by garry at FacePunch Forums