Using Eventscripts to mod CS Source

Daniel - Dec 16, 2007 - Games Code

My last article detailed how to write a very simple mod for Counter Strike Source servers; here, I plan to go into more detail. I will assume basic knowledge of Python, and an understanding of how to create a simple addon on your source server - essentially I plan to discuss Eventscripts' API and features.

Userids

Each player that joins a server is allocated a unique user id. This userid is used as a key to identify users, and a given e player's name, steamid, health -- almost any information on that player -- will be available using their userid. For example, to find a player's name, one might simply use the 'getplayername' method of the es module:

es.getplayername(userid)

es

Importing this module is crucial to displaying any kind of output on your server, and registering the events in your addon. Es contains methods which can be used to:

  • Display text or a menu on a player's screen
  • Send shell commands to the server console
  • Create visual effects
  • Affect the player (e.g. change their position/view)

And a whole load more. Detailed documentation for each of the methods of es can be found here

Events

Events are almost crucial to achieving anything with Valve's source engine. In the majority of cases, events are where you will get your inputs, whether it is:

  • User inputs (e.g. Console commands, chat messages, menu key-presses)
  • Actions the player performs (e.g. jumping, shooting, taking damage etc)
  • Important checkpoints in the game's progress (e.g. Map start, round start, bomb planted)

Events are registered simply by creating a function in your script with the specific name of the event you wish to hook. All of the available events are documented here.

These functions take one argument; traditionally named 'event_var', this is a variable which will store a dictionary containing details about the event when it is called. For example, the key 'player_name' will return the name of the player that the event is called by - if the event is player specific.

def player_spawn(event_var):
  es.tell(event_var['userid'], 'Hello, world')

An Example Addon

As an example of a very basic addon, the following script logs the number of times a player says anything in chat, in a given session, and returns this value to them when they type 'stats'.

import es
stats = {}

def player_connect(event_var):
  stats[event_var['userid']] = 0

def player_say(event_var):
  userid = event_var['userid']
  if event_var['text'] == 'stats':
     es.tell(userid, 'You have sent ' + str(stats[userid]) + ' messages during this session'
  else:
    stats[userid] += 1

Docs

Once you've got to grips with the basics described above, the rest is simple to pick up. All of the documentation you could possibly need can be located in the following places:

http://eventscripts.com

http://python.eventscripts.com

http://forums.mattie.info

http://addons.eventscripts.com

Name