Events vs. Listeners

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Events vs. Listeners

Postby BackRaw » Thu Jun 02, 2016 9:39 am

Hi,

to this day I'm not quite sure when to favor events over listeners or vice versa.
For example:

Syntax: Select all

from listeners import OnClientDisconnect

@OnClientDisconnect
def on_client_disconnect(index):
print(index)
vs

Syntax: Select all

from events import Event
from players.helpers import index_from_userid

@Event('player_disconnect')
def on_player_disconnect(event):
print(index_from_userid(event['userid'])

Am I right to assume that the event 'player_disconnect' is fired after the listener 'OnClientDisconnect' has been called? Also, if my assumption is true, can we say that using an event we can declare a callback that simply expects different arguments than 'its listener' would?
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Events vs. Listeners

Postby iPlayer » Thu Jun 02, 2016 4:55 pm

Well, OnClientDisconnect works well when paired with OnClientActive and other listeners.

I also find events less reliable. I can't remember for sure, but I suspect I have had problems with bots not firing some connect/disconnect events.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Events vs. Listeners

Postby Ayuto » Thu Jun 02, 2016 5:36 pm

BackRaw wrote:Am I right to assume that the event 'player_disconnect' is fired after the listener 'OnClientDisconnect' has been called?
A quick test has shown that the event is getting fired before the listener is called.

The reason why we have added listeners is because there are callbacks in the engine for which no events are fired. We have also used our listeners implementation to provide custom callbacks like OnVersionUpdate or OnEntityOutput.

I can't really say something on the reliability, because I have never tested that.
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: Events vs. Listeners

Postby BackRaw » Thu Jun 02, 2016 5:41 pm

So for me as a scripter it really doesn't matter how I go about it, just personal preference? Because, working with both Android (Java) and its Interface mechanism and Source.Python I tend to like Interfaces/Listeners more than events.

I mean, events are cool and all but I personally don't see any reason to use them in my script when we have those listeners :D

Ayuto wrote:
BackRaw wrote:Am I right to assume that the event 'player_disconnect' is fired after the listener 'OnClientDisconnect' has been called?
A quick test has shown that the event is getting fired before the listener is called.

Now that is a surprise lol
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Events vs. Listeners

Postby Ayuto » Thu Jun 02, 2016 5:44 pm

Well, I think OnClientDisconnect/player_disconnect and OnClientActive/player_activate are pretty much the only duplicates where an event and a listener exist. Since I don't know any downsides go with whatever you prefer.
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: Events vs. Listeners

Postby BackRaw » Fri Jun 03, 2016 6:36 am

Ayuto wrote:Well, I think OnClientDisconnect/player_disconnect and OnClientActive/player_activate are pretty much the only duplicates where an event and a listener exist. Since I don't know any downsides go with whatever you prefer.

Okay then, that clears it up for me :D

There is also OnConVarChanged tho. :)
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Events vs. Listeners

Postby iPlayer » Fri Jun 03, 2016 1:29 pm

BackRaw wrote:There is also OnConVarChanged tho. :)


A huge upside to OnConVarChanged listener is that it doesn't require NOTIFY flag to be set on a cvar. While server_cvar event does.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 12 guests