Making a custom hook.

Yello. So I was looking at the hook.Call and hook.Run. It doesn’t really give me what I need.

What I am trying to do is make my own hook/event of where one of my functions run. I then want to be able to take the arguments of that function and pass them through my own custom hook somewhere else.

An example of what I’m trying to do.




function playerflying (ply, flying)

    if flying then
        ply:NoClip()
    end

end


So above is a custom function.




hook.Add("playerflying", "ThisIsMyOwnCustomFunctionHook", function (ply, flying)

    if ply == some_other_predefined_ply_obect then
        ply:GiveCape() -- Pretend this is a function that gives a cape
    end

end)



So as above, I am trying to somehow get a hook to call when my own custom function runs. I also want the arguments passed through the function to also pass through the hook.

From what I understand, hook.Call and hook.Run just work as calling the actual function.

Do I have to make my custom function a meta-function to do this?

Just use hook.Call with the hook name you want, and leave the gamemode table arg as nil.

You do use

hook.Run or

hook.Call, they both call all hooks for said hook name. E.g.



hook.Add("wew", '', function(...)
  print("Calling from hook, arguments: ", ...)
end)

print "Calling all 'wew' hooks"
hook.Run "wew"

print "Calling all 'wew' hooks"
hook.Run("wew", 1, 2, 3)


After the first hook.Run, "Calling from hook, arguments: " will be printed to console. After the second hook.Run, “Calling from hook, arguments: 1 2 3” will be printed.

Edit: ninja’d

You need to put

hook.Call inside your playerflying function.

Your hook.Add/hook.Call does not have to be named playerflying or anything resembling that function name.

Edit: Also Ninja’d lol

-automerge why-

Snipped* I figured out the problem. Thanks.

Follow up question, what determines if my hook is shared, client or server? Is it the function and whether if that function is in shared, client or server?

It’s called in the same state as the function that called hook.Run/Call

Could you link me on how to link shared files to a server file so that I don’t have to make my function global in my shared file? Thanks, you’re a doll.

EDIT: I know I can make the function global under a table, but it seems inefficient and problematic when I can just directly link the files.

If you’re wanting to get information from server to client and vice versa, you’ll most likely have to use the net library. If you’re simply wanting to define a function in a single file so other files can use it, either make a global table or use the return from

include.

E.g. global

file1.lua



MyUniquelyNamedTable = {}

function MyUniquelyNamedTable.MyFunc()
  return 2
end


file2.lua



include "file1.lua" -- this can be done in a separate file, will still work because MyUniquelyNamedTable is global

print(MyUniquelyNamedTable.MyFunc() + 5) -- 7


E.g. include return

file1.lua



local function myFunc()
  return 2
end

return myFunc


file2.lua



local thatFunc = include "file1.lua" -- This'll have to be done in each file you want to have this function

print(thatFunc() + 5) -- 7


You could also just return a local table.

[editline]10th August 2017[/editline]

It’s not inefficent, if you localize the global in the file you’re using it’d have the same effect (though the performance will already be unnoticeable anyway). It’s also that not problematic so long as you use unique table names.

My net libary usage is becoming dangerously high.

I already have a global table but I don’t want to start adding other crap into it because it’s the config.

I’ll just go the include way. Seems easiest and fastest way to do it.

Unrelated but what program is that, OP?
It has GLua syntax!

It’s Sublime.

Notepad++ and Atom also have plugins to show glua syntax. They’re user made.

But notepad is ugly as shit and Atom isn’t as user friendly.

[editline]10th August 2017[/editline]

Sublime 3

[editline]10th August 2017[/editline]

[editline]10th August 2017[/editline]

So another quick question. The hook.Add doesn’t seem to be working. Do I have to also include/AddCSLuaFile into my client side to get the function from shared so it can hook?

Here’s my code.

serverside:



local unrestrain = include "autorun/shared/sh_unrestrain.lua"


sharedside:



local function unrestrain(ply, num)

    ply:EmitSound("weapons/elite/elite_sliderelease.wav")
    local function JSReturnWeapons()

        for k,v in pairs(ply.JSWepTable) do
            ply:Give(v)
        end

        for k,v in pairs(ply.JSAmmoTable) do
            ply:SetAmmo(v, k)
        end
        
        for k,v in pairs(ply.JSAmmoTable2) do
            ply:SetAmmo(v, k)
        end
    end

    if IsValid(ply) then

        hook.Run("JSUnrestrain", ply) -- Creates a hook with the ply that was unrestrained.

        local rbones = ply:GetBoneCount()

        while rbones >= 0 do
            ply:ManipulateBoneAngles(rbones, Angle(0,0,0))
            rbones = rbones - 1
        end

        ply:Freeze(false)
        ply:SetNWBool("JSRestrained", false)
        if num == 1 then
            JSReturnWeapons()
        end
    end
end

return unrestrain


clientside:



hook.Add("JSUnrestrain", "JSUnreCloseMenu", function(JSperp)

		ply:PrintMessage(3, "Unrestrained")

		if JSperp == entperp then

			mainwindow:Close()
			removeJSHooks()
		end
	end)


Only 2 people on the server (Me and me again on a different computer, different steam account and everything), I’m restraining and unrestraining the person who is entperp in the clientside code. The print msg doesn’t print.

EDIT: Not even sure if this is the problem but I assume the server is just making a copy of the function, which in turn makes the hook.Run server side only.