HookFunction
This function was added in DSL 6
Description
Register a new function hook for a base game C function with a given functionName
. Your callback
function will be called with 3 arguments after the function returns.
Callbacks are passed a table of arguments, a table of results, and a boolean indiciating whether or not the function was a replacement.
Registered hooks are tied to the current script, and will be automatically cleaned up when the script is terminated.
function HookFunction(functionName, callback) --[[ ... ]] end
Parameters
functionName
:string
- The name of the function to hook.callback
:fun(args: { [integer]: any, n: integer }, results?: { [integer]: any, n: integer }, isReplacement?: boolean)
- A function that will be called with the following parameters:args
:table
- A table containing the arguments passed to the hooked function, with an additional keyn
indicating the number of arguments.results
:table
- A table containing the results returned by the hooked function, with an additional keyn
indicating the number of results. This may benil
if the function does not return any values.isReplacement?
:boolean
- A boolean indicating whether the hooked function was replaced by a custom implementation.
Return Values
hook
:userdata
- A handle to the created hook. This can be used to remove the hook later if needed.
Example
local hook = HookFunction('GetPlayerName', function(args, results, isReplacement)
print('GetPlayerName called with args:', args)
if isReplacement then
print('This was a replacement function.')
else
print('This was the original function.')
end
end)
-- Later, you can remove the hook if needed
RemoveFunctionHook(hook)