Server plugins: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(moved function list to IServerPluginCallbacks)
(→‎Coding: get players)
Line 62: Line 62:


[[ConVar]]s let you specify variables that users can use to configure the behavior of your plugin. [[ConCommand]]s allow the creation of commands that your plugin provides. Creating both is easy and self contained, the code snippet below creates a new command <code>empty_version</code> and a new variable <code>plugin_empty</code>. These commands can be run from the server or client, you should use the index provided by <code>SetCommandClient</code> to determine the source of the command.
[[ConVar]]s let you specify variables that users can use to configure the behavior of your plugin. [[ConCommand]]s allow the creation of commands that your plugin provides. Creating both is easy and self contained, the code snippet below creates a new command <code>empty_version</code> and a new variable <code>plugin_empty</code>. These commands can be run from the server or client, you should use the index provided by <code>SetCommandClient</code> to determine the source of the command.
<pre>
 
<source lang=cpp>
CON_COMMAND( empty_version, "prints the version of the empty plugin" )
CON_COMMAND( empty_version, "prints the version of the empty plugin" )
{
{
Line 69: Line 70:


static ConVar empty_cvar("plugin_empty", "0", 0, "Example plugin cvar");
static ConVar empty_cvar("plugin_empty", "0", 0, "Example plugin cvar");
</pre>
</source>
 
=== Other tricks ===
 
Get player entities:
 
<source lang=cpp>
static CGlobalVars *gpGlobals;
static IVEngineServer *engine;


===Release your plugin===
CON_COMMAND( list_players, "Prints the name of each connected player" )
When ready for public consumption or beta testing, announce your server plugin at a community website like [http://www.sourceplugins.com SourcePlugins].
{
for (int i=1;i<gpGlobals->maxClients;i++) // EntIndex 0 is worldspawn, after which come the players
{
IPlayerInfo *playerinfo = playerinfomanager->GetPlayerInfo(engine->PEntityOfEntIndex(i));
if (playerinfo)
{
Msg(playerinfo->GetName());
Msg("\n");
}
}
}
</source>


[[Category:Programming]]
[[Category:Programming]]

Revision as of 12:30, 10 September 2010

Template:Otherlang2

Server plugins are code libraries that modify the behaviour of dedicated servers. They are used to provide everything from maintenance tools to additional gametypes.

Warning.pngWarning:Plugins do not work with the GUI dedicated servers available through Steam. Only servers installed through the HLDS Update Tool can use them.

Installing

Source automatically loads plugins defined in files matching <game>\addons\*.vdf*. These files should be formatted like this:

Plugin
{
    file <path to plugin>
}
  • On Windows, do not add the file extension. On Linux, do.
  • The path value is relative to the engine's binary folder (not the game's), so if you want to load a plugin from TF2's addons folder it will need to read ..\tf\addons\plugin.

Managing

The following console commands are provided:

  • plugin_print (particularly useful for confirming that your plugin is installed correctly)
  • plugin_load
  • plugin_unload
  • plugin_pause (the engine stops sending callbacks to the plugin)
  • plugin_unpause
  • plugin_pause_all
  • plugin_unpause_all

Compiling

You can find a sample plugin project at src\utils\serverplugin_sample\. The Orange Box version is good for both Source 2007 and 2009.

Windows
Build with the Visual Studio project provided; see Compiling under VS2008 or Compiling under VS2010 for help with upgrading
Linux
Execute make plugin from src/linux_sdk/
Mac
Not possible yet

To debug your plugin, you must launch the server with -allowdebug.

Coding

Plugins work by exposing to the engine an object inheriting from IServerPluginCallbacks and IGameEventManager2. The engine will send function calls into the plugin at various times, some hard-coded and some determined by the programmer, and the plugin can react to each of those events by running its own code. In some cases (e.g. player connection) changing the outcome of the original function is also possible.

Listening to Events

The IGameEventManager2 (IGameEventManager was used previously) interface allows a plugin to listen to game events. Game events are fired by a Mod when things of interest happen, like a player dying or the bomb being planted. IGameEventManager2::AddListener should be called to subscribe to particular game events. It must be called for each event that your listener wishes to be aware of. FireGameEvent is then called in your plugin when an one of the listened for events happen. The data contained in each event is described by event configuration files, in particular:

  • hl2/resource/serverevents.res
  • hl2/resource/GameEvents.res
  • <mod dir>/resource/ModEvents.res

Creating ConVars and Commands

ConVars let you specify variables that users can use to configure the behavior of your plugin. ConCommands allow the creation of commands that your plugin provides. Creating both is easy and self contained, the code snippet below creates a new command empty_version and a new variable plugin_empty. These commands can be run from the server or client, you should use the index provided by SetCommandClient to determine the source of the command.

CON_COMMAND( empty_version, "prints the version of the empty plugin" )
{
        Msg( "Version:1.0.0.0\n" );
}

static ConVar empty_cvar("plugin_empty", "0", 0, "Example plugin cvar");

Other tricks

Get player entities:

static CGlobalVars *gpGlobals;
static IVEngineServer *engine;

CON_COMMAND( list_players, "Prints the name of each connected player" )
{
	for (int i=1;i<gpGlobals->maxClients;i++) // EntIndex 0 is worldspawn, after which come the players
	{
		IPlayerInfo *playerinfo = playerinfomanager->GetPlayerInfo(engine->PEntityOfEntIndex(i));
		if (playerinfo)
		{
			Msg(playerinfo->GetName());
			Msg("\n");
		}
	}
}