Cross plugin events

Please post any questions about developing your plugin here. Please use the search function before posting!
12jdlovins
Junior Member
Posts: 8
Joined: Mon Apr 16, 2018 5:11 am

Cross plugin events

Postby 12jdlovins » Mon Apr 16, 2018 5:14 am

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!
User avatar
Zeus
Junior Member
Posts: 24
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

Re: Cross plugin events

Postby Zeus » Mon Apr 16, 2018 7:40 pm

User avatar
Ayuto
Project Leader
Posts: 1804
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Cross plugin events

Postby Ayuto » Mon Apr 16, 2018 7:57 pm


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)
12jdlovins
Junior Member
Posts: 8
Joined: Mon Apr 16, 2018 5:11 am

Re: Cross plugin events

Postby 12jdlovins » Mon Apr 16, 2018 8:17 pm

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

Re: Cross plugin events

Postby Ayuto » Mon Apr 16, 2018 8:25 pm

The wiki page linked by Zeus is describing it pretty well. If you have a specific question, feel free to ask.
12jdlovins
Junior Member
Posts: 8
Joined: Mon Apr 16, 2018 5:11 am

Re: Cross plugin events

Postby 12jdlovins » Mon Apr 16, 2018 8:49 pm

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?
User avatar
Zeus
Junior Member
Posts: 24
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

Re: Cross plugin events

Postby Zeus » Mon Apr 16, 2018 10:09 pm

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

Re: Cross plugin events

Postby satoon101 » Mon Apr 16, 2018 11:02 pm

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

Re: Cross plugin events

Postby satoon101 » Mon Apr 16, 2018 11:06 pm

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.
Image
User avatar
Zeus
Junior Member
Posts: 24
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

Re: Cross plugin events

Postby Zeus » Mon Apr 16, 2018 11:35 pm

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

Re: Cross plugin events

Postby satoon101 » Mon Apr 16, 2018 11:39 pm

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.
Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 1 guest