Skip to main content

Introduction to GoldSrc Programming – Setting up Visual Studio

Description

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

Version

2016.1

Support
Software

Windows, Half-Life SDK, Microsoft Visual Studio [2010, 2012, 2013 +], Steam, Half-Life

Learn how to setup Visual Studio to work with GoldSrc

Installation

Install Visual Studio (2010, 2012, 2013 or 2015) ( you are required to create a free account )

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.

To do this locate player.cpp inside hldll > Source Files > dlls in Visual Studio



Ctrl – F to Find the word “Jump”

Locate the following:

case PLAYER_JUMP:
m_IdealActivity = ACT_HOP;
break;

Let’s append a line that outputs a custom message when the player jumps in-game

case PLAYER_JUMP:
UTIL_ClientPrintAll(HUD_PRINTNOTIFY, UTIL_VarArgs("Hello Half-Life")); // Message
m_IdealActivity = ACT_HOP;
break;

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.

You can learn more about this kind of game architecture here.

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.

References

Authors Note

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.

The support thread for this tutorial can be found over at the forums.

Kind Regards
Cathal McNally