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.
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.
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
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.
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).
cstrike/addons/eventscripts/myscript/myscript.py
Importantly, the script name must be the same as the folder it is in
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.
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.
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.
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.
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://python.eventscripts.com/
http://addons.eventscripts.com/
Popularity: 6%
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))
Hello…Thanks for the nice read, keep up the interesting posts..what a nice Friday
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.
wow nice! thanks
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
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
Thanks :P Very Nice :)
it worked ty