Solitude (Working Title) is a PSP and PC game using a heavily modified Quake engine, expanding upon the story of Halo: Combat Evolved. Expect a great, brand new single player story and all of the most popular multiplayer modes from Halo: CE! Our team is working hard to bring the game to you as fast as possible so be sure to stay tuned for any new updates. More information can be found on our site at www.halosolitude.com. Note: We are NOT ripping anything from any of Halos! All our models and textures are our own, made by using the original Halo art only as reference.

Report article RSS Feed 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 Ark_ on Jan 14th, 2010
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 Jan 14 2010, 9:55pm says:

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

+1 vote     reply to comment
Ark_ Author
Ark_ Jan 15 2010, 4:27am replied:

No problem.

+1 vote   reply to comment
Breadsticks
Breadsticks Jan 14 2010, 11:17pm says:

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

+1 vote     reply to comment
Ark_ Author
Ark_ Jan 15 2010, 4:26am replied:

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

+2 votes   reply to comment
Breadsticks
Breadsticks Jan 15 2010, 9:24pm replied:

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.

+1 vote     reply to comment
DarkSlayer_
DarkSlayer_ Jan 15 2010, 9:09am says:

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

+1 vote     reply to comment
AaronLewis
AaronLewis Jan 15 2010, 7:51pm says:

Thanks Ark.
Helps me a bunch.

+1 vote     reply to comment
Jukki Online
Jukki Mar 29 2010, 7:24am says:

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

+1 vote     reply to comment
Ark_ Author
Ark_ Apr 2 2010, 9:00am says:

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

+1 vote   reply to comment
Jukki Online
Jukki Apr 2 2010, 1:24pm replied:

k thank you.

+1 vote     reply to comment
Hazematman
Hazematman Jul 15 2010, 6:02pm says:

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.

+1 vote     reply to comment
dr_mabuse1981
dr_mabuse1981 Sep 22 2011, 4:54am says:

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

+1 vote     reply to comment
thommoboy
thommoboy Dec 1 2011, 2:45am says:

i get this error
,
weapons.qc:931: error: Unknown value "w_pistol".

wtf?

+1 vote     reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Icon
Solitude
Platforms
Windows, PSP
Developer & Publisher
Flaming Ice
Engine
Quake Engine
Contact
Send Message
Release Date
TBD
Game Watch
Track this game
Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Games
Solitude
Solitude Single & Multiplayer First Person Shooter
Related Engines
Quake Engine
Quake Engine GPL Released Jun 22, 1996
Related Groups
Flaming Ice
Flaming Ice Developer & Publisher with 6 members
Indie Devs
Indie Devs Hobbies & Interests group with 1,129 members
QuakeDB
QuakeDB Fans & Clans group with 141 members