Tutorials - Chair Throwing Gun
Introduction
In this tutorial we will create a weapon that fires chairs.
File Layout
- Open your garry's mod folder.
- Open the 'addons' folder.
- Create a folder called 'chairlauncher'
- Create a folder inside called 'lua'
- Create a folder inside called 'weapons'
- Create a file called 'weapon_chairgun.lua'
Your folder structure should be like this:
You have created an addon called chairlauncher which contains a weapon called weapon_chairgun. Whenever you create a new weapon, most people use the weapon_ prefix, since most of the default weapons names also start with weapon_.
The Code
Add these to your weapon after you understand them.
Informational
This code is just informational. It shows up in the spawnmenu and the weapon selection menu.. so it will help people out if your weapon is complicated to use. You can also put your name in the author field so people know who made it.
Spawn Info
Defines whether players can spawn this weapon from the spawnmenu.. and whether only admins can spawn it.
Clip Info
Our weapon doesn't use any ammo or clips, so we just set the clip sizes to -1 and the ammo to "none".
We set primary to be automatic. This means that the player doesn't have to release the mouse button and press it again - it will continually fire.
More Info
These are mostly self explanatory. The higher the weight the more likely you are to switch to it. Slot and SlotPos decide where in your weapon menu the weapon will be.
Models
The view-model and the world-model to use.
Precache Sounds
You can precache sounds by defining them as locals or SWEP variables using the Sound function. This prevents the sound loading when you first shoot the weapon - causing a hitch.
The actual chair throwing code
Commented for your learning pleasure
Challenges
Can you edit the new weapon to:
- Use a different firing sound for left/right click
- Throw a melon when you press reload? (tip: WEAPON:Reload)
- Throw 3 chairs at a time