Change event's name?

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Change event's name?

Postby Mahi » Mon Jan 05, 2015 4:17 pm

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)?
User avatar
satoon101
Project Leader
Posts: 2703
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Jan 05, 2015 4:22 pm

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
Image
User avatar
Ayuto
Project Leader
Posts: 2209
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Mon Jan 05, 2015 6:34 pm

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).
User avatar
satoon101
Project Leader
Posts: 2703
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Jan 05, 2015 7:25 pm

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):
...
Image
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Tue Jan 06, 2015 5:14 pm

Thanks a lot for help :) Got it working using Satoon's example.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 149 guests