hook/Call

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (Stalker's Grammar Correction Service)
Line 1: Line 1:
 
{{Func
 
{{Func
|Name=Call
 
|Parent=hook
 
 
|Description=Calls hooks associated with the given event<br />
 
|Description=Calls hooks associated with the given event<br />
 
Calls all hooks until one returns something other than nil and then returns that data.
 
Calls all hooks until one returns something other than nil and then returns that data.
 
|Realm=Shared
 
|Realm=Shared
 
|IsClass=No
 
|IsClass=No
 +
|Name=Call
 +
|Parent=hook
 
}}
 
}}
 
{{Arg
 
{{Arg
Line 54: Line 54:
 
end
 
end
  
function MakeCheeseOrNo()
+
function MakeCheeseOrNot()
 
     if #player.GetAll() >= 1 then
 
     if #player.GetAll() >= 1 then
 
         return true
 
         return true
Line 61: Line 61:
 
     end
 
     end
 
end
 
end
hook.Add( "MakeCheezPleez", "Does something else", MakeCheeseOrNo )
+
hook.Add( "MakeCheezPleez", "Does something else", MakeCheeseOrNot )
  
 
MakeCheese()
 
MakeCheese()
 
|Output=If there is players in the server, we print "yay". If there isn't, we print "nay"
 
|Output=If there is players in the server, we print "yay". If there isn't, we print "nay"
 
}}
 
}}

Revision as of 09:15, 5 July 2015

 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

vararg

Return data from called hooks. Limited to 6 return values

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 custom functions controllable via hooks.

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

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

function MakeCheeseOrNot()
    if #player.GetAll() >= 1 then
        return true
    else
        return false
    end
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"
Personal tools
Navigation