events.html~ 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <html>
  2. <head>
  3. <link href="../tutorial.css" rel="stylesheet" type="text/css">
  4. </head>
  5. <body>
  6. <div class="header">
  7. The NakedMud Tutorial :: Events
  8. </div>
  9. <!-- content starts here -->
  10. <div class="content-wrap"><div class="content-body-wrap"><div class="content">
  11. <div class="head">Events</div>
  12. <div class="info">
  13. NakedMud provides support for delayed events. These are simply functions that
  14. are called after a pre-specified amount of time. They are not to be confused
  15. with Events in, say, Java, which refer to specific things that take place
  16. (such as someone clicking on a mouse) which have a set of 'listeners' that
  17. execute some function once the event occurs. NakedMud supports this sort of
  18. behavior as well, in the form of hooks.
  19. <p></p>
  20. The most basic thing that could be done with events is creating a command that
  21. delays another command for a specified amount of time. Here is how something
  22. like that would be written:
  23. <pre class="code">
  24. import mudsys, event, mud
  25. def delayed_cmd_event(owner, data, arg):
  26. ch.act(arg)
  27. def cmd_delay(ch, cmd, arg):
  28. '''attempt to delay a command by a specified amount of time.'''
  29. try:
  30. # take two arguments: the delay time, and the command to execute
  31. time, todo = mud.parse_args(ch, True, cmd, arg, "int(time) string(cmd)")
  32. except: return
  33. # notify the character that their command has been delayed,
  34. # and start the event handler
  35. ch.send("In %d seconds you will perform '%s'." % (time, todo))
  36. event.start_event(ch, time, delayed_cmd_event, None, todo)
  37. mudsys.add_cmd("delay", None, cmd_delay, "player", False)
  38. </pre>
  39. Events have three parameters. An owner, data (possibly None), and a string
  40. argument (possibly None). The owner is the person, place, or thing the event
  41. belongs to. If the owner ever leaves the game, the event is canceled. The
  42. owner can be None. For instance, you may want to write a combat update event
  43. that iterates across all characters and makes them perform an attack every
  44. second. To do this, you could start the event up without an owner when your
  45. module is initialized, and have the event do its work, and re-start itself
  46. whenever it is called.
  47. <p></p>
  48. Data can be anything you want it to be. Suppose you want to implement a delayed
  49. fireball spell that hurts everyone in the room except for the person who set it
  50. up. The owner would be the room, and the data might be the character. You could
  51. possibly do it as the owner being the character and the data being None, but
  52. think what might happen if the character decided to move before the event went
  53. off! Here is one way to do a delayed fireball event
  54. <pre class="code">
  55. import mudsys, event
  56. def delayed_fireball_event(room, caster, arg):
  57. '''do the delayed fireball event. Hurt everyone in the room except the
  58. caster. arg is None.'''
  59. room.send("With violent force, a delayed fireball explodes in the room!")
  60. for ch in room:
  61. if ch != caster:
  62. # do something mean here
  63. ############
  64. # FINISH ME
  65. ############
  66. def cmd_delayed_fireball(ch, cmd, arg):
  67. '''cast a delayed fireball.'''
  68. ch.send("You set up a delayed fireball to go off in 5 seconds!")
  69. event.start_event(ch.room, 5, delayed_fireball_event, ch, None)
  70. mudsys.add_cmd("df", None, cmd_delayed_fireball, "player", False)
  71. </pre>
  72. Once an event is started, it cannot be canceled unless its owner is removed
  73. from the game, so be certain to design your events with this in mind.
  74. <p></p>
  75. Events are started with a call to start_event. This function takes three
  76. mandatory arguments, and two optional ones. The first argument is the owner,
  77. the second is the time to delay the event, and the third is the event itself.
  78. The fourth and fifth are the data and the string argument, respectively.
  79. </div>
  80. <!-- content ends here-->
  81. </div></div></div>
  82. <!-- navigation starts here -->
  83. <div class="nav-wrap"><div class="nav">
  84. <iframe src="nav.html" height="100%" width="100%" scrolling=no frameborder=0>
  85. </iframe>
  86. <!-- navigation ends here -->
  87. </div></div>
  88. <!--div class="footer">Edit Date: Nov 15, 2008. By Geoff Hollis</div-->
  89. </body>
  90. </html>