hook.Add
Example
This will hook onto the "Think" event with the function onThink, printing to the console whenever the event occurs.
local function onThink()
print( "onThink has been called" )
end
hook.Add( "Think", "Some unique name", onThink )
Output: "onThink has been called" repeating continuously.
Example
This works the same as above, but defines the function inside hook.Add rather than above it.
Output: "Think has been called" repeating continuously.
Example
This code demonstrates how you can add a table function with a 'self' argument, without the use of a wrapper function
local myTable = {}
function myTable:IsValid()
return true
end
function myTable:PlayerInitialSpawn(ply)
print( "CustomHook", self, ply )
end
hook.Add( "CustomHook" , myTable , myTable.PlayerInitialSpawn )
hook.Run( "CustomHook" )
Output: "CustomHook table: 0x00000000 Player [1][PotatoMan]"