Guarantee an event listener to be called before other listeners of the same event?

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Guarantee an event listener to be called before other listeners of the same event?

Postby Mahi » Thu Jul 02, 2015 1:12 pm

How would I ensure that my player_spawn event listener gets called before others?
I'm trying to reset every player's gravity to 1.0 whenever they spawn, and I want to make sure it happens before other player_spawn functions modify the gravity again.

Here's an easy example code of what I have:

Syntax: Select all

# ../plugins/foo/bar.py
@Event
def player_spawn(game_event):
player = PlayerEntity(index_from_userid(game_event.get_int('userid')))
player.gravity = 1.0

# ../plugins/foo/foo.py
@Event
def player_spawn(game_event):
player = PlayerEntity(index_from_userid(game_event.get_int('userid')))
player.gravity -= 0.5
and I need to make sure that player's gravity gets reset in bar.py before it's reduced by 0.5 in foo.py
stonedegg
Senior Member
Posts: 141
Joined: Sun Aug 12, 2012 11:45 am

Postby stonedegg » Thu Jul 02, 2015 1:22 pm

You could do something like this:


Syntax: Select all

# ../plugins/foo/bar.py

grav = {}

@Event
def player_spawn(game_event):
userid = game_event.get_int('userid')
player = PlayerEntity(index_from_userid(userid))
player.gravity = 1.0
grav[userid] = 1



# ../plugins/foo/foo.py

from foo.bar import grav

@Event
def player_spawn(game_event):
userid = game_event.get_int('userid')
if userid in grav and grav[userid] == 1:
# delay this or something
player = PlayerEntity(index_from_userid(userid))
player.gravity -= 0.5
grav[userid] = 0



Also, i'm fairly sure that whichever plugin gets loaded first, will execute events first.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Thu Jul 02, 2015 1:25 pm

The first one registered is the first one called. Just make sure your imports are set so that the one you want first gets imported first. Either that, or create a pre-event hook and change it there.
Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 135 guests