Page 1 of 1

Change event's name?

Posted: Mon Jan 05, 2015 4:17 pm
by Mahi
Hey there!
I was wondering if it's possible (or would be possible in near future) to change event's name, i.e. have a set_name method for events.

Syntax: Select all

@Event
def player_death(game_event):
game_event_kill = copy(game_event)
game_event_kill.set_int('victim', game_event.get_int('userid'))
game_event_kill.set_int('userid', game_event.get_int('attacker'))
game_event_kill.set_name('player_kill')


If this is not possible, would there be a workaround to easily create custom events (player_kill being just one example)?

Posted: Mon Jan 05, 2015 4:22 pm
by satoon101
I believe it is probably possible to change the name using memory hacking, but it would also be a very bad idea. Doing so could cause other scripts that use the same event to not have that event fire at all.

Creating and firing custom events, on the other hand, has already been written into the API, and would be a much better choice in this situation:
http://forums.sourcepython.com/showthread.php?545
http://wiki.sourcepython.com/pages/events.custom

Posted: Mon Jan 05, 2015 6:34 pm
by Ayuto
One thing you could do is to duplicate the event by using this method. https://github.com/Source-Python-Dev-Team/Source.Python/blob/master/src/core/modules/events/events_wrap_python.cpp#L260

Then you need to change the event's name (like Satoon mentioned).

Posted: Mon Jan 05, 2015 7:25 pm
by satoon101
As another example of a custom event, I will show how to use your specific situation:

Syntax: Select all

from events import Event
from events.custom import CustomEvent
from events.resource import ResourceFile
from events.variable import BoolVariable
from events.variable import ShortVariable
from events.variable import StringVariable


class Player_Kill(CustomEvent):

victim = ShortVariable('Userid of the player who died.')
userid = ShortVariable('Userid of the player who killed the victim.')
headshot = BoolVariable("True if the kill shot was to the victim's head.")
weapon = StringVariable('The weapon that caused the kill shot.')

resource = ResourceFile('my_events', Player_Kill)

resource.write()
resource.load_events()

@Event
def player_death(game_event):
new_event = Player_Kill()
new_event.victim = game_event.get_int('userid')
new_event.userid = game_event.get_int('attacker')
new_event.headshot = game_event.get_bool('headshot')
new_event.weapon = game_event.get_string('weapon')
new_event.fire()


@Event
def player_kill(game_event):
...

Posted: Tue Jan 06, 2015 5:14 pm
by Mahi
Thanks a lot for help :) Got it working using Satoon's example.