|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Declares that an object has events and/or methods that can be "hooked" by other objects.
A hook is a special kind of notification that can take place before or after the associated action (method or event) has happened. The main difference between a hook and a normal event notification (such as the java.awt.event notification system) is that a hook lets you not only veto the event, but in fact change the parameters of the event so that the result is different.
Hooks are an important tool for Wizards coding objects and areas. Using hooks, you can know when just about anything happens in the game - random examples include:
Event hooks are the most common kind of hook. An Event hook is created and run automatically by the game engine for all command events. Event hooks include pre-hooks (before the event) and post-hooks (after the event). Post-hooks are further subdivided into successful post-hooks and failed post-hooks, depending on whether the event was successful or not.
Method hooks are a way for an object to be notified when a given method is invoked on a given target. There is no general mechanism in Java for method hooks, so only certain methods that "provide" their own method hooks can be hooked in this fashion. The standard type of method hook has the same name as the method, although it's possible for a method to provide multiple hooks at different points in its execution. In this case the method will document the names and semantics of its various hooks.
Method Summary | |
void |
addHook(HookCallback callback,
java.lang.String hookName)
Registers a HookCallback object for the specified hook. |
HookList |
getHookList()
Returns the HookList containing the registered hooks for this object. |
java.lang.String[] |
getHooks()
Returns a list of custom hooks implemented by this object. |
void |
removeHook(HookCallback callback,
java.lang.String hookName)
Removes a callback from a specified hook list. |
void |
runFailedPostHook(CommandEvent event)
Notifies all the objects registered on this post-hook that the command failed (for whatever reason). |
void |
runPostHook(CommandEvent event)
Notifies all the objects registered on this post-hook that the event completed successfully. |
void |
runPreHook(CommandEvent event)
Calls all objects registered for a particular event and gives them a chance to deal with the event before it happens. |
Methods inherited from interface wyvern.lib.MethodHookable |
addMethodHook, removeMethodHook, runMethodHook |
Method Detail |
public java.lang.String[] getHooks()
The list doesn't include names of default hooks for built-in commands.
public void addHook(HookCallback callback, java.lang.String hookName)
The reserved pseudo-command "any" is reserved for callbacks that need to get every single command that comes through the queue. This should very rarely be necessary, and can hurt overall game performance considerably if abused.
callback
- the HookCallback to notify when the hook is run.hookName
- the name of the hook list to register on.public void removeHook(HookCallback callback, java.lang.String hookName)
callback
- the HookCallback to remove from the listhookName
- the name of the hook list to unregister from.public void runPreHook(CommandEvent event)
Callbacks may veto the event, but it's still passed to any other callbacks on the list, and eventually to the Command. For callbacks, it typically doesn't really matter if the flag has been set by a previous callback; callbacks should do the same thing they would have done even if it wasn't vetoed, because the Command (or any other callback downstream) may un-veto it.
The callbacks may modify any of the event parameters. Callbacks aren't required to do rigorous checking of the validity of their changes. For example, a simple callback registered on a player's preMoveHook might change the direction in the MoveEvent randomly.
To prevent callbacks from generating illegal event parameters, the Command handling the event is implicitly the last hook on the list. Before executing the command it re-checks the preconditions to make sure the move is still legal, and if it isn't, it vetoes it and issues an error message. The message, of course, is a modifiable parameter of the event.
If a HookCallback wants to force an "illegal" event, it has to change the game state (in the agent, the map, the Command, or whatever) to make the move legal. For example, if the hook wants to make sure the player moves over a wall, the hook can set a property on the player (or the wall) that allows this move, before returning control to this method.
event
- a CommandEvent that has already run. Calling
getVerb() should return the canonical verb for this command,
such as "move" or "get". The verb is used to construct the
hook name, by appending "PreHook" to the verb.public void runPostHook(CommandEvent event)
event
- a CommandEvent that has already run. Calling
getVerb() should return the canonical verb for this command,
such as "move" or "get". The verb is used to construct the
hook name, by appending "PostHook" to the verb.public void runFailedPostHook(CommandEvent event)
Changing the event parameters will have no effect at this point.
event
- a CommandEvent that has failed for some reason.
getVerb() should return the canonical verb for this command,
such as "move" or "get". The verb is used to construct the
hook name, by appending "FailedPostHook" to the verb.public HookList getHookList()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |