Author Topic: question about hook.Add  (Read 3378 times)

0 Members and 1 Guest are viewing this topic.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
question about hook.Add
« on: February 14, 2009, 12:46:27 PM »
I know how to use this..

hook.Add("function to hook", "your name for this hook", function_you_are_hooking)


However I was looking through some ULX code and I noticed you guys have an extra parameter in there..

ex: hook.Add( "PlayerSpawnedEffect", "ULXLogEffectSpawn", effectSpawn, 20 )

What is this number at the end for? I've looked on the wiki but it doesn't list there being a 4th parameter to the hook command.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: question about hook.Add
« Reply #1 on: February 14, 2009, 01:26:12 PM »
http://ulyssesmod.net/docs/files/lua/ULib/server/hook-lua.html
Allows developers to write hooks that take precedence over/under other hooks of same name.
Garry's default 'if anything is returned from an in game hook then all other scripts using that same hook stop executing' method is just a pain in the rear.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: question about hook.Add
« Reply #2 on: February 14, 2009, 01:42:34 PM »
So this allows hooks with the same unique name to be run together but in priority chains?

if so, thats interesting...

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: question about hook.Add
« Reply #3 on: February 14, 2009, 04:46:21 PM »
This allows hooks using the same event to continue running it's unique hooks if that particular event's functions returns anything, not the same unique name. (Right Megiddo?)
For example, without ULib, if a script using "GM:PlayerSay" to hook returns anything, Gmod stops executing all other hooks using that event whether it be within that same author's script or not. (at least it did when ULib's hook system was written)
Using ULib's hook.Add, and setting a higher priority than 0 (default), would allow ULib to continue performing the unique hooks that use GM:PlayerSay instead of dying.
« Last Edit: February 14, 2009, 04:57:53 PM by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: question about hook.Add
« Reply #4 on: February 14, 2009, 07:49:49 PM »
You should still have unique names for each hook, MrPresident. The objectives with ULib's modified hooks are as follows:
1. Gives a "pre-hook" (-20) and "post-hook" (20) functionality. Pre hooks are guaranteed to always be called when the hook is used because they are called before any other hooks, and are not allowed to return any values themselves (read-only). Post hooks are likewise called after all other hooks have modified whatever is in question. Post hooks are useful for things like spawn loggers because you know that anything that could have modified the player's ability to spawn the object has already been resolved.

So, it calls in order from -20 and 20, where -20 and 20 are 'read-only'. Anything that doesn't pass in a number is set to 0.

Does that answer your questions? :)
Experiencing God's grace one day at a time.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: question about hook.Add
« Reply #5 on: February 15, 2009, 02:50:44 AM »
Yes it does! =) Thanks.