Difference between revisions of "Introduction to SourceMod Plugins"

From AlliedModders Wiki
Jump to: navigation, search
(New page: This guide will give you a basic introduction to writing a SourceMod plugin. If you are not familiar with the SourcePawn language, it is recommended that you at least briefly read the...)
 
m
Line 135: Line 135:
 
=ConVars=
 
=ConVars=
 
ConVars, also known as cvars, are global console variables in the Source engine.  They can have integer, float, or string values.  ConVar accessing is done through [[Handles (SourceMod Scripting)|Handles]].  Since ConVars are global, you do not need to close ConVar Handles (in fact, you cannot).
 
ConVars, also known as cvars, are global console variables in the Source engine.  They can have integer, float, or string values.  ConVar accessing is done through [[Handles (SourceMod Scripting)|Handles]].  Since ConVars are global, you do not need to close ConVar Handles (in fact, you cannot).
 +
 +
The handy feature of ConVars is that they are easy for users to configure.  They can be placed in any .cfg file, such as <tt>server.cfg</tt> or <tt>sourcemod.cfg</tt>.  To make this easier, SourceMod has an [http://docs.sourcemod.net/api/index.php?fastload=show&id=558& AutoExecConfig()] function.  This function will automatically build a default .cfg file containing all of your cvars, annotated with comments, for users.  It is highly recommend that you call this if you have customizable ConVars.
  
 
Let's extend your example from earlier with a new ConVar.  Our ConVar will be <tt>sm_myslap_damage</tt> and will specify the default damage someone is slapped for if no damage is specified.
 
Let's extend your example from earlier with a new ConVar.  Our ConVar will be <tt>sm_myslap_damage</tt> and will specify the default damage someone is slapped for if no damage is specified.
Line 145: Line 147:
  
 
sm_myslap_damage = CreateConVar("sm_myslap_damage", "5", "Default slap damage")
 
sm_myslap_damage = CreateConVar("sm_myslap_damage", "5", "Default slap damage")
 +
AutoExecConfig(true, "plugin_myslap")
 
}
 
}
  
Line 154: Line 157:
 
/* The rest remains unchanged! */
 
/* The rest remains unchanged! */
 
</pawn>
 
</pawn>
 +
 +
[[Category:SourceMod Development]]

Revision as of 09:13, 15 November 2007

This guide will give you a basic introduction to writing a SourceMod plugin. If you are not familiar with the SourcePawn language, it is recommended that you at least briefly read the Introduction to SourcePawn article.

For information on compiling plugins, see Compiling SourceMod Plugins. The author of this article uses Crimson Editor to write plugins. Other possibilities are PSPad, UltraEdit, Notepad++, TextPad, or any other text editor you're comfortable with.

Plugin Structure

Almost all plugins follow have the same three elements:

  • Includes - Allows you to access the SourceMod API, and if you desire, API from external SourceMod extensions/plugins.
  • Info - Public information about your plugin.
  • Startup - A function which performs start-up routines in your plugin.

A skeletal plugin structure looks like:

#include <sourcemod>
 
public Plugin:myinfo =
{
	name = "My First Plugin",
	author = "Me",
	description = "My first plugin ever"
	version = "1.0.0.0",
	url = "http://www.sourcemod.net/"
}
 
public OnPluginStart()
{
}

The information portion is a special syntax construct. You cannot change any of the keywords, or the public Plugin:myinfo declaration. The best idea is to copy and paste this skeletal structure and modify the strings to get started.

Includes

Pawn requires include files, much like C requires header files. Include files list all of the structures, functions, callbacks, and tags that are available. There are three types of include files:

  • Core include files, which is sourcemod.inc and anything it includes. These are all provided by SourceMod's Core.
  • Extension include files, which if used, will add a dependency against a certain extension.
  • Plugin include files, which if used, will add a dependency against a certain plugin.

Include files are loaded using the #include compiler directive.


Commands

Our first example will be writing a simple admin command to slap a player. We'll continue to extend this example with more features until we have a final, complete result.

Declaration

