Also something to note here is that decorators are implented, normal functions should still be implace; while decorators are truely useful in everyday scripting, functions for registering and unregistering event listeners/commands etc prove useful if you do the registering dynamically (for example, if you only listen to events if related config settings were enabled).
Dualism example:
Syntax: Select all
import sp.event
from sp.decorators import event, load
@load
def load():
global config
config = MyConfig()
if config['extrahealthonspawn']: # Dynamic registration
sp.event.Register(health, 'player_spawn')
@event
def player_hurt(ev):
pass # need this in any case
def health(ev):
pass #code here
def OnConfigChanged(config, oldconfig): # some more dynamic registration
if config['extrahealthonspawn'] and not oldconfig['extrahealthonspawn']:
sp.event.Register(health, 'player_spawn')
if oldconfig['extrahealthonspawn'] and not config['extrahealthonspawn']:
sp.event.Unregister(health)
Anyway, though I think it is generally a good idea to unregister events on unload automatically (I don't see why they should point to a non existing function).
DanielB wrote:If this were to be done I'd like to see each addon have to create an addon instance, similar to es.AddonInfo.
This would ensure each addon had some basic info (author, name, version, forum url, ...) and could provide other functionality.
so @event(...) would become @myAddon.event(...)
Yeah that sounds like a good idea as well.
your-name-here wrote:Don't take this the wrong way as I am just trying to understand. What problem are decorators supposed to address?
I think DanielB's post addresses this really well. Though in addition I don't think it is a good idea to just register any function that happens to have the same name as an event as cb. This functionality could be optional, but I don't think it should be default - in ES I think this was done due to legacy compability, but now we are not forced to do it that way.
If you happen to choose a name for a function that matches an event, you'll be registering an event without knowing it, similarily, you may not listen to events to want to listen to because they do not exist (though AFAIK you can't really check for event existence - but at least a warning could be shown). It also seems a bit of [unnecessary?] extra work to go though the script files and search for a matching event function everytime instead of going though a list of registered events/callbacks for a matching eventname.