Garry's Mod Wiki

hook.Call

hook.Call

  vararg hook.Call( string eventName, table gamemodeTable, vararg args )

Description

Calls all hooks associated with the given event until one returns something other than nil, and then returns that data.

In almost all cases, you should use hook.Run instead - it calls hook.Call internally but supplies the gamemode table by itself, making your code neater.

Arguments

1 string eventName
The event to call hooks for.
2 table gamemodeTable
If the gamemode is specified, the gamemode hook within will be called, otherwise not.
3 vararg args
The arguments to be passed to the hooks.

Returns

1 vararg
Return data from called hooks. Limited to 6 return values.

Example

Runs function DoSomething, which eventually calls the event DoneDoingSomething, triggering the hooked function DoSomethingElse.

local function DoSomething() -- Does something hook.Call( "DoneDoingSomething" ) end local function DoSomethingElse() -- Does something else, once the hook "DoneDoingSomething" is called. print( "Done!" ) end hook.Add( "DoneDoingSomething", "Does something else", DoSomethingElse ) DoSomething()
Output:
Done!

Example

You can also make custom functions controllable via hooks.

local function MakeCheese() local shouldMakeCheese = hook.Call( "MakeCheezPleez" ) if shouldMakeCheese then print( "yay" ) else print( "nay" ) end end local function MakeCheeseOrNot() return player.GetCount() >= 1 end hook.Add( "MakeCheezPleez", "Does something else", MakeCheeseOrNot ) MakeCheese()
Output: If there is players in the server, we print yay. If there isn't, we print nay.

Example

Calls the event DoneDoingSomething with some arguments.

hook.Add( "DoneDoingSomething", "Does something else", function( a, b ) print( a ) print( b ) end ) hook.Call( "DoneDoingSomething", nil, "Hello", "Hey" )
Output:
Hello Hey

Special Pages


Wikis

?

Render Time: 40ms

DB GetPage 4
Generate Html 8
SaveChanges (1) 15
Render Body 0
Render Sidebar 10