Page 1 of 1
Events vs. Listeners
Posted: Thu Jun 02, 2016 9:39 am
by BackRaw
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?
Re: Events vs. Listeners
Posted: Thu Jun 02, 2016 4:55 pm
by iPlayer
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.
Re: Events vs. Listeners
Posted: Thu Jun 02, 2016 5:36 pm
by Ayuto
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.
Re: Events vs. Listeners
Posted: Thu Jun 02, 2016 5:41 pm
by BackRaw
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
Re: Events vs. Listeners
Posted: Thu Jun 02, 2016 5:44 pm
by Ayuto
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.
Re: Events vs. Listeners
Posted: Fri Jun 03, 2016 6:36 am
by BackRaw
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. :)
Re: Events vs. Listeners
Posted: Fri Jun 03, 2016 1:29 pm
by iPlayer
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.