CodeHooks

by Ter13
Create dynamic behavior easily!
ID:1390595
 
Codehooks requires the user to define a hook by name at the global level, as well as link one object to another explicitly.

Setting up a hook:

world
New()
. = ..()
AddHook("moved")


setting up an object to use the hook:

atom/movable
Move(Newloc,Dir=0,step_x=0,step_y=0)
var/oldloc = src.loc
var/odir = src.dir
var/osx = src.step_x
var/osy = src.step_y
. = ..(Newloc,Dir,step_x,step_y)
if(.)
Moved(oldloc,odir,osx,osy)
Moved(var/atom/Oldloc,var/olddir=0,var/oldstep_x=0,var/oldstep_y=0)
HookCalled("moved",src,args)


CallHook gives you the option to provide a list of arguments, which will be provided to the hooked function. Let's link the object to another with a hook:

obj
testobj
proc
linkproc()

mob
Login()
. = ..()
var/obj/testobj/o = new()
Hook("moved",src,o,"linkproc")


Now, one thing to remember, is that this object will never be deleted by the garbage collector while it has live hooks, or is hooked to another object.

You can get around this by explicitly deleting the object. The default action of delete at the datum level will now force all references created by hooking behaviors together to be de-referenced, thus explicit deletion will cause no issues with the expected behavior of the hook library.