post

Code

StumbleUpon Digg Reddit Technorati

Writing your first mod for Counter Strike Source

ZebraLogoSmall

Starting 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.

1. 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 coming to it without knowing either, you might as well begin with it. If you already have experiences with interpreted languages, you'll find Python infinitely easier to follow anyway - plus it's magnitudes faster than the built in language, and has more power.

2. 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 two ways of doing this - and I'd recommend the first method over the second.

or

  • Go to Steam > Tools > Dedicated Server

You can get away without a server for 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.

3. Download and set up Eventscripts 2.0 (or greater)

Crucially, it's only versions 2.0 and upwards of Eventscripts that include Python, so make sure you get a copy from here. After that you'll need to unzip it to the right location: if you're using SRCDS, you need to go to:

C:\srcds\cstrike\addons

And 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.

Don't forget to create a file called:

cstrike/cfg/autoexec.cfg

And place the line 'mattie_eventscripts 1' somewhere in there, to tell eventscripts to enable events (this is very important).

4. 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 as follows:

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 event 'player_jump' 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.msg(

This 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.cfg

And place the line 'es_load myscript' somewhere in there.

5. 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:

http://eventscripts.com/

http://python.eventscripts.com/

http://forums.mattie.info/

http://addons.eventscripts.com/

Popularity: 4%

1 Star2 Stars3 Stars4 Stars5 Stars
3 votes, average: 4.33 out of 5
Loading ... Loading ...

  • Using Eventscripts to mod CS Source
  • Eventscripts 2.0 - now with Python
  • Greasemonkey Reddit Filter
  • Discussion

    9 comments for “Writing your first mod for Counter Strike Source”

    1. 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))

      Posted by Rick Hull | November 16, 2007, 1:27 am
    2. Hello…Thanks for the nice read, keep up the interesting posts..what a nice Friday

      Posted by Brad Garrett | November 16, 2007, 7:08 am
    3. 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.

      Posted by Daniel | November 16, 2007, 9:25 am
    4. wow nice! thanks

      Posted by mike | January 23, 2008, 6:28 am
    5. 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

      Posted by mike | January 24, 2008, 12:14 am
    6. 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

      Posted by Daniel | January 24, 2008, 12:51 pm
    7. Thanks :P Very Nice :)

      Posted by SoMeOnE | June 6, 2008, 6:37 pm
    8. […] last article detailed how to write a very simple mod for Counter Strike Source servers; here, I plan to go into […]

      Posted by Using Eventscripts to mod CS Source | BlueSunCorp | June 17, 2008, 4:41 pm
    9. it worked ty

      Posted by infoman | July 2, 2008, 11:52 pm

    Post a comment

    Categories