Module Working, but not functioning properly

Ok, i am making a ‘remake’ of the hook module, but everything failed so i need ya help :3

[LUA]local ipairs = ipairs
local unpack = unpack
local print = print

module(“Call”)

local HOOKS = { }

function Add( Call , Name , Func )
if Call && Name && Func then
HOOKS[Call] = { }
HOOKS[Call][Name] = Func
print("Hooked "… Name … " To " … Call)
end
end

/*
– Add :

Call.Add(“PlayerInitialSpawn”, “TEST2” , function( ply )
ply:Kill()
end

// Exaclty like Hook.Add , but without all the engine hooks
*/

function Prime( Call, … )
if Call then
local arg = {…}
HOOKS[Call] = { }
for k,v in ipairs( HOOKS[Call] ) do
local func = v
func( unpack( arg ) )
end
print("Added “… Call … " Hook” )
end
end

/*
– Prime :

hook.Add(“PlayerInitialSpawn”, “TEST”, function( ply )
Call.Prime( “PlayerInitialSpawn”, ply )
end

// As you can see, its like Hook.Call , but less complexe
*/

function RemovePrime( Name )
if Name then
HOOKS[Name] = nil
end
end

/*
– RemovePrime :
– WARNING: MISSUSE MIGHT BRAKE EVERYTHING USING THIS MODULE!!!

Call.RemovePrime( “PlayerInitialSpawn” )

// Removes the Whole hook , beware
*/

function Remove( Call, Name )
if Call && Name then
HOOKS[Call][Name] = nil
end
end

/*

– Remove :

Call.Remove( “PlayerInitialSpawn”, “TEST2” )

// Removes the hook we created earlier
*/
[/LUA]

no errors when i test it, but then again it doesn’t actual do what i lua’d it to do :frowning:


function Add( Call , Name , Func )
	if Call && Name && Func then
		HOOKS[Call] = { }
		HOOKS[Call][Name] = Func
		print("Hooked ".. Name .. " To " .. Call)
	end
end


You are remaking the table every time you add a hook, this will remove all previous functions that are hooked to the same hook.


function Prime( Call, ... )
	if Call then
		local arg = {...}
		HOOKS[Call] = { }
		for k,v in ipairs( HOOKS[Call] ) do
			local func = v
			func( unpack( arg ) )
		end
		print("Added ".. Call .. " Hook" )
	end
end


You are remaking the HOOK[Call] table then looping through it. (it will be empty)

OHH KAYY!!! Thanks, now all i need to do is find out how to fix this

Can I just ask why you are doing this?

Meh, i keep overwriting Hooks with hooks and stuff like that, i use it so much i don’t think there are any english words left to use :3 . And anyway, if it works, i will know how i did it, and i will learn from it, and know how to edit it in the near futur. For example, if i see soemthing nice, i will make it, not download it, like that i know how it works

[editline]09:00PM[/editline]

fidled with the code, but not working still :

[LUA]

local ipairs = ipairs
local unpack = unpack
local print = print

module(“Call”)

local HOOKS = { }

function Add( Call , Name , Func )
if Call && Name && Func then
local pan = HOOKS[Call]
pan[Name] = Func
print("Hooked "… Name … " To " … Call)
end
end

function Prime( Call, … )
if Call then
local arg = {…}
local pan = HOOKS[Call]
for k,v in ipairs( pan ) do
local func = v
func( unpack( arg ) )
end
print("Added “… Call … " Hook” )
end
end

function RemovePrime( Name )
if Name then
HOOKS[Name] = nil
end
end

function Remove( Call, Name )
if Call && Name then
HOOKS[Call][Name] = nil
end
end

[/LUA]

I am using this Code with it

[LUA]

require(“Call”)

hook.Add(“PlayerInitialSpawn”, “TESTINGBETA”, function ( ply )
Call.Prime(“PlayerISpawn”, ply )
end)

concommand.Add(“LOBBY_TEST”, function( ply )
Call.Prime(“PlayerISpawn”, ply )
end)

Call.Add(“PlayerISpawn”, “LOL” , function( ply )
print(“It WORKED!!!”)
end)

[/LUA]

And i get the error :





Hook 'TESTINGBETA' Failed: includes/modules/Call.lua:21: bad argument #1 to 'ipairs' (table expected, got nil)



ipairs is for sequential integer indexed tables, use pairs.

ok, working on that, but the error said it didn’t get the table, but it got nil ( nothing i think )

[editline]09:16PM[/editline]

Yep, same error

Why don’t you just copy the contents of hook.lua and just rename the module?

