I have a function in my addon I would like to make available to another addon. Is it possible to turn this function into a hook so the other addon can hook into it?
Name it
[lua]function GM:FunctionName( arg1, arg2, arg3 )
// Logic
end[/lua]
Then, where you normally call it, use
[lua]hook.Call( "FunctionName", GAMEMODE, arg1, arg2, arg3 )[/lua]
Then any addon can hook into it by using
[lua]hook.Add( "FunctionName", "LocalNameOfHook", function( arg1, arg2, arg3 )
// Magic
end )[/lua]
You'd make a function, then do hook.Call as far as my knowledge goes.
As an example, in TTT, when you order equipment, "hook.Call("TTTOrderedEquipment", GAMEMODE, ply, id, is_item)" exists at the end, so I assume once the function is ran, it will call that hook (creating it?) and it becomes something everyone else can use.
This is far as my research went, and if I'm a dumbshit, just rate me dumb and ignore me.
What if said function I was trying to create a hook out of was "cleanup.Add" I could not find an existing hook for this function.
There may be another way to do this, but here's how I redefined the print function:
[lua]// Make a link to the original function so it's not lost
if ( !CLEANUP_ADD ) then CLEANUP_ADD = cleanup.Add; end
// Redefine the function
function cleanup.Add( ... )
// Call your hook
hook.Call( "CleanupAdd", GAMEMODE, unpack( ... ) )
// Call the original function
CLEANUP_ADD( unpack( ... ) )
end
// Define the hook
function GM:CleanupAdd( ... )
// Logic or blank
end[/lua]
Ah of course! Thank you Acecool your the bomb!
I'm getting confused when I see "unpack( ... )" is that just a placeholder for the example?
EDIT: It looks like its passing the vars for Cleanup.Add, I can be quite dumb sometimes!
Say you have a table, and you want to pass that table as separate/individual args to a function.
You unpack it.
So I could to print( unpack( { "one", "two", "three" } ) ) and it should print just fine. It turns one table argument into n arguments.
Might be dumb, but I don't think you need to unpack varargs, you only would if you already put it into a table, like unpack( { ... } )
which wouldn't make sense because it's just going in a big circle
You'd just do CLEANUP_ADD( ... )
Works flawlessly Acecool, thank you once again!
You might be right on that Bl00d. I didn't test the above code.
HOLD THE BOAT! :P
I'm getting
[code]attempt to index global 'GAMEMODE' (a nil value)
and
attempt to index global 'GM' (a nil value
for
function GAMEMODE:CleanupAdd(Player, Type, Entity)
and
function GM:CleanupAdd(Player, Type, Entity)
[/code]
I tried both ways, GM and GAMEMODE
EDIT:
Using GAMEMODE works only if I reload the script after the game has started..
Sorry, you need to Log In to post a reply to this thread.