Methods
HookSystem.hook(target: table, name: string, hook: fun( fun(...any), ...any), exact_func: boolean?)
Replaces a function within a class with a new function.
Also allows calling the original function, allowing you to add code to the beginning or end of existing functions.
HookSystem.hook()
should always be called in Mod:init()
. An example of how to hook a function is as follows:
-- this code will hook 'Object:setPosition(x, y)', and will be run whenever that function is called
-- all class functions receive the object instance as the first argument. in this function, i name that argument 'obj', and it refers to whichever object is calling 'setPosition()'
HookSystem.hook(Object, "setPosition", function(orig, obj, x, y)
-- calls the original code (setting its position as normal)
orig(obj, x, y)
-- sets 'new_x' and 'new_y' variables for the object instance
obj.new_x = x
obj.new_y = y
end)
@param target
— The class variable containing the function you want to hook.
@param name
— The name of the function to hook.
@param hook
— The function containing the new code to replace the old code with. Receives the original function as an argument, followed by the arguments the original function receives.
@param exact_func
— (Used internally) Whether the function should be replaced exactly, or whether it should be replaced with a function that calls the hook function. Should not be specified by users.
Arguments:
target: table | The class variable containing the function you want to hook. |
name: string | The name of the function to hook. |
hook: fun( fun(...any), ...any) | The function containing the new code to replace the old code with. Receives the original function as an argument, followed by the arguments the original function receives. |
exact_func: boolean? | (Used internally) Whether the function should be replaced exactly, or whether it should be replaced with a function that calls the hook function. Should not be specified by users. |
HookSystem.hookScript(include: (string|function|Class>|<T>)?)
@param include
— The class to extend from. If passed as a string, will be looked up from the current registry (e.g. scripts/data/actors
if creating an actor) or the global namespace.
@return class
— The new class, extended from include
if provided.
@return super
— Allows calling methods from the base class. self
must be passed as the first argument to each method.
Arguments:
include: (string|function|Class>|<T>)? | The class to extend from. If passed as a string, will be looked up from the current registry (e.g. |
Returns:
class: function|Class> | The new class, extended from |
super: function|Class>|function|Class>> | Allows calling methods from the base class. |
HookSystem.override(old_func: function, new_func: fun( <T))
Returns a function that calls a new function, giving it an older function as an argument.
Essentially, it's a version of HookSystem.hook()
that works with local functions.
@param old_func
— The function to be passed into the new function.
@param new_func
— The new function that will be called by the result function.
@return result_func
— A function that will call the new function, providing the original function as an argument, followed by any other arguments that this function receives.
Arguments:
old_func: function | The function to be passed into the new function. |
new_func: fun( <T) | The new function that will be called by the result function. |
Returns:
result_func: function | A function that will call the new function, providing the original function as an argument, followed by any other arguments that this function receives. |