hook/Call

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{Func
 
{{Func
|Name=Call
+
|Description=Calls all hooks associated with the given event until one returns something other than nil, and then returns that data.
|Parent=hook
+
 
|Description=Calls hooks associated with the given event<br />
+
In almost all cases, you should use {{LibraryFunction|hook|Run}} instead - it calls hook.Call internally but supplies the gamemode table by itself, making your code neater.
Calls all hooks until one returns something other than nil and then returns that data.
+
|Realm=Shared and Menu
|Realm=Shared
+
 
|IsClass=No
 
|IsClass=No
 +
|File=lua/includes/modules/hook.lua
 +
|Line=62-L125
 
}}
 
}}
 
{{Arg
 
{{Arg
Line 23: Line 24:
 
}}
 
}}
 
{{Ret
 
{{Ret
|type=any
+
|type=vararg
|desc=Return data from called hooks
+
|desc=Return data from called hooks. Limited to 6 return values
 
}}
 
}}
 
{{Example
 
{{Example
Line 54: Line 55:
 
end
 
end
  
function MakeCheeseOrNo()
+
function MakeCheeseOrNot()
 
     if #player.GetAll() >= 1 then
 
     if #player.GetAll() >= 1 then
 
         return true
 
         return true
Line 61: Line 62:
 
     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"
 +
}}
 +
{{Example
 +
|Description=Calls the event "DoneDoingSomething" with args
 +
|Code=hook.Add("DoneDoingSomething", "Does something else", function(a, b)
 +
print(a)
 +
print(b)
 +
end)
 +
 +
 +
hook.Call("DoneDoingSomething", nil, "Hello", "Hey")
 +
|Output=Hello
 +
Hey
 
}}
 
}}

Latest revision as of 19:06, 11 June 2019

 hook.Call( )

Contents

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

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"

Examples

Example

Calls the event "DoneDoingSomething" with args

hook.Add("DoneDoingSomething", "Does something else", function(a, b)
	print(a)
	print(b)
end)


hook.Call("DoneDoingSomething", nil, "Hello", "Hey")

Output:

Hello Hey
Personal tools
Navigation