hook.Call

From Garry's Mod
Revision as of 23:57, 11 February 2015 by Master the Third (Talk | contribs)
Jump to: navigation, search
 hook.Call( )

Contents

Description

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

Arguments

string eventName

The event to call hooks for

Arguments

table gamemodeTable

If the gamemode is specified, the gamemode hook within will be called, otherwise not

Arguments

vararg args

The arguments to be passed to the hooks

Returns

any

Return data from called hooks

Examples

Example

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

function DoSomething()
    --Does something
    hook.Call("DoneDoingSomething")
end

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

DoSomething()

Output:

Done!

Examples

Example

You can also make your own custom functions controllable via hooks.

function MakeCheese()
    local shouldMakeCheese = hook.Call("MakeCheezPleez")

    if shouldMakeCheese then
        print("yay")
    else
        print("nay")
    end
end

function MakeCheeseOrNo()
    if #player.GetAll() >= 1 then
        return true
    else
        return false
    end
end
hook.Add( "MakeCheezPleez", "Does something else", MakeCheeseOrNo )

MakeCheese()

Output:

If there is players in the server, we print "yay". If there isn't, we print "nay"
Personal tools
Navigation