Coding weapons as minimally as possible

Using scripts

There’s really no good reason to be writing as much code as you need to to implement a networked weapon into GoldSource. A huge amount of the functions, for each weapon, are almost identical clones of each other, perhaps with some properties tweaked. For that reason, I wanted to create a weapon script system that would read from a .txt file and obtain most of its functionality from that. This was fairly easy to do, though a bit of work was needed to ensure that the script parsing was consistent on both client and server. This is because the client should know about some stuff about the weapon to avoid having to send network messages across. The main functions for the script parsing are in a file shared on both client and server, with a couple of ifdefs just to make sure that the client can correctly parse the weapon script too. For the actual parsing, I’m using the simple built in COM_Parse functionality found in both client and server.

I then got to work analysing a single weapon from Half-Life (I picked the MP5) and seeing how much of the code I could move into generic CBasePlayerWeapon code. It turns out that, actually, quite a lot can. So much so that my specific weapon .cpp files are about 30 lines of code now.

As for the client-side prediction stuff, I decided upon a single event for shooting weapons, using iparam, bparam and fparams to store all the info I needed, and will use 2 other separate generic ones for melee and projectile weapons.

I’m overall very happy with my system, as adding new, networked weapons into Half-Life is probably one of the most dull parts of development.