evennia/game/gamesrc/events/example.py
Griatch c7cbc4854e Reworked object command tables.
Object commands used to require re-adding every call in the script parent's __init__ or factory functions, adding the commands to a new command table directly on the object. Since all other attributes can be set up in at_object_creation(), this was both inconsistent and a bit confusing to work with. There is now a method add_commands() directly defined on all objects. It takes the same arguments as the normal add_command()o but use a reserved attribute to create and update a command table on the object. This has the advantange of completely removing the __init__ call in the script parent, all definitions can now be kept in at_object_creation() and are, more importantly, persistent without having to be recreated every call.
- I updated the examine command to show all the commands defined on an object (if any).
- I updated gamesrc/parents/examples/red_button.py considerably using the new command methodology and also using the updated Events.
.
Griatch
2009-12-03 00:41:53 +00:00

67 lines
2.6 KiB
Python

"""
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.
Note that this type of event will cause *all* red buttons to blink at the same
time, regardless when they were created. This is a very efficient way
to do it (it is also very useful for global events like weather patterns
and day-night cycles), but you can also add events directly to individual objecs
(see the example event in gamesrc/parents/examples/red_button)
"""
import traceback
from src.events import IntervalEvent
from src import scheduler
from src.objects.models import Object
#the logger is useful for debugging
from src import logger
class EventBlinkButton(IntervalEvent):
"""
This event lets the button flash at regular intervals.
"""
def __init__(self):
"""
Note that we do NOT make this event persistent across
reboots since we are actually creating it (i.e. restarting it)
every time the module is reloaded.
"""
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.
Note that we must make sure to handle all tracebacks in this
function to avoid trouble.
"""
#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:
try:
b.scriptlink.blink()
except:
# Print all tracebacks to the log instead of letting them by.
# This is important, we must handle these exceptions gracefully!
logger.log_errmsg(traceback.print_exc())
#create and add the event to the global handler
blink_event = EventBlinkButton()
scheduler.add_event(blink_event)