Creating your first mod for Counter Strike Source
Daniel - Nov 15, 2007 - Games Tech CodeStarting projects for anything new can be fairly daunting - but if you're a fan of Valve's Source games, you can be writing addons for their games within a week. Mattie's Eventscripts makes writing server addons a breeze - and if you want to see the kind of things you can create, look no further than my post on CS Source Mods
-
Brush up on Python
If this scares you off, hold for a moment. Even if you don't know how to code in Python, it's one of the easiest languages to learn - even if you have little or no coding experience whatsoever. If you can understand simple logic, and have a passion for problem solving, you'll not have much difficulty at all getting to grips with it - and if you're looking for a place to start, check out Getting into coding in 8 steps.
Eventscripts actually comes with two languages built in - I've chosen to write about Python because in my opinion it's both easier and more readable than the inbuilt shell language - and if you're starting off without knowing either language, you might as well begin with python. If you already have experiences with interpreted languages, you'll find Python infinitely easier to follow anyway - plus it's orders of magnitude faster than the built in language, and has much more power.
-
Set up a Source Server
If you're already an admin of a server, with FTP access, this makes things easier - but even still, you can quickly and easily set up your own dedicated server on your home PC. There are a number of ways of doing this - and I'd recommend the first.
- Download and install SRCDS from http://www.srcds.com
- Go to Steam > Tools > Dedicated Server
- Select 'Create a Server' in the in-game CSS menu
- Rent a server on a monthly basis, with FTP access
You can get away without a server if you're writing the most basic of scripts, but without somewhere to test what you write, you'll be in difficulty when you come to release a working version. There is a great tutorial for everything you need to do while setting up SRCDS here, I recommend you follow this as best you can.
-
Download and set up Eventscripts 2.0
Crucially, it's only versions 2.0 and upwards of Eventscripts that include Python, so make sure you get a copy here. After that you'll need to unzip it to the right location: if you're using SRCDS, you need to go to
srcds/cstrike/addonsand make sure you unzip the files to the directory structure they follow in the zip file. There is an awesome screencast with explicit instructions for how to do this here. -
Write your first script!
-
First you want to create a file on your server called
cstrike/addons/eventscripts/myscript/myscript.py. Importantly, the script name must be the same as the folder it is in -
The first line in your script should be
import es. This is vital for interacting with the game; with the es module contains all of the functions and methods you will need for:- Displaying outputs in the form of text messages, menus, effects, sounds
- Getting information about the server and the users on it
And a whole load of other things. It is extremely unlikely you will not need this, as without it you will be unable to interact with the server in any meaningful way.
-
Functions can act as in game 'events' - these are called when things happen in game, and let you run code when they do
For example:
def player_jump(event_var): es.msg(event_var['es_username'] + ' has jumped')
Let's analyse each section of this:
-
def player_jump(event_var):This registers the
player_jumpevent to be called by your script. Obviously, this is called whenever any player jumps. The 'event_var' is the variable eventscripts passes data about the event to, in the form of a dictionary, for example, who jumped, where did they jump, what is their name. All of the available events can be seen here -
es.msgThis is a function which displays text in the chat area of the screen for all players. All of the available funtions can be seen here.
-
event_var['es_username']This is, very simply, the player name associated with the event.
-
-
Variables exist for the full duration of the time a script is loaded
This means that they are ideal for small data storage: global variables exist until a script is unloaded or the server is rebooted or shut down. Gobal variable scope, as would be expected, is limited to the script itself.
-
Scripts must be 'loaded' before they take effect.
Very simply, this is so that Eventscripts can tell which scripts to call, which events to register, and so on. To load a script, simple type 'es_load myscript' into your SRCDS console; or, if you want a script to load every time you run your server, create a file called
cstrike/cfg/autoexec.cfgand place the linees_load myscriptsomewhere in there.
-
-
Documentation and Examples
There's only so much that can be said in the space of one post: in order to fully realise the potential of eventscripts, you need to read up on the forums, see some examples, and understand how everything ties in. I recommend the following links as a means of getting started:

Rick Hull
If you want to beef up the content:
Step 4.5
Please show the standard contents of an event var. This will give a first clue as to the available functionality.
Also, are there other forms to the API? (i.e. not `def event_name(event_var))
Brad Garrett
Hello...Thanks for the nice read, keep up the interesting posts..what a nice Friday
Daniel
Rick: the contents of each event_var for a given event are documented here:
http://www.eventscripts.com/pages/Category:Valve_Events
The only way to register events in espython is to create a function with the event name; there is another included language known as ESShell, but it's pretty much superseded by python in every way, which is why I chose not to write about it.
mike
wow nice! thanks
mike
This code wouldn't work...I had to indent like so:
def player_jump(event_var): es.msg(event_var['es_username'] + ' has jumped')
else it won't work. Don't know if thats just me or what, but great tutorial i must say
Daniel
Yeah, mike - sorry about that, I've yet to get indentation working properly for code on the site.
I posted up a second guide with a bit more detail and extra examples, if anyone is interested in learning more:
http://bluesuncorp.co.uk/2007/12/16/using-eventscripts-to-mod-cs-source
SoMeOnE
Thanks :P Very Nice :)
infoman
it worked ty