My First Script

From AlliedModders Wiki
Jump to: navigation, search

Intro

This is a Hello World type plugin tutorial. Every beginner should learn these first baby steps. The full code is at the bottom. Now let's begin.

Includes

We first need to have our include file added. This file is located in the addons/sourcemod/scripting/include/ . Includes are your best friend. They give you access to all functions Sourcemod and its Module Extensions allow! Note: This always goes at the top of the entire source code.

#include <sourcemod>

Plugin Info

Next we give our information, so people who download know who made it and what version they have.

public Plugin:myinfo = {
	name = "Hello Gaben",
	author = "AlliedModders, LLC",
	description = "Hi",
	version = "1.0",
	url = "http://www.sourcemod.net/"
};

Initialize

This function will be used in nearly every plugin available. It is your friend. This runs one time when your plugin is loaded. You will use it to register many of SourceMod's capabilities including Custom Console Commands, CVARs, Natives, and Forwards.

Here I just ran a simple function I created to print a LogMessage to our log files in sourcemod/logs/ folder.

public OnPluginStart() 
{
	SayHello();
}

My Function

Here is the function ran at OnPluginStart(). Very simple and it uses the LogMessage function located in sourcemod.inc.

//Print Message to LogFile
SayHello()
{
	LogMessage("Hello Gaben | Time: %i", GetTime()); 
}

Conclusion

Sourcemod scripting has been extended in syntax compared to AMXX. This allows for better organization. However there is the learning curve of learning the syntax for each corresponding function or variable. Study the includes in the scripting/includes folder and you should be fine. All functions are in there so make them your friend!

Here is the code used in its entirety.

/* Hello Gaben */
 
#include <sourcemod>
 
public Plugin:myinfo = {
	name = "Hello Gaben",
	author = "AlliedModders, LLC",
	description = "Hi",
	version = "1.0",
	url = "http://www.sourcemod.net/"
};
 
public OnPluginStart() 
{
	SayHello();
}
 
SayHello()
{
	LogMessage("Hello Gaben | Time: %i", GetTime()); 
}