First, let's look at what an admin command requires. Admin commands are registered using the RegAdminCmd function. They require a name, a callback function, and default admin flags.

The callback function is what's invoked every time the command is used. Click here to see its prototype. Example:

public OnPluginStart()
{
	RegAdminCmd("sm_myslap", Command_MySlap, ADMFLAG_SLAY)
}
 
public Action:Command_MySlap(client, args)
{
}

Now we've successfully implemented a command -- though it doesn't do anything yet. In fact, it will say "Unknown command" if you use it! The reason is because of the Action tag. The default functionality for entering console commands is to reply that they are unknown. To block this functionality, you must return a new action:

public Action:Command_MySlap(client, args)
{
	return Plugin_Handled;
}

Now the command will report no error, but it still won't do anything.

Implementation

Let's decide what the command will look like. Let's have it act like the default sm_slap command:

sm_myslap <name|#userid> [damage]

To implement this, we'll need a few steps:

  • Get the input from the console. For this we use GetCmdArg().
  • Find a matching player. For this we use FindTarget().
  • Slap them. For this we use SlapPlayer(), which requires including sdktools, an extension bundled with SourceMod.
  • Respond to the admin. For this we use ReplyToCommand().

Full example:

#include <sourcemod>
#include <sdktools>
 
public Plugin:myinfo =
{
	name = "My First Plugin",
	author = "Me",
	description = "My first plugin ever"
	version = "1.0.0.0",
	url = "http://www.sourcemod.net/"
}
 
public OnPluginStart()
{
	RegAdminCmd("sm_myslap", Command_MySlap, ADMFLAG_SLAY)
}
 
public Action:Command_MySlap(client, args)
{
	new String:arg1[32], String:arg2[32]
	new damage
 
	/* Get the first argument */
	GetCmdArg(1, arg1, sizeof(arg1))
 
	/* If there are 2 or more arguments, and the second argument fetch 
	 * is successful, convert it to an integer.
	 */
	if (args >= 2 && GetCmdArg(2, arg2, sizeof(arg2)))
	{
		damage = StringToInt(arg2)
	}
 
	/* Try and find a matching player */
	new target = FindTarget(client, arg1)
	if (target == -1)
	{
		/* FindTarget() automatically replies with the 
		 * failure reason.
		 */
		return Plugin_Handled;
	}
 
	SlapPlayer(target, damage)
 
	new String:name[MAX_NAME_LENGTH]
 
	GetClientName(target, name, sizeof(name))
	ReplyToCommand(client, "[SM] You slapped %s for %d damage!", name, damage)
 
	return Plugin_Handled;
}

For more information on what %s and %d are, see Format Class Functions. Note that you never need to unregister or remove your admin command. When a plugin is unloaded, SourceMod cleans it up for you.


ConVars

ConVars, also known as cvars, are global console variables in the Source engine. They can have integer, float, or string values. ConVar accessing is done through Handles. Since ConVars are global, you do not need to close ConVar Handles (in fact, you cannot).

The handy feature of ConVars is that they are easy for users to configure. They can be placed in any .cfg file, such as server.cfg or sourcemod.cfg. To make this easier, SourceMod has an AutoExecConfig() function. This function will automatically build a default .cfg file containing all of your cvars, annotated with comments, for users. It is highly recommend that you call this if you have customizable ConVars.

Let's extend your example from earlier with a new ConVar. Our ConVar will be sm_myslap_damage and will specify the default damage someone is slapped for if no damage is specified.

new Handle:sm_myslap_damage = INVALID_HANDLE
 
public OnPluginStart()
{
	RegAdminCmd("sm_myslap", Command_MySlap, ADMFLAG_SLAY)
 
	sm_myslap_damage = CreateConVar("sm_myslap_damage", "5", "Default slap damage")
	AutoExecConfig(true, "plugin_myslap")
}
 
public Action:Command_MySlap(client, args)
{
	new String:arg1[32], String:arg2[32]
	new damage = GetConVarInt(sm_myslap_damage)
 
	/* The rest remains unchanged! */