Events

Introduction

Events are probably one of the most important tools when you want to write aplugin for a game. The game fires them in specific situations (e.g. when aplayer was hurt, died or the round started) and Source.Python is able tolisten to them. An event might provide various event variables, which containmore information about the event. E.g. the event “player_hurt” always providesthe user ID of the attacker and the user ID of the victim, which can be usedto interact with these players. A basic example could look like this:

fromeventsimportEvent@Event('player_hurt')defon_player_hurt(game_event):# Retrieve the user ID of the victimuserid=game_event['userid']# Retrieve the user ID of the attackerattacker=game_event['attacker']print('user ID "{}" was hurt by attacker ID "{}"'.format(userid,attacker))

In order to listen to an event, you have to import theevents.Eventdecorator. The decorator requires a name, which is the name of the event youwant to listen to. All event callbacks require exactly one argument, which isaevents.GameEvent object. This object can be used to access thespecific event variables.

Note

events.GameEvent.__getitem__() will raise aKeyError if youare trying to access an event variable that doesn’t exist.

Note

events.GameEvent.__getitem__() will return the value in the typethat has been defined in the event resource files.

Warning

All games provide different events. Some events might exist on all games,but they can still provide different event variables.

Printing event variables

Sometimes you might want to print all event variables and their values.Fortunately, there is aevents.GameEvent.variables attribute, whichcontains all event variables. You can use this little snippet to display them.

fromeventsimportEventfrompprintimportpprint@Event('player_hurt')defon_player_hurt(game_event):pprint(game_event.variables.as_dict())

Available events

Below you can find a list of games and their supported events.