Named Game of the Year by over 50 publications, Valve's début title blends action and adventure with award-winning technology to create a frighteningly realistic world where players must think to survive. Also includes an exciting multiplayer mode that allows you to play against friends and enemies around the world.

Here we take a look at setting up and configuring Visual Studio 2013 alongside the Half-Life SDK for programming. We also show how to build, and run the Game in Debug mode. We also touch lightly on some coding by showing how to output a HUD message when the player jumps

Installation

Note: The Half-Life SDK available on Github was built using Visual Studio 2010 so it will run out of the box with that version. Newer versions require that the Solutions be converted to work with these newer versions of the IDE. The IDE will tell you when it performs this conversion.

Download the Half-Life SDK from Valve’s Github repository, You can place and extract the downloaded ZIP anywhere you like.

Note: For those using Visual Studio 2015 changes to the C Library have caused many warnings in Valves Half-Life SDK. Community member Malortie has created a fork which compiles for Visual Studio 2015. I would suggest using it until Valve makes changes to support Visual Studio 2015 in the official repository.

Mine is located here:

C:\_Projects\halflife-master

Opening the Project and Configuring

Open the projects.sln from the projects directory in the downloaded Half-life Master

C:\_Projects\halflife-master\projects\vs2010\projects.sln

Note: You may see the following if you are using a version of Visual Studio newer than 2010 Simply click OK and let it work. This is the conversion process I noted earlier.

I also suggest you change the color scheme to Dark to make viewing and editing the code easier. This can be done from Tools > Options > Environment > General and setting Color Theme to Dark

To the right hand side of the IDE note the Solution Explorer which should have 6 projects included:

The one of interest to us is hldll as highlighted in bold ( Bold highlighting indicates that it is the start-up project ) If it is not highlighted in bold simply right click on it an Select Set as StartUp Project.

We need to make some changes to hldll’s properties. To access these simply right click and select Properties.

Firstly let’s change Configuration in the top left corner to “All Configurations”. This means that changes we make to the properties page will propagate through all project configurations in this case both Debug and Release.

Then we should turn off the post Build process which can be disabled by setting Use In Build to No in Configuration Properties > Build Events > Post-Build Event ( This command when it works correctly would have copied the complied DLL to the Mod directory, simply put for the scope of this tutorial we do not need it )

Next we must Change under Configuration Properties -> General the Output Directory from:

$(Configuration)\$(ProjectName)\

to the directory inside your Half-Life installation where hl.dll resides. In my case it is here:

D:\Programs\Steam\SteamApps\common\Half-Life\valve\dlls\

If we want to be able to debug our code within Visual Studio we need to tell Visual Studio where hl.exe resides.

Under Debugging -> Command provide the path to hl.exe. For me this is:

D:\Programs\Steam\SteamApps\common\Half-Life\hl.exe

Add the following to Command Arguments:

-windowed -debug -dev

Set the working directory to the root Half Life Directory, in my case:

D:\Programs\Steam\SteamApps\common\Half-Life\

We should Unload the remaining Projects in the solution that we are currently not using. Right click on all unwanted projects (hl_cdll, dmcdll, etc..) and select Unload from the menu. hldll should be the only remaining “loaded” project.

Build the Project

Now it is time to build the project for the first time, Under Build Select Project Only > Build Only hldll ( saves time compared to building all projects )

If you unloaded all other projects you can simply click under Build “Build Solution”, it will build the remaining loaded projects.

It should take a few minutes and you will probably get quite some warnings in the Output towards the bottom of the screen, This can be ignored (These errors occur due to changes to C++, Visual studio and the source code down through the years)

The SDK was developed originally in the mid-nineties and was not updated for new C++ standards when it was released on Github in 2013

Let’s disable these common warnings.

Right click on hldll and under Properties navigate to Configurations Properties -> C/C++ -> Advanced, (Make sure to set “All Configurations” so that it affects both Debug & Release)

For “Disable Specific Warnings“ add the following:

4028;4058;4996

Apply these settings and compile again. The warnings should no longer be present in the log.

You should see that the build has succeeded, You can now double check the generated hl.dll file’s timestamp to ensure that it created a fresh Library in the right location.

For me it can be located here:

D:\Programs\Steam\SteamApps\common\Half-Life\valve\dlls\

