This article relates to the game "Left 4 Dead". Click here for more information.
This article relates to the game "Left 4 Dead 2". Click here for more information.
This article's documentation is for anything that uses the Source engine. Click here for more information.

Left 4 Dead Plugins

From Valve Developer Community
Jump to navigation Jump to search
English (en)Translate (Translate)

Left 4 Dead series In this tutorial you will learn how to compile Server Plugins for the Left 4 Dead series. This tutorial assumes you are well acquainted with both C++ and your chosen compiler; however, if you have ever compiled a program before you should be able to follow the tutorial step-by-step no problem.
If you don't have a compiler installed, you can download Visual C++ Express 2008 for free from Microsoft.

Creating Your Project

1. Under Visual Studio Express 2008 Select File > New > Project.
2. Chose Win32 for the project Type and Win32 Project for the template.
3. When the wizard appears click on application settings and chose DLL as the application type and Select Empty Project under Additional Options (Or you'll be sorry).

Getting the Left 4 Dead SDK

The Left4Dead SDK.
  1. You'll want to download the reserve-engineered Left 4 Dead SDK from AlliedModders, as Valve has not released a new SDK to create Left 4 Dead Plugins.
  2. Open the folder containing your new project. It usually looks like: C:\Documents and Settings\User Name\My Documents\Visual Studio 2008\Projects\Project Name\Project Name (This is the folder containing your .vcproj file.)
    Ex: C:\Documents and Settings\Andy\My Documents\Visual Studio 2008\Projects\Left4Dead Plugin\Left4Dead Plugin
  3. You'll want to copy the lib and public folders from the SDK zip into your project folder.
  4. Also copy serverplugin_empty.cpp from utils\serverplugin_sample in the SDK zip directly into the project folder.
  5. Addtionally you may want to copy the game folder from the zip to the project folder but more on that later.
  6. To reduce the size of your project folder you can delete all the .cpp files from the SDK (keeping serverplugin_empty.cpp of course).
  7. Click and drag serverplugin_empty.cpp into your visual studio project. Drop it on to the name of your project in the Solution Explorer.

Adjusting the Project Settings

Project Properties.
General Properties.
C/C++ Properties.
The Addtional Include Directories.
Linker Settings.
Linker Settings.

1. Right-Click on your project in the Solution Explorer and chose Properties.
2. Under Configuration chose All Configurations.
3. Expand Configuration Properties, click on General and change the Character Set to Use Multi-Byte Character Set.
4. Expand C/C++, click on General and add public;public\tier0;public\tier1;public\tier2;public\tier3;game;game\shared;game\server;game\client to Addtional Include Directories.
5. Expand Linker , click on General and add lib\public to Additional Library Directories.
6. Click on Input and add mathlib.lib tier0.lib tier1.lib tier2.lib vstdlib.lib to Additional Dependencies.
7. Add libc;libcd;libcmt to Ignore Specific Library.

Setting the Project to Automatically Copy the Plugin to Left 4 Dead

Configuring the project to copy automatically.

1. Expand Custom Build Step, and Enter copy /y "$(TargetDir)$(TargetFileName)" "C:\Program Files\Steam\steamapps\common\left 4 dead\Left4Dead\addons\$(TargetFileName)" as the Command Line.
2. Don't for get to add "C:\Program Files\Steam\steamapps\common\left 4 dead\Left4Dead\addons\$(TargetFileName)" to Outputs or the plugin will not copy.
3. Optionally, add a description.

Making the Code Compile

1. Press Ctrl+H and find and replace all occurences of IGameEventManager with IGameEventManager2.
2. Change the IGameEventListener line 50 to IGameEventListener2.
3. Change the KeyValues on line 70 to IGameEvent.
4. Change line 185 from gameeventmanager->AddListener( this, true ); to gameeventmanager->AddListener( this, "player_say", true ); 5. Remove lines 201-204. (This tutorial does cover the bot code in the example plugin, it is probably outdated anyway).
6. Change the KeyValues on line 403 to IGameEvent.
7. Remove the engine-> from lines 257 and 295.
somewhere under line 36 (CGlobalVars *gpGlobals = NULL;) but before line 251 (void CEmptyServerPlugin::ClientSettingsChanged( edict_t *pEdict )) add this code:

inline int IndexOfEdict(const edict_t *pEdict)
{
	return (int)(pEdict - gpGlobals->baseEdict);
}
inline edict_t *PEntityOfEntIndex(int iEntIndex)
{
	if (iEntIndex >= 0 && iEntIndex < gpGlobals->maxEntities)
	{
		return (edict_t *)(gpGlobals->baseEdict + iEntIndex);
	}
	return NULL;
}


8. You may also want to compile your plugin in Release mode so it loads in to Left 4 Dead easily. (In Debug mode you must add -allowdebug to your Left 4 Dead launch options.)
9. Build your plugin!

Installing the plugin into Left 4 Dead

1. Create file named C:\Program Files\Steam\steamapps\common\left 4 dead\Left4Dead\addons\my plugin.vdf containing this text:

"Plugin"
{
	"file"	"..\left4dead\addons\[Plugin Name].dll"
}

(this can be done by opening the file with notepad but make sure the file is .vdf not .txt or .vdf.txt, also the name itself it not important as Left 4 Dead reads all .vdf files in the addons folder.)

2. If your plugin does not automatically, copy your plugin .dll into your addons folder.
3. Test your plugin by issuing the command empty_version in the console.

Adding Enhanced Entity Interaction

Remember the game folder from before? That can be used to give your plugin access to the CBaseEntity and CBasePlayer classes if you chose to use them.
The easiest way to add this is to include the base server file in your serverplugin_empty.cpp.

#define GAME_DLL
#include "server\cbase.h"
#undef GAME_DLL

the #define GAME_DLL tells the compiler you want to use the server code and is undefined afterward because this is not actually a server dll, its a plugin for the server dll.