What are hooks exactly?

I’ve looked over the internet, the GMod wiki, and FP itself. I’m still unsuccessful.

Could someone give me an explanation telling me what they generally are, and an example that makes sense?

I know they’re structured like this:


hook.Add("<something here>", "<Something else here>", <something like a function?>)

Thanks, it’s been a bit mind muddling.

A hook is function that is called whenever an event occurs in the engine. For example, a PlayerDeath hook would be called whenever a player dies.

[lua]local function PlayerDeath(ply)
print(ply:Nick() … “has died!”)
end
hook.Add(“PlayerDeath”, “Unique Name”, PlayerDeath)[/lua]

The first argument passed to hook.Add is the event that should trigger the function. A list of them can be found here.

The second argument is a unique identifier, this is so that you can have many hooks ‘hooked’ onto the same event. The unique identifier must be unique for the event, so you can have the same unique identifier as long as the event is different.

The third argument is the function to be called, this is called the callback. Any other arguments passed to hook.Add will be passed into the callback when it is triggered.

Thanks. Really needed that. I was wondering: “Shouldn’t it only need 2 parts?”
But yeah. Thanks again. :slight_smile:

The unique name is so that you can use that name to remove the hook and identify it if it gives off an error