hook_builtins.html~ 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 :: Hooks
  8. </div>
  9. <!-- content starts here -->
  10. <div class="content-wrap"><div class="content-body-wrap"><div class="content">
  11. <div class="head">Hooks</div>
  12. <div class="info">
  13. NakedMud functions can be open-endedly extended in key areas, through the use
  14. of hooks. When a character enters a room, a few things might need to be done.
  15. If there are hostile NPCs in the room, they might start attacking the character.
  16. If the player is walking over soft earth, footprints might need to be placed.
  17. If a trap is present, it might need to go off. If the character is a king, his
  18. servants might need to greet him. If NPCs have other scripted behavior, it might
  19. also need to be resolved. Adding checks for this in the enter and exit function
  20. leads to unwieldy and unmanageable code. Instead, NakedMud provides a framework
  21. that allows you to specify places where functions can register to 'hook' on to.
  22. Those functions will then be called whenever the designated place is reached.
  23. </div>
  24. <div class="head">Creating a Hook Point</div>
  25. <div class="info">
  26. In lib/pymodules/movement.c, there is a function called try_move. It it the
  27. main function for handling all directional movement. After a character has
  28. sucessfully moved between rooms, this line of code is called:
  29. <pre class="code">
  30. import hooks
  31. ...
  32. hooks.run("enter", hooks.build_info("ch rm", (ch, ch.room)))
  33. </pre>
  34. This provides a point for other functions to hook on to. They can register to be
  35. called whenever this point in the code is reached. Hooks are not ordered, nor
  36. are they blocking. You cannot guarantee they will run in a particular ordering,
  37. nor can you stop sucessive hooks from executing if something special happens in
  38. a previous one.
  39. </div>
  40. <div class="head" style="text-transform: none;">hooks.run(type, info)</div>
  41. <div class="info">
  42. The run function takes two arguments. The first is the type of the hooks to
  43. run. It can be any string. The second is a specially formatted list of
  44. variables to pass on to registered functions. This list is created with a call
  45. to hooks.build_info
  46. </div>
  47. <div class="head" style="text-transform: none;">hooks.build_info(format, variables)</div>
  48. <div class="info">
  49. Hook points pass arguments to their attached hooks through a special information
  50. string that stores the argument information. Arguments can be characters,
  51. objects, rooms, exits, sockets, strings, integers, or doubles. Argument strings
  52. need to be built from a format string and a tuple of the variables that will be
  53. stored in the argument information. The format string is a space-separated list
  54. of shorthands for the argument types. The shorthands are:
  55. <div class="table">
  56. <table width=100% border=1>
  57. <thead>
  58. <tr>
  59. <th> formatting </th>
  60. <th> argument type </th>
  61. <tr>
  62. </thead>
  63. <tbody>
  64. <tr class="odd"> <td>ch</td> <td>characters</td></tr>
  65. <tr class="even"><td>obj</td> <td>objects</td></tr>
  66. <tr class="odd"> <td>rm</td> <td>rooms</td></tr>
  67. <tr class="even"><td>ex</td> <td>exits</td></tr>
  68. <tr class="odd"> <td>sk</td> <td>sockets</td></tr>
  69. <tr class="even"><td>str</td> <td>strings</td></tr>
  70. <tr class="odd"> <td>int</td> <td>integers</td></tr>
  71. <tr class="even"><td>dbl</td> <td>doubles</td></tr>
  72. <tbody>
  73. </table>
  74. </div>
  75. </div>
  76. <div class="head" style="text-transform: none;">hooks.add(type, function)</div>
  77. <div class="info">
  78. To add a hook, a call to hooks.add must be made during initialization. The add
  79. function takes two arguments: the type of hook point to attach to, and a
  80. function to be called. The function itself should take one argument: an argument
  81. string that is created by the hook point, with hooks.build_info
  82. <pre class="code">
  83. def footstep_hook(info):
  84. """make crunchy footstep sounds when we enter a room with snow."""
  85. ############
  86. # FINISH ME
  87. ############
  88. hooks.add("enter", footstep_hook)
  89. </pre>
  90. </div>
  91. <div class="head" style="text-transform: none;">hooks.parse_info(info)</div>
  92. <div class="info">
  93. Hooks take a single argument: an information string, that can be unpacked to
  94. retreive the original arguments passed to hooks.build_info by the hook point.
  95. This function will return a tuple of the original arguments. Here is a
  96. continuation of the footstep hook:
  97. <pre class="code">
  98. def footstep_hook(info):
  99. """make crunchy footstep sounds when we enter a room with snow."""
  100. ch, room = hooks.parse_info(info)
  101. if room.terrain == "snow":
  102. ch.send("Snow crunches beneath your feet.")
  103. </pre>
  104. </div>
  105. <!-- content ends here-->
  106. </div></div></div>
  107. <!-- navigation starts here -->
  108. <div class="nav-wrap"><div class="nav">
  109. <iframe src="nav.html" height="100%" width="100%" scrolling=no frameborder=0>
  110. </iframe>
  111. <!-- navigation ends here -->
  112. </div></div>
  113. <!--div class="footer">Edit Date: Nov 15, 2008. By Geoff Hollis</div-->
  114. </body>
  115. </html>