2009-04-25 20:51:12 +00:00
|
|
|
"""
|
|
|
|
|
Example of the event system. To try it out, make sure to import it from somewhere
|
|
|
|
|
covered by @reload (like the script parent). Create an object inheriting
|
|
|
|
|
the red_button parent to see its effects (e.g. @create button=examples/red_button)
|
|
|
|
|
|
|
|
|
|
Technically the event don't contain any game logics, all it does is locate all
|
|
|
|
|
objects inheriting to a particular script parent and calls one of its functions
|
|
|
|
|
at a regular interval.
|
|
|
|
|
"""
|
|
|
|
|
|
2009-04-30 08:23:54 +00:00
|
|
|
import sys
|
2009-04-25 20:51:12 +00:00
|
|
|
from src.events import IntervalEvent
|
|
|
|
|
from src.scheduler import add_event
|
|
|
|
|
from src.objects.models import Object
|
|
|
|
|
|
2009-04-30 15:01:59 +00:00
|
|
|
#the logger is useful for debugging
|
|
|
|
|
from src.logger import log_errmsg
|
2009-04-25 20:51:12 +00:00
|
|
|
|
|
|
|
|
#Example of the event system. This example adds an event to the red_button parent
|
|
|
|
|
#in parents/examples. It makes the button blink temptingly at a regular interval.
|
|
|
|
|
|
|
|
|
|
class EventBlinkButton(IntervalEvent):
|
|
|
|
|
"""
|
|
|
|
|
This event lets the button flash at regular intervals.
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self):
|
|
|
|
|
"""
|
|
|
|
|
A custom init method also storing the source object.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
super(EventBlinkButton, self).__init__()
|
|
|
|
|
self.name = 'event_blink_red_button'
|
|
|
|
|
#how often to blink, in seconds
|
|
|
|
|
self.interval = 30
|
|
|
|
|
#the description is seen when you run @ps in-game.
|
|
|
|
|
self.description = "Blink red buttons regularly."
|
|
|
|
|
|
|
|
|
|
def event_function(self):
|
|
|
|
|
"""
|
|
|
|
|
This stub function is automatically fired every self.interval seconds.
|
|
|
|
|
|
|
|
|
|
In this case we do a search for all objects inheriting from the correct
|
|
|
|
|
parent and call a function on them.
|
|
|
|
|
"""
|
|
|
|
|
#find all objects inheriting from red_button (parents are per definition
|
|
|
|
|
#stored with the gamesrc/parent/ drawer as a base)
|
|
|
|
|
parent = 'examples.red_button'
|
|
|
|
|
buttons = Object.objects.global_object_script_parent_search(parent)
|
|
|
|
|
|
|
|
|
|
for b in buttons:
|
2009-04-30 08:23:54 +00:00
|
|
|
try:
|
|
|
|
|
b.scriptlink.blink()
|
|
|
|
|
except AttributeError:
|
|
|
|
|
#button has no blink() method. Just ignore.
|
|
|
|
|
pass
|
|
|
|
|
except:
|
2009-04-30 15:01:59 +00:00
|
|
|
#show other tracebacks to log and owner of object.
|
|
|
|
|
#This is important, we must handle these exceptions gracefully!
|
2009-04-30 08:23:54 +00:00
|
|
|
b.get_owner().emit_to(sys.exc_info()[1])
|
2009-04-30 15:01:59 +00:00
|
|
|
log_errmsg(sys.exc_info()[1])
|
2009-04-25 20:51:12 +00:00
|
|
|
|
|
|
|
|
#create and add the event to the global handler
|
|
|
|
|
blink_event = EventBlinkButton()
|
|
|
|
|
add_event(blink_event)
|