If all looks good we can now try and run the game from within Visual studio. Click on Local Windows Debugger to launch the game.

You will get the following error if Steam is not running:

Failed to Initialize authentication interface. Exiting...

Start up Steam, Half-Life should then run and you should be able to play through the original game without issue.

Hello Half-Life

So let’s do something to the code: Let’s add a message for when the player jumps.

Save the changes and build hldll, Then enter the game and jump. You should see in the top left hand corner the message “Hello Half-Life”

Post Build Notes

Printing to the Console Only:

If you want to print to the console and not the console & HUD as described above use the following commands

For printing to the server (hldll) console this is:

ALERT( at_console, "Hello Half-Life\n" );

The \n is important because it adds a line break so that the next call to this method doesn’t simply stick text to the end of your statement but rather creates its own newline.

The equivalent in the client (hl_cdll) is:

gEngfuncs.Con_Printf( "Hello Half-Life\n" );

Server Vs Client:

The Server (aka hldll) contains all the code that is common between all players, These include AI, Monsters, Items, Weapons, gamerules, entities, etc..

The Client (aka hl_cdll) contains all the code that is specific to each player, Things such as the HUD, events system, input processing, VGUI (MOTD, TFC team/class selection, command menu) as well as some items for rendering.

Release Binaries:

Build your binaries in release mode when you are making a final binary to distribute to friends or the community. To do this simply right click on the project, select Properties -> Configuration Manager (Top Right). In the window that appears select Release from the Top-Left drop down box. This sets the active configuration to Release as opposed to Debug which is the default setting. Close the window and compile the project again.

This will create a Release folder next to your Debug folder in the solution (code) directory. If you set your settings to affect all configurations as described earlier the newly compiled dll will have copied to the correct mod folder.

When compared to the Debug binaries the Release binaries are typically smaller in size and contain optimizations to improve performance at runtime.

Further Reading

Thanks

I want to extend my thanks to Shepard62700FR who provided some very helpful advice which I added to the tutorial.

I want to thank Malortie for his Visual Studio 2015 compatible version of the Half-Life SDK which I am sure many of you will find useful.

And finally I want to thank you the reader who took the time to read this, I hope this short tutorial helps you in some small way. If you find any issues or if you know of anything this tutorial should include please feel free to send an e-mail onto me concerning it.

If you do have a neck for coding and seeing issues others don't help him. He is enhancing Half-Life beyond everything I've seen to date. Fixing so many glitches Valve & Gearbox left etc. it's unbelievable. He is some sort of coding genius to give you a simple example. He is the 1st person ever in the History of Hl1 who successfully recreated the Op4 Rope. That was something of a holy grail for the hl1 coders. There are still plenty of issues to find in his code enhancements. Help him on the bug hunt if he succeeds Spirit Half-Life will be a trash can in comparison.

@Foxtrop: As far as I know you can. His intend is it to enhance Half-Life and then releasing what he has as some sort of Spirit like enhanced free to use build. However if I were you. I would wait not just for the rope but for all of his fixes. For instance there were also many enhancements he made for the weapons. Apparently some of them didn't even play their proper animations code wise. So things we barely or never saw before are suddenly fixed. Not to mention new additions to the engine such as velocity when you jump onto moving objects or op4 monster replica which are identical in their behavior and visuals to the original op4 etc.

It would be even better if you play test his builds and report bugs so he can fix em. We all will benefit from his work. Yes this entire remaining community would benefit, I'm sure of it. :)

I strongly suggest you read through the entire discussion Foxtrop. Unlike you I'm not a coder but even I find it extremely interesting what he is writing there.

As for the Rope you just can't copy and paste it as he changed the code. He uses Anglescript and a whole lot of new references. He practically re-codes Half-Life based upon the limitations of the engine itself of course.

Good Find! I must add it to sourcemodding's sdk and source code section. He is the same guy that made an up to date model viewer for Half-Life recently. Gonna put a pin in this and watch his progress :), Thanks.

Excellent tutorial, well written and easy to digest. A small side-note I wish to add however about Debug mode, is that it causes small sprites to appear above AI's heads.

Its just worth noting that for anyone who has compiled their code and wonders why
people are emitting sparks. Had this happen to me once and forgot it was due to
Debug mode. Needless to say I was worried for a bit.

I only tested this on SDK2.3 converted to VS2008 but I assume it still applies to the modern source code release.