06.05.2018, 15:19
(
Last edited by Y_Less; 12/05/2018 at 11:59 PM.
)
OK, question:
I’ve been thinking about extensions to y_hooks for a while now (I’ve mentioned this a few times already). Namely the ability to hook regular and native functions. In theory the code would look something like this:
There are two options:
Or using the newer syntax will require explicit chaining, but would also require that the next callback exists already:
Thoughts?
I’ve been thinking about extensions to y_hooks for a while now (I’ve mentioned this a few times already). Namely the ability to hook regular and native functions. In theory the code would look something like this:
Code:
hook native SetPlayerPos(playerid, Float:x, Float:y, Float:z) { // What goes in here? }
- Explicit chaining. So your hook would look like this:
Code:hook native SetPlayerPos(playerid, Float:x, Float:y, Float:z) { printf("cool"); return SetPlayerPos(playerid, x, y z); // NOT recursive, calls the next hook, or the native. }
- Control flow is obvious and explicit.
- Preventing and manipulating when the next function is called is easy.
- Return values are easy to deal with. This wasn’t an issue with publics, because they only return 0 or 1, but other function types can return anything.
- The generated code is MUCH simpler.
- Compile-time errors if the original function you are hooking (for hook stock) doesn’t exist. You can hook publics that don’t exist, but not natives and stocks.
- Control flow is obvious and explicit.
- Implicit chaining.
So within the hook you don’t call the original SetPlayerPos, all hooks and the original are called one after the other. I really don’t like this idea - it makes writing hook code almost impossible. HOWEVER, this is how hooked publics currently work, and I’m a big fan of consistency. On the other hand, since you can hook publics that don’t actually exist, requiring them to exist breaks the whole situation entirely.
Code:
hook OnPlayerConnect(playerid) { printf("cool"); return 1; }
Code:
hook public OnPlayerConnect(playerid) { printf("cool"); return OnPlayerConnect(playerid); }