Return to original function after calling hook.

Is it possible to add a hook for a function, do something then return to the normal function? Instead of completly overriding that function I just need to add something but want it to continue on.

Could you give a example of what you mean? I don’t understand your original question.

Give an example of what you mean, because that is exactly how hooks work (as long as you don’t return a value then the rest of the hooked functions will also be called).

Lets just say I call the function when a player dies. I want them to spawn as normal but also print what killed them. lets say the normal death function just respawns the player. I want to hook that function, such that I say something like “Print(attacker)”, then return to the normal death function to have them respawn like normal.

so, just detect when the hook is called, do something, then go back and do what the original function is supposed to do. When I write hooks it seems to just override, so in my example if I were to just have print(attacker) it wouldn’t go back and spawn the player without me telling it to.

Since you haven’t posted any code I’m going to have to make some assumptions.

Don’t write functions like[lua]function GAMEMODE:PlayerDeath–GAMEMODE might be GM, not 100% sure[/lua]as that will cause them to overwrite each other. Only do it that way if you are writing a gamemode / want to override all the functions.

You should write them using hook.Add

[lua]local function MyDeathFunction(victim,attacker,inflictor)
print(victim,“killed by”,attacker)
end
hook.Add(“PlayerDeath”, “My Unique Name”, MyDeathFunction)[/lua]

That would call your function without affecting the default functions in any way.

If you wanted to prevent any other hooked functions from running, you can return a value.
[lua]local function MyDeathFunction(victim,attacker,inflictor)
print(victim,“killed by”,attacker)
return true --returning anything other than nil will make it stop calling the rest of the functions
end
hook.Add(“PlayerDeath”, “My Unique Name”, MyDeathFunction)[/lua]

Does that help?

Your second example is how I current write the hooks. So, when I don’t return anything it stops the original function from running? I havn’t been returnning anything. I’ll do a bit more testing.

Yep. When a hook is called, the game loops through all the functions added by hook.Add() until one of them returns something. If none of them return anything, it calls GM:SomeHook() if it exists.

You can use this yourself as well, which barely anyone does:
[lua]
– In a base gamemode:
function GM:EndRound(reason)

if self.Round < self.Config.RoundLimit then
timer.Create(“StartRound”, self.RoundEndLength, 1, function()
hook.Run(“StartRound”)
end)
else
timer.Create(“ChangeMap”, self.RoundEndLength, 1, function()
hook.Run(“ChangeMap”)
end)
end

end

– In a derived gamemode, or something…
hook.Add(“StartRound”, “CTF_ResetFlags”, function()
for k, ent in pairs(ents.FindByClass(“up_ctf_flag”)) do
ent:SendHome()
end
end)
[/lua]

you don’t need to replace the function, just use hook.Add