What is a hook exactly?

So obviously I’m new to Lua. So I’m wondering what exactly a Hook is. I don’t understand the explanation here. I also cannot find another, could someone please explain to me what a Hook is in Lua? Thanks!

In programming in general, a hook is a piece of code that “hooks” into a process (or an assembly such as a DLL file) to either get or set functions from said process. It can be used for various things: debugging, showing extra information about a specific thing, or even cheating. In Gmod hooks should mostly be fine, as long as you’re not messing with the actual game, but rather a Lua script or addon.
My knowledge about this stuff is limited, but it should give you a pretty clear idea of what it is.

hooks attached to events. when one event happen hook is activated and do something related to that event for example drinking water is an event but if you want to learn who is drinking the water you have to use hooks.

so event itself is useless without hooks. events happen and hooks respond think of an event is like your brain and hook is like your consciousness without your consciousness you can’t respond to any of your brain function

Hmm, well I think I have a general understanding… But in it’s arguments… The first argument is what?.. I don’t understand it… such as the following:


hook.Add( "Think", "Some unique name", onThink )

I don’t want to insult your intelligence or anything but have you seen/has anyone showed you this wiki?

it’s very useful for all the gmod specific lua things, including a little info on the specific hooks themselves and what they do too http://wiki.garrysmod.com/page/GM/Think

also just another thing, I use this as a pretty handy little cheat sheet for glua https://steamcommunity.com/sharedfiles/filedetails/?id=144938866, it also has a little info on hooks too which i found pretty useful (i suck/ed at understanding concepts like hooks)

Well I am there… but like on these lines of code:


local function onThink()
	print( "onThink has been called" )
end

hook.Add( "Think", "Some unique name", onThink )

Like where is the event name “think” specified… like on the wiki it says “The event to hook on to.” for the description of that argument… I just don’t understand…

Well the wiki says what each argument is, the first one is the hook its self (So the bit after GM: on the page I linked with the GM:Think info), the 2nd is a unique name and third is what it runs, so on every time the hook think is called (every frame/tick depending on client or server) it will run the code in the onThink function - that first argument is where you tell it which hook you’re trying to add stuff to

should note I suck at lua so I could be wrong with some parts but I’m fairly sure that’s right

edit: also just found this http://wiki.garrysmod.com/page/Hook_Library_Usage

Most of the time, the first argument in a hook.Add is a hook that’s already in the game. I believe you can find all of them under the Hooks section of the gmod wiki. The second argument is whatever you want to call this specific hook.Add that you’re making, and the third is the actual function that’s being hooked on to the first argument. You could even do a function(ply and define the function there for the third argument

For Garry’s Mod specifically, hooks are sort of adding a function in an event (i’m horrible at wording things so go with me lol)

Adding a hook is easy, hook.Add takes three arguments (event name, unique name you want to call it so you can delete it later if you want to etc, and function that is executed when the event presents itself)

Let’s take an example of the Think hook. Think is called every tick so when you hook Think with a function, when the Think event occurs, the function runs.


hook.Add(eventName, uniqueName, func)

There’s a list of hooks here: http://wiki.garrysmod.com/page/Category:Hooks

So basically, I’d put things here into the “eventName” argument?

yeah exactly

Omg thanks soooo much!