sourcemod.events — Game Events

The Source Engine defines events that are fired when certain things happen. For example, the event player_shoot is fired when a player fires their weapon in Counter-Strike: Source.

Module Contents

Event Hook Mode Description
EventHookMode_Pre
Hook callback fired before the event is fired.
EventHookMode_Post
Hook callback fired after the event is fired.
EventHookMode_PostNoCopy
Hook callback fired after event is fired, but event data is not copied.
create(name[, force=False])

Creates a game event. If force is True, it forces the event to be created even if it’s not being hooked. Note that this will not force the event to be created if the event doesn’t exist at all.

Returns an Event object that represents the game event created, or None if the event does not exist.

hook(event, callback[, mode=EventHookMode_Post])

Hooks a game event. This raises a ViperError if the game event does not exist. mode expects an EventHookMode constant.

callback should be a callable that accepts two arguments: an Event object, and the event name as a string – in that order.

unhook(event, callback[, mode=EventHookMode_Post])

Unhooks the specified callback from a game event. Raises a ViperError if the specified event does not exist or the callback was invalid. mode expects an EventHookMode constant.

class Event

Event Objects

Event fields are accessed using the dictionary syntax:

evt['field']
evt['field'] = 'value'

Viper parses the mod’s resource/modevents.res file for every field and type for each game event. That allows Viper to return evt['field'] as the correct type for that field. So if in modevents.res there is a “round_start” event with a field “fraglimit” of type “short”, Viper will return a Python int for evt['fraglimit'].

Since Viper knows the fields each game event has, it can provide length:

len(evt) # The number of fields `evt` has

To iterate over the fields of an event, use the get_fields function to retrieve the fields. Then iterate over the dictionary however you please:

fields = evt.get_fields()
for field in fields.iterkeys():
    print "%s: %s" % (field, evt[field])
for field,type in fields.iteritems():
    print "%s is of type %s" % (field, type)
Event.dont_broadcast

Read-only. Whether or not this event will be broadcast to players.

Event.name

Read-only. The name of this game event.

Event.cancel()

Cancels this created event.

Event.fire([dont_broadcast=False])

Fires a created event. If dont_broadcast is True, the event is broadcast to the clients.

Event.get_fields()

Retrieves the fields of a game event as seen in modevents.res. They are returned as a dictionary, with the keys being the field names and the values being their type. For example:

>>> evt.get_fields()
{'player': 'byte', 'quality': 'byte', 'item': 'string', 'method': 'byte', 'propername': 'bool'}
Event.is_empty(field)

Returns whether or not an event field has an empty value.

Note

This only tells if an event field has a value. This means that if you create an event, none of its fields will have values, and this function will return True for all of them.

Table Of Contents

Previous topic

sourcemod.entity — Entity manipulation.

Next topic

sourcemod.forwards — Collections of Callbacks

This Page