Scripting A Gun From Scratch

Hey guys, I’ve just recently gotten into LUA, and I am somewhat used to the tools by now.
That being said, I’m still a newbie, so keep the language simple please? Thanks!
I’ve googled for hours on end and watched endless videos on Youtube about it.
But no matter what I look at or read, it’s all about RE-SCRIPTING guns instead of making them from scratch.
I tried adding a Mass Effect 2 prop to the mix as a V model and W model, but it came up as an error.
I now realize why, it’s because it physically needs set animations.
But I posted that question in another topic. (Link down below)

So basically, I’m asking, How would I go about scripting a gun from scratch?
I use Notepad++ to code, if you suggest something else, please tell.

Link to question about Custom Animation Creation;

First go to your lua/entities/weapons/ directory and create a folder for the new weapon. ex: “Raygun”.

Inside the folder, create a shared.lua, inside it put this

if (SERVER) then
AddCSLuaFile( “shared.lua” );
SWEP.Weight = 5;
SWEP.AutoSwitchTo = false;
SWEP.AutoSwitchFrom = false;

if ( CLIENT ) then
SWEP.PrintName = “Hands”;
SWEP.Author = “LauScript”;
SWEP.Contact = “”;
SWEP.DrawAmmo = false;
SWEP.DrawCrosshair = false;
SWEP.ViewModelFOV = 70;
SWEP.ViewModelFlip = false;
SWEP.CSMuzzleFlashes = false;

SWEP.Spawnable = true;
SWEP.AdminSpawnable = true;

SWEP.ViewModel = “”; – The weapons view model.
SWEP.WorldModel = “”; – The weapons world model.
SWEP.HoldType = “normal”; – The holdtype for the weapon.

SWEP.Primary.ClipSize = 1; – The maximum amount of ammunition a magazine can hold.
SWEP.Primary.DefaultClip = 1; – The amount of ammunition is in the weapon when picked up without ammunition.
SWEP.Primary.Automatic = false; – Whether or not the weapon is fully automatic or not.

SWEP.Secondary.ClipSize = 1; – Secondary fire clipsize.
SWEP.Secondary.DefaultClip = 1; – How much secondary ammo in the gun by default.

– The hook that is called when the weapon is reloaded.
function SWEP:Reload()

– The hook to call when the weapon is shot on primary attack.
function SWEP:PrimaryAttack()

– The hook to call when the weapon is shot on primary attack.
function SWEP:SecondaryAttack()

– The hook called every frame.
function SWEP:Think()

There you are.

Look, quite honestly, I was also blasted away by the fact that you just had to redefine a SWEP base to create weapons.

Anyways if you feel ballsy enough, you could reimplement a whole new gun code that wouldn’t use the base.

Like : load some model, get trace from muzzle to eyetrace, get a circle around that point, decide where the bullet should land, stuff like that. (lolimbadatlua)

Thanks guys, and yeah Space, I was mostly just thinking about that kind of stuff. Like, I modeled a new gun in blender and want to script it from scratch, just wondering how to. Thanks a ton.

[editline]9th November 2011[/editline]

One question, Lua, that script you have there. With the model, what would I put in? Could I put in literally any prop?

Totally. But viewmodels are obviously made so that when they are loaded on the screen, they look like some real gun being held by the player.

You surely already have seen toybox weapons where people put some models on the regular gun to simulate a new appearence. This is obviously not what we’re talking about but that shows you that you may indeed load whatever model you want.

First things you wanna be sure about are :

-Is your gun model compatible with the source engine ? (AKA did you go through the fucking tiresome compiling process)

-Is your gun model’s camera setup so that it would show what you’d want to see ingame ?

(Fact is making gun models for source is a P.I.T.A. but watev)

So yeah if I were you, if I really wanted to script my gun from scratch, here are the stages I’d go through :

-Have a way to show the gun model on screen and have the correct animations loaded.

-Make it so that on every click :
-Check for an ammo count and act accordingly
-If enough ammo, get trace from gun muzzle (Setup that point on relevant coordinates relative to the player entity) to the
-Use your brain and your math knowledge to determine a circle that would correspond to a certain spread value for your
gun, based on the distance (Just a rectangle triangle, really.)
-Get a random point of a circle that would be randomly smaller than the first one to get the point where the gun should fire.
-Get the trace from muzzle to point, if there’s an entity inside that trace, apply damage to it. If not, well, I’m actually shit
at lua, so I don’t know if traces prolounge until they hit shit or if they are just segments. In the latter case, you’d need to
find a way to prolounge it and get the according entity. Of course, eyetracemake it only hurt things that can be hurt.
-Update counters for ammo and shit, fire up the according animations and stuff.

Then just add stuff for when reload is pressed, and all.

Keep in mind that this is just some algorithm and that surely can’t be implemented just like that : you have to find the appropriate ways to implement that shit up and that’s not really what I’m good at. BUT YE WHY NOT USE THE SWEP BASE LOL

put in a view_model, and if it’s a world model, put it anything yes. If the model doesn’t have animations it goes to the crotch.

And so with that, crotch thing. Do you suggest I use blender for my animations? Or some other program?

Really, you gotta get the hand on 3DSMax or Maya to have an easy time animating and exporting to source