• Lua Tutorial 3 (If you're to lazy to look at GMod Wiki)
    3 replies, posted
[B]Scripted Weapons[/B] Better known as a SWep (or SWEP in some cases) a scripted weapon is a weapon that has an underlying script controlling its operation. Popular choices for SWeps include launching objects, such as watermelons or chairs, and 'better' versions of existing weapons, like a crossbow that can shoot several bolts per second. In this tutorial, we will be making a SWep with the RPG Launcher model to fire chairs. [B]Where to start [/B] First of all, it is suggested you download a good code editor such as [URL="http://notepad-plus-plus.org/"]Notepad++[/URL], with a GMod Lua syntax highlighter like [URL="http://code.google.com/p/npp-gmod-lua/"]this one[/URL]. This will make your life a lot easier. See G[URL="http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index2f64.html?title=Getting_Started_With_Lua"]etting Started With Lua[/URL] for help. [B]The Basic Layout[/B] Go to the directory (folder): <Steam Folder>/steamapps/<Steam Username>/garrysmod/garrysmod/lua. The default location of the steam folder is C:\Program Files\Steam, or C:\Program Files (x86)\Steam for 64-bit operating systems. Now, create a new directory with a simple name (without spaces) for your SWep. e.g. chair_thrower [IMG]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/images/4/45/Tip.png[/IMG]Tip: Note how the underscore _ has been used to replace spaces. [B] Files[/B] In a SWep, there are normally 3 script files: - shared.lua - init.lua - cl_init.lua init.lua is run Serverside. This environment will contain functions that will affect the server, such as killing players or throwing things. cl_init.lua is run Clientside. This is for functions that will affect clients individually. It is used for things like rendering shoot effects/explosions. shared.lua is run in both environments (server/clientside). Normally this is used only for weapon information. There is however, an alternative way of laying out SWeps. It is possible to to include both the server-only and client-only code in the shared.lua file. Simply put the serverside/clientside code inside an if statement like so: [CODE]if SERVER then -- Serverside code -- end if CLIENT then -- Clientside code -- end[/CODE] [IMG]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/images/4/45/Tip.png[/IMG]Tip: You can also use the else/elseif statement instead of two if statements, to tidy up the code. An example of this is shown below: [CODE]if SERVER then // This is where the init.lua stuff goes. //This makes sure clients download the file AddCSLuaFile ("shared.lua") //How heavy the SWep is SWEP.Weight = 5 //Allow automatic switching to/from this weapon when weapons are picked up SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false elseif CLIENT then // This is where the cl_init.lua stuff goes //The name of the SWep, as appears in the weapons tab in the spawn menu(Q Menu) SWEP.PrintName = "Chair throwing gun" //Sets the position of the weapon in the switching menu //(appears when you use the scroll wheel or keys 1-6 by default) SWEP.Slot = 4 SWEP.SlotPos = 1 //Sets drawing the ammuntion levels for this weapon SWEP.DrawAmmo = false //Sets the drawing of the crosshair when this weapon is deployed SWEP.DrawCrosshair = false //Ensures a clean looking notification when a chair is undone. How it works: //When you create an undo, you specify the ID: // undo.Create("Some_Identity") //By creating an associated language, we can make the undo notification look better: // language.Add("Undone_Some_Identity", "Some message...") language.Add("Undone_Thrown_SWEP_Entity","Undone Thrown SWEP Entity") end[/CODE] [B]Note: We will be using the single file (shared.lua) layout for this tutorial. [/B] [B]SWEP Information[/B] At the top of the shared.lua file, you can enter all of the information that will define a SWep's name, category, and certain behaviours, amongst other things. Your SWep will work without most of this information, but it's ideal to include details. [CODE] SWEP.Author = "Your Name" SWEP.Contact = "Your Email Address" SWEP.Purpose = "What your SWep does." SWEP.Instructions = "How to operate your SWep" //The category that you SWep will be shown in, in the Spawn (Q) Menu //(This can be anything, GMod will create the categories for you) SWEP.Category = "Category"[/CODE] The next piece of information is [B]important[/B]. It tells Garry's Mod what player groups can see the SWep in the Spawn (Q) Menu. [CODE] SWEP.Spawnable = true -- Whether regular players can see it SWEP.AdminSpawnable = true -- Whether Admins/Super Admins can see it[/CODE] This information is also important. It defines the SWep's models. [CODE] SWEP.ViewModel = "models/weapons/v_RPG.mdl" -- This is the model used for clients to see in first person. SWEP.WorldModel = "models/weapons/w_rocket_launcher.mdl" -- This is the model shown to all other clients and in third-person.[/CODE] The following code sets up the SWeps primary/secondary fire and ammo. Primary: [CODE]//This determins how big each clip/magazine for the gun is. You can //set it to -1 to disable the ammo system, meaning primary ammo will //not be displayed and will not be affected. SWEP.Primary.ClipSize = -1 //This sets the number of rounds in the clip when you first get the gun. Again it can be set to -1. SWEP.Primary.DefaultClip = -1 //Obvious. Determines whether the primary fire is automatic. This should be true/false SWEP.Primary.Automatic = false //Sets the ammunition type the gun uses, see below for a list of types. SWEP.Primary.Ammo = "none"[/CODE] [B] List of ammo types[/B] [CODE]AR2 - Ammunition of the AR2/Pulse Rifle AlyxGun - (name in-game "5.7mm Ammo") Pistol - Ammunition of the 9MM Pistol SMG1 - Ammunition of the SMG/MP7 357 - Ammunition of the .357 Magnum XBowBolt - Ammunition of the Crossbow Buckshot - Ammunition of the Shotgun RPG_Round - Ammunition of the RPG/Rocket Launcher SMG1_Grenade - Ammunition for the SMG/MP7 grenade launcher (secondary fire) SniperRound SniperPenetratedRound - (name in-game ".45 Ammo") Grenade - Note you must be given the grenade weapon (weapon_frag) before you can throw grenades. Thumper - Ammunition cannot exceed 2 (name in-game "Explosive C4 Ammo") Gravity - (name in-game "4.6MM Ammo") Battery - (name in-game "9MM Ammo") GaussEnergy CombineCannon - (name in-game ".50 Ammo") AirboatGun - (name in-game "5.56MM Ammo") StriderMinigun - (name in-game "7.62MM Ammo") HelicopterGun AR2AltFire - Ammunition of the AR2/Pulse Rifle 'combine ball' (secondary fire) slam - Like Grenade, but for the Selectable Lightweight Attack Munition (S.L.A.M)[/CODE] The same applies to the secondary system: [CODE]SWEP.Secondary.ClipSize = -1 SWEP.Secondary.DefaultClip = -1 SWEP.Secondary.Automatic = false SWEP.Secondary.Ammo = "none"[/CODE] [B] Sounds[/B] It is always good practice to precache sounds, so there is no delay when using the gun. It can be done as below: [CODE]//When the script loads, the sound ''Metal.SawbladeStick'' will be precached, //and a local variable with the sound name created. local ShootSound = Sound("Metal.SawbladeStick")[/CODE] [B]Functions[/B] This is where we define what the SWep will do... [B]Unused Functions[/B] Both Reload() and Think() are not needed by this SWep so we will provide empty functions for them. [CODE]function SWEP:Reload() end function SWEP:Think() end[/CODE] [B] Throw Function[/B] This function we are defining will spawn and throw the chair. It accepts one argument, the filename of the model to be used. That means it can be used to throw almost anything, from barrels to ragdolls. [CODE]function SWEP:throw_attack (model_file) //Get an eye trace. This basically draws an invisible line from //the players eye. This SWep makes very little use of the trace, except to //calculate the amount of force to apply to the object thrown. local tr = self.Owner:GetEyeTrace() //Play some noises/effects
what do I do if I have .50cal Rifle Swep I made for ttt (HOLD TYPE IS AR2) and it looks as if the gun comes out of the player's crotch. Essentially I think I need to "raise" the gun up a but but I dont know how to do this, thanks!
Just a quick tip. Use [noparse][lua][/lua][/noparse] to make the formatting nicer.
great tutorial! thank you
Sorry, you need to Log In to post a reply to this thread.