Why use Hooks?

So, I “kind of get” how hooks are used (looking at Wiki). But why do people use them? What are they good for? Do you mind including examples w̶h̶e̶r̶e̶ ̶s̶o̶m̶e̶o̶n̶e̶ ̶w̶o̶u̶l̶d̶ ̶m̶i̶g̶h̶t̶ ̶u̶s̶e̶ ̶t̶h̶e̶m̶ ̶a̶n̶d̶ ̶w̶h̶y̶ ̶i̶n̶s̶t̶e̶a̶d̶ ̶o̶f̶ ̶o̶t̶h̶e̶r̶ ̶m̶e̶t̶h̶o̶d̶s̶? - Thanks

What other methods are you referring to? A hook is used when you need a function to run when a certain event occurs in the game. For instance if I wanted to create a chat command I would have to use the PlayerSay hook. This allows me to run code when the player types a message in game. To my knowledge there isn’t another way to do this besides using a hook so I am a bit confused on what other methods you are referring to.

Edit: You should also be aware that there is a difference between using.


GM:PlayerSay()

And using


hook.Add( "PlayerSay", "PlayerSayExample", function( ply, text, team ) print("Hello World!") end)

Using GM:PLayerSay() you are overriding that function and telling it to not run any other code except for the code I give you here. Meanwhile when you use hool.add you are adding onto that function. You are saying when you run the other code you should also run this code too.

I’m really not sure what you are asking. I mean just by the wiki; viewing all of the hooks can show you what they can do. I mean just alone, PlayerTick, Think, and GM hooks, they are all extremely important.

[editline]2nd November 2015[/editline]

Another example - I just wrote a bit of code yesterday that utilized



hook.Add("PostGamemodeLoaded", "CheckConfigs", function()

end)


I used it to determine information about the gamemode and settings for it, VS settings someone modified within my config, and if the settings aren’t compatible with that gamemode it goes through and disables the settings that won’t work with the loaded gamemode, and posts a server warning message with the details.

Hooks make for endless possibilities.

Ah right my bad. I guess I misunderstood it completely. Thanks for the replies. I get it now :slight_smile: