Adding reloading to Quake.
This is my first tutorial on programming, ok with the confession out of the way lets move on shall we.
For this tutorial I assume that you have at least a familiarity with the basic structure for how programming works, a bit of general knowledge really.
I don’t want this to be a copy, paste and your done style tutorial instead I will try to explain how and why it works so that you can code more that just reloading.
It can be good practice to think you code thought before you start to actually code it in, Programming is problem solving and the best way to solve a problem is to break it up into small steps and that’s what we will do here.
Main Goal: add reloading to Quakes shot gun.
- Only let the gun fire when there are bullets in the clip.
- Take a shell away from the clips count after each shot.
- When the reload button is pressed take the number of shots fired away from the inventory and restore the clip to full amount.
As an example lets say we have 50 shells in our inventory for the shot gun.
The clip can hold 12.
The player shoots 5 shells and then reloads.
What we want is for each shot to take 1 shell from the clip.
So after those 5 shots are taken away from the clip (12-5=7) we have 7 shells left in the clip.
Now we only want take away the shots fired from our inventory (50) so to do this we need to know how many shots the player fired, now there are two ways to do this.
- For each shot fired increase a float(a storage space for a number) by one, then use this to take away from the inventory.
- Since the capacity of the clip is consent for this weapon we can calculate the
shots fired by taking the shells left in the clip from its max capacity (12-5=7).
I prefer the latter method and that’s what we will be using in this tutorial.
Ok so lets start with the code shall we, I will explain more as we go along.
Note in this code “pistol_clip” will be the shotguns clip(due to convience for me as I was using it as such). First off we will need to make a place to store the amount of shots in the clip, “pistolclip in this case. The best place to do this is in the file “Defs.qc” this is where most of quakes variables are declared.
It is important to not that QC has a build order, think of all those .qc files as being just one pasted in the order according to the “progs.src” file. So the .qc file listed first would be before any thing else in the qc code. This is important because in QC functions can only know what is above them but not below, this is way defs.qc is near the to, so that the rest of the code can read these values.
Ok back on topic lets add our variable that will hold the shell in our clip.
Open up the file “Defs.qc” and scroll to the bottom of the file and add the following lines of code.
Now what this does is set up a place for us to store the clips vaule. The “.” Means that it can be assigned to an entity like the player and the float means that it’s a number.
Now we need to change some of the “W_FireShotgun” (you can do a search by holding ctrl and pressing F in most code editors)
Replace this line:
With this line:
The comment should explain whats happening there.
Next we will add the check so that the shotgun will only shoot when it has ammo and also a check that if the clip is empty to automatically reload.
Still in “Weapons.qc” go to the function “W_Attack” and replace this:
self.attack_finished = time + 0.5;
if (self.pistolclip > 0) // ARK: If there are bullets in the clip
W_FireShotgun (); // Then shoot
self.attack_finished = time + 0.5;
if (self.pistolclip == 0) // If the clip is empty
reload (); //reload, This calls the function “reload” that we are about to makemake
Now lets make the reload function mentioned above.
Go to the top of the “Weapons.qc” file and copy the following in there.
Reload by Arkage
void() reload =
local .float shots_fired; // A float that will only be used in this function and cant be called by others
if (self.weapon == IT_SHOTGUN) // ARK: Checks if the current weapon is the shotgun(used as a pistol in this case)
self.shots_fired = 12 - self.pistolclip; // ARK: caluclate the shots fired
if (self.ammo_shells > self.shots_fired) // If we have more shells in our invitory then those that need to be reload then its easy.)
self.ammo_shells = self.ammo_shells - self.shots_fired; // We take the shots fired and take that away from out invetory.
self.pistolclip = 12; // Now we refill the clip.
self.pistolclip = self.pistolclip + self.ammo_shells; // Since the ammo in ammo_shells is less than the shots fired we can add that to the clip
self.ammo_shells = 0; // set the shells to zero because there are none left.
self.currentammo = self.ammo_shells; // Update the current ammo (shown on the Quake hud) to show how many bullets are left in the invetory
self.attack_finished = time + 4; // Set the next attack four seconds from now, you cant shoot for four seconds.
The comments should explain it well enough.
Now its all well and good the shot gun will now fire 12 shots and then reload automaticly ( with a four second pause) but what if you want to be able to reload at any time. Read on as this is very easy.
Still in “Weapons.qc” go to the “Impulsecommands” function, this is where the impulses are set up (where you type impulse (number) in to quakes console) and it is here where we will make our own for reloading.
Put the next block of code after:
Then code to put after the above:
All that code does is if impulse 16 was typed in to the console run the function called “reload”.
You can bind that impulse to a key with the console command: bind(th key here) “impulse 16” Then save that to your configs.
Now you should have a shot gun with a clip that reloads when the clip is empty or you type impulse 16. The limit at the moment is that the gun doesn’t animate when reloading, I may address this in a later tutorial or add on to this one.
Now try adding reloading to the other weapons in quake with different clip sizes or even try one of you ideas that you are bound to have, I hope that this tutorial has given you a nice introduction to qc and that you have picked up some of the ins and outs of the language.
If you want to try one of those ideas you are bound to have had, remember to break it up into steps, the odd are that some of IDs code does something similar to one or more of those steps so you will often find yourself repurposing some of the existing code to suit your needs.
Please leave a comment if you found this tutorial useful, thanks.