Resource icon

Hook Library v1.0.1

What permissions do you give others to modify and/or maintain your submission?
Modify: YES - Maintain: YES - I give permission for my entire submission to be modified by others or used in their own work. I give permission for my entire submission to be maintained by others as well.
I made sure my file(s) follow the Submissions Guidelines
  1. Yes
What originally started as a fix for potential conflicts between scripts, this is a library to easily create custom hooks within Lua scripts!

First off, put everything in Lua/hooklib.lua inside your file to load the library for use within your scripts.
You can use the function hookLib.RunHook(string hookname, ...) in your script to determine where you want your custom hook to run its hooked functions, with ... being the arguments a hooked function will take. When this function is called, the hook you specify will run all of its hooked functions at that part of the script with the arguments you gave them, just like SRB2's built in hooks.
Afterwards, you can use hookLib.AddHook(string hookname, function func) to hook functions to be ran when hookLib.RunHook is called. Ideally, you'd want some sort of global variable set so other scripts can detect when the custom hook has been loaded so it's not loaded twice.

Additionally, if you set hookLib.valuemodes[string hookname] to one of the three constants listed below, hookLib.RunHook can return a value! This is great if you want to use your custom hook to overwrite behaviours, similar to a hook like MobjDamage, like so:
Lua:
if (hookLib.RunHook(string hookname, ...))
    -- overwrite behaviour
    return
end
-- default code
All the constants included in the library are:
Name​
Value​
Description​
HF_LASTFUNC
1​
Returns the value of the last hooked function to not return nil. (Like MobjLineCollide)
HF_ANYTRUE
2​
Returns true if any hooked function returns true. (Like MobjDamage)
HF_ANYFALSE
3​
Returns false if any hooked function returns false. (Just included to cover the opposite situation of HF_ANYTRUE)

If you make a custom hook with this library isn't mod-specific that you feel would benefit a lot of coders, feel free to contact me and I can add it to the base file! I couldn't really think of any others to include in the base file myself, but I'm sure there might be some useful ones I didn't think of.

Even though this library was made with vanilla SRB2 in mind, it should work in SRB2Kart as well!
Author
Snu
Downloads
844
Views
3,304
Extension type
pk3
File size
1.2 KB
MD5 Hash
f41ec84729c85ab6b25729cf481df923
First release
Last update

More resources from Snu

Share this resource

Latest updates

  1. Long overdue removal.

    Quickly updating this to remove the wall collision example from the library. This example ended...
Back
Top