Cause i can’t find it so i just thorght it was internal?

I can’t find it either. But here is the one from the LuaBin.

[lua]// Globals that we are going to use
local pairs = pairs
//local unpack = unpack
local Error = Error
local ErrorNoHalt = ErrorNoHalt
local pcall = pcall
local tostring = tostring
local concommand = concommand
local PrintTable = PrintTable
local CLIENT = CLIENT
local Msg = Msg
local type = type

/---------------------------------------------------------
Name: hook
Desc: For scripts to hook onto Gamemode events
---------------------------------------------------------
/
module(“hook”)

// Local variables
local Hooks = {}

// Exposed Functions

/---------------------------------------------------------
Name: Hooks( )
Desc: Returns the hook table
---------------------------------------------------------
/
function GetTable()

return Hooks

end

/---------------------------------------------------------
Name: Add( event_name, name, function )
Desc: Adds a hook
Usage: hook.Add( “Think”, “MyHookName”, MyThinkFunction )
---------------------------------------------------------
/
function Add( event_name, name, func )

if (Hooks[ event_name ] == nil) then
	Hooks[ event_name ] = {}
end

Hooks[ event_name ][ name ] = func

end

/---------------------------------------------------------
Name: Remove( name )
Desc: Removes a hook
Usage: Remove( “Think”, “MyHookName” )
---------------------------------------------------------
/
function Remove( event_name, name )

Hooks[ event_name ][ name ] = nil

end

/---------------------------------------------------------
Name: Call( name, args )
Desc: Called by the engine to call a gamemode hook
---------------------------------------------------------
/
function Call( name, gm, … )

local b, rA, rB, rC, rD, rE, rF, rG, rH
local HookTable = Hooks[ name ]


if ( HookTable != nil ) then

	for k, v in pairs( HookTable ) do 
		
		if ( v == nil ) then
		
			ErrorNoHalt("Hook '"..tostring(k).."' tried to call a nil function!

")
HookTable[ k ] = nil // remove this hook
break;

		else
			// Call hook function
			b, rA, rB, rC, rD, rE, rF, rG, rH = pcall( v, ... )
			
			if (!b) then
			
					ErrorNoHalt("Hook '"..tostring(k).."' Failed: "..tostring(rA).."

")
HookTable[ k ] = nil // remove this hook

			else
			
				// Allow hooks to override return values
				if (rA != nil) then
					return rA, rB, rC, rD, rE, rF, rG, rH
				end
				
			end
		end
		
	end
end

if ( gm ) then

	local GamemodeFunction = gm[ name ]
	if ( GamemodeFunction == nil ) then return nil end
	
	if ( type( GamemodeFunction ) != "function" ) then
		Msg( "Calling Non Function!? ", GamemodeFunction, "

" )
end

	// This calls the actual gamemode function - after all the hooks have had chance to override
	b, rA, rB, rC, rD, rE, rF, rG, rH = pcall( GamemodeFunction, gm, ... )
	
	if (!b) then
	
		gm[ name .. "_ERRORCOUNT" ] = gm[ name .. "_ERRORCOUNT" ] or 0
		gm[ name .. "_ERRORCOUNT" ] = gm[ name .. "_ERRORCOUNT" ] + 1
		ErrorNoHalt( "ERROR: GAMEMODE:'"..tostring(name).."' Failed: "..tostring(rA).."

" )
return nil

	end
	
end

return rA, rB, rC, rD, rE, rF, rG, rH

end[/lua]

http://luabin.foszor.com/code
:science:

Your best ressource after the wiki.

Copying the hook module won’t work. The engine specifically calls the call function in the hook table, not any random similarly named function.

You would call just call your own call function in your gamemode. But to be honest this idea is pretty stupid, you are never going to run out of unique names for your hooks.

True, still a good learning exercise. Of course you don’t learn much from fopy pasting a module.

Could even do something like this.

[lua]function hook.AddRand(typ , func)
local name = “”
name = name…string.char(math.random(65 , 117))
while hook.GetTable()[typ][name] and name != “” do
name = “”
for i = 1 , 10 do
name = name…string.char(math.random(65 , 117))
end
end
hook.Add(typ , name , func)
end
[/lua]

Anyways, i found it in the GCF befor froszor posted, and i proceded to change what i needed and it works a marvel! I have to say, thank you for sujesting to use the hook module. But MakeR , i also wanted my own hook module so that i could rename a couple of hooks without overiding them ( hard to explain why, but otherwise my gamemode will go kapoote [ german for bang ] )