<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://wiki.lostsouls.org/w/skins/common/feed.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>Man hooks (mechanism) - Revision history</title>
		<link>http://wiki.lostsouls.org/w/index.php?title=Man_hooks_%28mechanism%29&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.8.2</generator>
		<lastBuildDate>Mon, 04 May 2026 08:43:50 GMT</lastBuildDate>
		<item>
			<title>Chaos: Man hooks(mechanism) moved to Man hooks (mechanism)</title>
			<link>http://wiki.lostsouls.org/w/index.php?title=Man_hooks_%28mechanism%29&amp;diff=26392&amp;oldid=prev</link>
			<description>&lt;p&gt;&lt;a href=&quot;/Man_hooks%28mechanism%29&quot; title=&quot;Man hooks(mechanism)&quot;&gt;Man hooks(mechanism)&lt;/a&gt; moved to &lt;a href=&quot;/Man_hooks_%28mechanism%29&quot; title=&quot;Man hooks (mechanism)&quot;&gt;Man hooks (mechanism)&lt;/a&gt;&lt;/p&gt;

			&lt;table border='0' width='98%' cellpadding='0' cellspacing='4' style=&quot;background-color: white;&quot;&gt;
			&lt;tr&gt;
				&lt;td colspan='2' width='50%' align='center' style=&quot;background-color: white;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' width='50%' align='center' style=&quot;background-color: white;&quot;&gt;Revision as of 15:27, 24 January 2013&lt;/td&gt;
			&lt;/tr&gt;
			
			&lt;/table&gt;
		</description>
			<pubDate>Thu, 24 Jan 2013 15:27:20 GMT</pubDate>			<dc:creator>Chaos</dc:creator>			<comments>http://wiki.lostsouls.org/Talk:Man_hooks_%28mechanism%29</comments>		</item>
		<item>
			<title>Laine at 21:55, 11 June 2007</title>
			<link>http://wiki.lostsouls.org/w/index.php?title=Man_hooks_%28mechanism%29&amp;diff=3988&amp;oldid=prev</link>
			<description>&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Synopsis==&lt;br /&gt;
&lt;br /&gt;
Ain Soph supports a generalized mechanism for allowing developer-defined&lt;br /&gt;
code to alter the course of and be notified of lib-handled events.  This&lt;br /&gt;
is referred to as the &amp;quot;hooks&amp;quot; mechanism.&lt;br /&gt;
&lt;br /&gt;
==Files==&lt;br /&gt;
&lt;br /&gt;
 /mod/basic/hooks.c&lt;br /&gt;
 /lib/hooks.h&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&lt;br /&gt;
