A group dedicated to indie and standalone game development.

Post tutorial Report RSS Adding reloading to Quake

A tutorial on how to add reloading to Quakes shotgun in qc code, and other tips to help you get started codeing for Quake.

Posted by on - Basic Client Side Coding

Adding reloading to Quake.

By Arkage.


Introduction:

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.

The theory:

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.

The steps:

  • 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.

How:

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.

  1. For each shot fired increase a float(a storage space for a number) by one, then use this to take away from the inventory.
  2. 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.

The code:

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.

c code:
// ARK: Weapon clipss
.float pistolclip;

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:

c code:
self.currentammo = self.ammo_shells = self.ammo_shells - 1;

With this line:

c code:
self.pistolclip = self.pistolclip -1; // This takes 1 away from the clips count.

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:

c code:
else if (self.weapon == IT_SHOTGUN)
  {
    player_shot1 ();
    W_FireShotgun ();
    self.attack_finished = time + 0.5;
  }

With:

c code:
else if (self.weapon == IT_SHOTGUN) // If the current weapon is a shotgun
  {
  if (self.pistolclip > 0) // ARK: If there are bullets in the clip
  {
    player_shot1 ();
    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.

c code:
/*
================
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.
}
else
{
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:

c code:
if (self.impulse == 9)
    CheatCommand ();

Then code to put after the above:

c code:
if (self.impulse == 16)
  {
    reload ();
  }

All that code does is if impulse 16 was typed in to the console run the function called “reload”.

Done.

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.

Good luck.

Please leave a comment if you found this tutorial useful, thanks.

Post comment Comments
Biodude
Biodude

Thanks for the tutorial, Ill use this in my game, my reload code was a bit messy.

Reply Good karma Bad karma+1 vote
Ark_ Author
Ark_

No problem.

Reply Good karma+1 vote
Breadsticks
Breadsticks

This reload code is a bit redundant, although it works, and could be optimized/made cleaner in places.

Reply Good karma Bad karma+1 vote
Ark_ Author
Ark_

Do you have any suggestions in specific? As I mentioned this is my first tutorial on codeing and this is probily the first full feature I have coded for a game, so I would appericate any pointers that you could give me as I would like to start out puting high quality code as soon as I can.
Thanks

Reply Good karma+2 votes
Breadsticks
Breadsticks

There's a redundancy in the first function you tell us to replace, the reload function could be made shorter and a bit simpler (Simpler in my opinion, anyway), and the tutorial's layout itself is messy and hard to read.

Reply Good karma Bad karma+1 vote
DarkSlayer_
DarkSlayer_

This is very similiar like programming in java, thats what I do.
I can understand most of this tutorial, the problem is, the Quake files is what i do not understand cause I never programmed Quake or something similar.
I unterstand all the steps in here, the tutorial is well done, but be carefull... this tutorial is only for people that understands a little bit of programming, in the other way, you dont stand a chance, cause you wont understand a sh*t of this...
Thanks Arkage

Reply Good karma Bad karma+1 vote
Jukki
Jukki

thank you arkage. I am tryng to learn qc. You wont mind if NZ:P Use this code?

Reply Good karma Bad karma+1 vote
Ark_ Author
Ark_

No not at all, if you get stuck just pm me and I can see if i can help.

Reply Good karma+1 vote
Jukki
Jukki

k thank you.

Reply Good karma Bad karma+1 vote
Hazematman
Hazematman

There is a bug in the code, if you have no ammo in your inventory but have ammo in your clip, you can't use the gun.

Reply Good karma Bad karma+1 vote
dr_mabuse1981
dr_mabuse1981

strange, when i try to add this mess to a vanilla quake i get tons of compile errors...

Reply Good karma Bad karma+1 vote
Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: