Page 1 of 1

Cross plugin events

Posted: Mon Apr 16, 2018 5:14 am
by 12jdlovins
Hey there,

Really cool project and alternative to source pawn :)

I was wondering if there is an equivalent to sourcepawns forwards / natives? Can i make a custom event and hook onto it in a different plugin and such?

Thanks!

Re: Cross plugin events

Posted: Mon Apr 16, 2018 7:40 pm
by Zeus

Re: Cross plugin events

Posted: Mon Apr 16, 2018 7:57 pm
by Ayuto

I would only use that if something outside of Source.Python (e. g. Sourcemod plugins) need to listen to those events. If you want to share notifications across Source.Python plugins or libraries, I would suggest to use our listeners module.

Syntax: Select all

from listeners import ListenerManager, ListenerManagerDecorator

class MyNotification(ListenerManagerDecorator):
manager = ListenerManager()

# Somewhere else: Call all registered callbacks
MyNotification.manager.notify(10, 20)

# Somewhere else: Register a callback
@MyNotification
def on_my_notification(x, y):
print('Called', x, y)

# Alternatively, if you don't want to use the decorator to register/unregister a callback,
# you can also do this:
def on_my_notification(x, y):
print('Called', x, y)

MyNotification.manager.register_listener(on_my_notification)
# Or MyNotification.manager.unregister_listener(on_my_notification)

Re: Cross plugin events

Posted: Mon Apr 16, 2018 8:17 pm
by 12jdlovins
Ayuto wrote:

I would only use that if something outside of Source.Python (e. g. Sourcemod plugins) need to listen to those events. If you want to share notifications across Source.Python plugins or libraries, I would suggest to use our listeners module.

Syntax: Select all

from listeners import ListenerManager, ListenerManagerDecorator

class MyNotification(ListenerManagerDecorator):
manager = ListenerManager()

# Somewhere else: Call all registered callbacks
MyNotification.manager.notify(10, 20)

# Somewhere else: Register a callback
@MyNotification
def on_my_notification(x, y):
print('Called', x, y)

# Alternatively, if you don't want to use the decorator to register/unregister a callback,
# you can also do this:
def on_my_notification(x, y):
print('Called', x, y)

MyNotification.manager.register_listener(on_my_notification)
# Or MyNotification.manager.unregister_listener(on_my_notification)


Hmm i see, that listener module is interesting. I'm also interested in knowing how the Event module works between Source.Python and Sourcemod, would you be able to go in more detail with that?

In SM i know you make a forward and share the .inc file between plugins, how would source.python call an event and have SM listen for it?

Is it just as simple as using https://wiki.alliedmods.net/Events_(SourceMod_Scripting) to listen and pulling the data from the event object?

Re: Cross plugin events

Posted: Mon Apr 16, 2018 8:25 pm
by Ayuto
The wiki page linked by Zeus is describing it pretty well. If you have a specific question, feel free to ask.

Re: Cross plugin events

Posted: Mon Apr 16, 2018 8:49 pm
by 12jdlovins
Ayuto wrote:The wiki page linked by Zeus is describing it pretty well. If you have a specific question, feel free to ask.



So for instance using the wikis example:

Code: Select all

# Create the event
event_instance = My_New_Event()

# Set the variable values
event_instance.userid = 2
event_instance.pos_x = 1.111
event_instance.pos_y = 2.222
event_instance.pos_z = 3.333
event_instance.weapon = 'weapon_knife'

# Fire the event
event_instance.fire()


Would create an instance of the event and fire it off.

On the SM side of things would i just do

Code: Select all

public void OnPluginStart()
{
   HookEvent("My_New_Event", My_New_Event, EventHookMode_Pre);
}
 
public Action My_New_Event(Event event, const char[] name, bool dontBroadcast)
{
   int userid = efvent.getInt("userid");
   return Plugin_Continue;
}


to be able to get the data from the event that was triggered in Source.Python?

Re: Cross plugin events

Posted: Mon Apr 16, 2018 10:09 pm
by Zeus
Ayuto wrote:

I would only use that if something outside of Source.Python (e. g. Sourcemod plugins) need to listen to those events. If you want to share notifications across Source.Python plugins or libraries, I would suggest to use our listeners module.

Syntax: Select all

from listeners import ListenerManager, ListenerManagerDecorator

class MyNotification(ListenerManagerDecorator):
manager = ListenerManager()

# Somewhere else: Call all registered callbacks
MyNotification.manager.notify(10, 20)

# Somewhere else: Register a callback
@MyNotification
def on_my_notification(x, y):
print('Called', x, y)

# Alternatively, if you don't want to use the decorator to register/unregister a callback,
# you can also do this:
def on_my_notification(x, y):
print('Called', x, y)

MyNotification.manager.register_listener(on_my_notification)
# Or MyNotification.manager.unregister_listener(on_my_notification)


What is the conceptual difference between the listener module and registering an event, and why one vs the other?

Re: Cross plugin events

Posted: Mon Apr 16, 2018 11:02 pm
by satoon101
12jdlovins wrote:
Ayuto wrote:The wiki page linked by Zeus is describing it pretty well. If you have a specific question, feel free to ask.



So for instance using the wikis example:

Code: Select all

# Create the event
event_instance = My_New_Event()

# Set the variable values
event_instance.userid = 2
event_instance.pos_x = 1.111
event_instance.pos_y = 2.222
event_instance.pos_z = 3.333
event_instance.weapon = 'weapon_knife'

# Fire the event
event_instance.fire()


Would create an instance of the event and fire it off.

On the SM side of things would i just do

Code: Select all

public void OnPluginStart()
{
   HookEvent("My_New_Event", My_New_Event, EventHookMode_Pre);
}
 
public Action My_New_Event(Event event, const char[] name, bool dontBroadcast)
{
   int userid = efvent.getInt("userid");
   return Plugin_Continue;
}


to be able to get the data from the event that was triggered in Source.Python?

Correct. I am not sure about SM event hooks, but there could be an issue if you try to register a hook for an event before the server loads that event. I know I have run into that issue within SP itself, and had to make sure my custom events were registered prior to any hooks being registered for them.

Re: Cross plugin events

Posted: Mon Apr 16, 2018 11:06 pm
by satoon101
Zeus wrote:What is the conceptual difference between the listener module and registering an event, and why one vs the other?

As far as "why", the only reason to use a custom event over a listener is if you want to have access in SM or another server plugin. That's the only reason I haven't changed GunGame to use listeners instead of custom events.

Re: Cross plugin events

Posted: Mon Apr 16, 2018 11:35 pm
by Zeus
satoon101 wrote:
Zeus wrote:What is the conceptual difference between the listener module and registering an event, and why one vs the other?

As far as "why", the only reason to use a custom event over a listener is if you want to have access in SM or another server plugin. That's the only reason I haven't changed GunGame to use listeners instead of custom events.

So im confused then; if registering an event allows one to use it in SM, but they're otherwise functionally the same, why not just have the 1 in SP?

Re: Cross plugin events

Posted: Mon Apr 16, 2018 11:39 pm
by satoon101
We created the Custom Events long before we even had the concept of the listeners. We have kept the Custom Events because of the added ability to work across server plugins.