A hook is a way of getting the core mechanics of the game to talk to your&lt;br /&gt;
code when some event it handles occurs, like an object moving or an item&lt;br /&gt;
being equipped or a lock being picked.  The term &amp;quot;hook&amp;quot; comes from the idea&lt;br /&gt;
that the lib is providing a &amp;quot;hook&amp;quot; that you can &amp;quot;hang&amp;quot; your code on.  There&lt;br /&gt;
are many types of hooks, each with a different function; some allow you to&lt;br /&gt;
determine whether an event will be allowed to occur, others allow you to&lt;br /&gt;
alter numerical values like damage amounts and success chances, and others&lt;br /&gt;
simply provide you with notification that an event has occurred.  All hooks&lt;br /&gt;
currently supported by the lib are listed in 'man hook_list'.&lt;br /&gt;
&lt;br /&gt;
When you define a hook, you send some particular object a hook type and&lt;br /&gt;
the code that the lib should talk to -- generally in the form of a function&lt;br /&gt;
pointer (see 'man function').  You are saying to the lib, &amp;quot;when this event&lt;br /&gt;
happens to the object I am manipulating, talk to this piece of code about&lt;br /&gt;
it&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The two basic functions required to interact with hooks from a perspective&lt;br /&gt;
of project building are add_hook() and remove_hook().&lt;br /&gt;
&lt;br /&gt;
status add_hook(mixed hook, mixed call)&lt;br /&gt;
&lt;br /&gt;
This function inserts a hook call into the object's list of hooks.  Since&lt;br /&gt;
add_hook() exists in all characters, items, rooms, and standard daemons,&lt;br /&gt;
any hook can be defined in nearly any object, but only certain object types&lt;br /&gt;
will make use of them, as described in 'man hook_list'.&lt;br /&gt;
&lt;br /&gt;
The first argument to add_hook() is a hook macro from hooks.h; the hook&lt;br /&gt;
macros and their effects are listed in 'man hook_list'.  While the hook&lt;br /&gt;
macros resolve to integer values, do not ever use a literal integer for&lt;br /&gt;
defining a hook.&lt;br /&gt;
&lt;br /&gt;
The second argument defines the call you wish to be performed by the hook&lt;br /&gt;
mechanism.  The most common thing to pass here is a closure; e.g.&lt;br /&gt;
add_hook(Can_Unlock, #'my_unlock_hook).  A closure will be simply&lt;br /&gt;
passed the arguments described for the hook in 'man &amp;lt;hook&amp;gt;'.  You may&lt;br /&gt;
also pass an object or a string object filename; using these will result in&lt;br /&gt;
the function resolve_hook() being called in the specified object and passed&lt;br /&gt;
the integer hook type followed by the standard arguments for the hook.&lt;br /&gt;
With improvements in the save mechanics, there is no really good reason to&lt;br /&gt;
use the object/string hook functionality, and it may be removed at some&lt;br /&gt;
point in the future; consider it deprecated.&lt;br /&gt;
&lt;br /&gt;
add_hook() returns true if the hook was successfully defined.&lt;br /&gt;
&lt;br /&gt;
set_hook() performs the same function as add_hook(), as a backward&lt;br /&gt;
compatibility function.&lt;br /&gt;
&lt;br /&gt;
status remove_hook(mixed hook, mixed call)&lt;br /&gt;
&lt;br /&gt;
remove_hook() is used to remove a previously defined hook from an object.&lt;br /&gt;
The arguments must be identical to the ones originally used to define the&lt;br /&gt;
hook.&lt;br /&gt;
&lt;br /&gt;
 closure hit_hook;&lt;br /&gt;
 int hits;&lt;br /&gt;
&lt;br /&gt;
 void weapon_hit() {&lt;br /&gt;
     hits++;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 void start_counting_hits() {&lt;br /&gt;
     add_hook(Mod_Inflict_Damage, #'weapon_hit)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 void stop_counting_hits() {&lt;br /&gt;
     remove_hook(Mod_Inflict_Damage, #'weapon_hit)&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
 void create() {&lt;br /&gt;
     start_counting_hits()&lt;br /&gt;
     call_out(&amp;quot;stop_counting_hits&amp;quot;, 300)&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
remove_hook() returns true if the hook was successfully removed.&lt;br /&gt;
&lt;br /&gt;
The other functions related to hooks are query_hook(), any_hook(),&lt;br /&gt;
query_hooks(), and check_hook().&lt;br /&gt;
&lt;br /&gt;
mixed query_hook(mixed hook)&lt;br /&gt;
&lt;br /&gt;
query_hook() returns the call information associated with the specified&lt;br /&gt;
hook.  If none are defined, this will be 0.  Otherwise, it may return a&lt;br /&gt;
closure, an object, a string, or an array composed of two or more of&lt;br /&gt;
these.&lt;br /&gt;
&lt;br /&gt;
status any_hook(mixed hook)&lt;br /&gt;
&lt;br /&gt;
Returns true if the object has any hook information for the hook specified.&lt;br /&gt;
This is intended as the fastest possible test to see if it is useful to&lt;br /&gt;
do a full check of the hook.  The reason to do this is that the mapping&lt;br /&gt;
construction used in specifying hook arguments is fairly expensive.&lt;br /&gt;
&lt;br /&gt;
mapping query_hooks()&lt;br /&gt;
&lt;br /&gt;
Returns the mapping containing the object's hook information.  A mapping&lt;br /&gt;
is used rather than an array despite the integer hook definitions because&lt;br /&gt;
the hook information is expected to be very sparse in nearly all objects.&lt;br /&gt;
&lt;br /&gt;
varargs mixed check_hook(mixed hook, mixed args, mixed working_value)&lt;br /&gt;
&lt;br /&gt;
This is the function used by lib modules to determine the effects a hook&lt;br /&gt;
should have.  The first two arguments are the hook to check and the&lt;br /&gt;
arguments to pass to the hook calls.  The third argument is optional and&lt;br /&gt;
is only used by Poll hooks.  The exact behavior of check_hook() depends on&lt;br /&gt;
the type of hook being analyzed:&lt;br /&gt;
&lt;br /&gt;
For Can hooks, each call is checked in turn until one returns a value&lt;br /&gt;
other than 1 or 0, at which point that value is immediately returned and&lt;br /&gt;
no other calls are checked.  If no such return value is encountered,&lt;br /&gt;
check_hook() returns 0 if any call returned 0, or 1 otherwise.&lt;br /&gt;
&lt;br /&gt;
For Do, Fail, and At hooks, all calls are checked.  If any call returned&lt;br /&gt;
a value other than 1 or 0, the first such value found is returned.  If no&lt;br /&gt;
such return values are encountered, check_hook() returns 1 if any call&lt;br /&gt;
returned 1, or 0 otherwise.&lt;br /&gt;
&lt;br /&gt;
For Mod hooks, all calls are checked and their return values are&lt;br /&gt;
accumulated (which will result in an error if any values are of types&lt;br /&gt;
which are not addition-compatible).  The accumulated value is returned.&lt;br /&gt;
&lt;br /&gt;
For Value hooks, each call is checked in turn until one returns a value&lt;br /&gt;
other than 0, at which points that value is immediately returned and no&lt;br /&gt;
other calls are checked.  If no such return value is encountered,&lt;br /&gt;
check_hook() returns 0.&lt;br /&gt;
&lt;br /&gt;
For Poll hooks, all calls are checked, and those with nonzero return values&lt;br /&gt;
are considered &amp;quot;votes&amp;quot; for their value.  A return value of ({ 0 }) is&lt;br /&gt;
considered a vote for zero.  Also, if a two-element array with an integer&lt;br /&gt;
second element is returned it is considered a number of votes for the first&lt;br /&gt;
element equal to the second element; i.e. ({ &amp;quot;foo&amp;quot;, 3 }) is three votes for&lt;br /&gt;
the value &amp;quot;foo&amp;quot;.  For these hooks, the third argument to check_hook(),&lt;br /&gt;
working_value, specifies a value which will be returned if no values are&lt;br /&gt;
obtained from hooks (allowing a result of zero to be distinguished from no&lt;br /&gt;
result).&lt;br /&gt;
&lt;br /&gt;
For Revise hooks, all calls are checked, with whatever value is returned&lt;br /&gt;
by each hook in turn considered as the desired revised value.  The current&lt;br /&gt;
version of this continually-revised value is passed as the first argument&lt;br /&gt;
to the hook functions, in addition to any normal argument set for the&lt;br /&gt;
hook.&lt;br /&gt;
&lt;br /&gt;
All lib-level hooks, and nearly all hooks in general, use a mapping for&lt;br /&gt;
their arguments, with various keys as appropriate to their function.  Note,&lt;br /&gt;
however, that if check_hook() receives an array argument, the array will be&lt;br /&gt;
expanded into an argument list as per apply().&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[[man hook list|hook_list]](mechanisms)&lt;/div&gt;</description>
			<pubDate>Mon, 11 Jun 2007 21:55:49 GMT</pubDate>			<dc:creator>Laine</dc:creator>			<comments>http://wiki.lostsouls.org/Talk:Man_hooks_%28mechanism%29</comments>		</item>
	</channel>
</rss>