Please explain hook.Call/hook.Run

Hello Facepunch, I wanted to create my own hooks, but whats the difference between hook.Call and hook.Run? Also, upon creating a new hook with those, will there be automaticly a function like GM:HookName(vargs)? Or how do I make such a function?

Use hook.Run, unless you want to do weird things where you reference other gamemode tables.

When you do call hook.Run, if a GM: function with the same event name exists, it is called. It is not required to have a GM: function unless you are making a gamemode, in which case it’s usually a good idea.

You can have hooks without any gamemode functions.

Hmm, but now for simplicities sake, lets say I do in player auth, and call it “playerisauthed”, wouldnt there automaticly “exist” the function with that name? And the player would be able to overwrite said function? If the function exists, how do I set a default, like some gmod functions allow returning(take chat function to stop the message from comming through), how would I go about doing so?

Hook.Call and Hook.Run is almost exactly the same. Just different names.

function GM:TestLength(text)
    return text:len()
local text = "Hello there!"
local length = hook.Call("TestLength", GAMEMODE, text)
local _length = hook.Run("TestLength", text)

print(length) //11 
print(_length) // 11

The wiki says the event has to be a string, doesnt it error?

Sorry, Im on my phone. And yes.

Also is it possible to do addon table functions, instead of gm? like

function ADDON:TestLength(text)
    return text:len()
local text = "Hello there!"
local length = hook.Call(TestLength, GAMEMODE, text)
local _length = hook.Run(TestLength, text)

print(length) //11 

Would that work too?

Yes but then you have to use hook.Call and change GAMEMODE to Addon plus Addon has to be a table.

Thanks for your help!

Only if you replaced ‘GAMEMODE’ with the global variable ADDON, in which case you might as well just call the function directly.


fucking ninjad