Hook priority allows for a developer to control when their hook will be called in relation to other hooks on the same function. When done well, hook priority can improve a mod’s compatibility. However, poorly applied priorities can also completely break compatibility with other mods. The syntax, as previously mentioned in the Hooking tutorial, looks like this:Documentation Index
Fetch the complete documentation index at: https://mintlify.com/geode-sdk/docs/llms.txt
Use this file to discover all available pages before exploring further.
Priority::Normal.
There are 7 pre-assigned priority values recommended for use: First, VeryEarly, Early, Normal, Late, VeryLate, Last. Arithmetic on these values are also possible, although not recommended: Priority::Early + 2, meaning 2 internal units later than Early.
There are also 2 prefixes related with priorities: Pre, Post. Pre priorities sort based on code called before the original call, and Post priorities sort based on code called after the original call.
Here’s an example of the pre/post differentiation:
Guidelines
Always use the before/after functions instead of doing arithmetic on priorities! Unless circular priorities happen, it is guaranteed for them to work consistently even if the other mods change their priorities.-
If you want to run your code after some other mod’s code and before calling original, use
setHookPriorityAfterPre. -
If you want to run your code before some other mod’s code and before calling original, use
setHookPriorityBeforePre. -
If you want to run your code before some other mod’s code and after calling original, use
setHookPriorityBeforePost. -
If you want to run your code after some other mod’s code and after calling original, use
setHookPriorityAfterPost.
-
If you want your function to run before all other code, including the original function, use
Priority::FirstwithsetHookPriorityPreand call the original function after your code. -
If you want your function to run after all other code, use
Priority::LastwithsetHookPriorityPostand call the original function at the start of your hook. -
If you want your function to run after the original function, but before other hooks, use
Priority::FirstwithsetHookPriorityPostand call the original function at the start. -
If you’re reimplementing the original function and do not call the original, use
Priority::LastwithsetHookPriorityPre.
Notes
This section details some additional tricks to the hook priority system.-
Be sure you have the function name correct in
setHookPriority! The namespace must be included. -
While the current priority is preserved across function calls, it is not preserved in other situations. For example:
Instead of calling the original, this will call the hook again. This also applies for calling the original through a Task (see geode#994). Calling the original through another function will work as long as it is in the same thread:
-
Using a stub function with
Priority::FirstwithPreorPriority::LastwithPostis an effective way to disable a function. In this scenario, it becomes impossible for other mods to call the original (as their hooks wouldn’t be called either), so this is not recommended. -
Pre/Post values embedded into
Priority::do exist, but they are not that recommended as arithmetic on them can be confusing. (Positive becomes negative for Post) - Manual hooks can have their priorities set through the Hook::setPriority method.
- It is not possible to set the priorities of multiple functions with overloaded parameters in the same modify class (as this would require specifying arguments). In this situation, either split the hooks into separate modify classes or manually hook.
Internal Implementation
The initial call to the function starts with the hook with lowest priority (internallyINT_MIN).
Each call to the original function increases the current priority, with the original function being at the highest priority (internally INT_MAX).
As each function in the chain returns, priority is decreased until the function with lowest priority returns.
At this point, the function call is finished and execution returns to the original caller.
The 7 pre-assigned priorities have the values of -3000, -2000, -1000, 0, 1000, 2000, 3000 respectively.
An example is shown by this diagram and its accompanying code:
Internal notes
- A hook priority of
INT_MAXwill not function as expected, as the original function also has a priority ofINT_MAX.