ent:AddCallback() to detect damage

Is it possible to use an ent:AddCallback on an entity to detect if it was shot and possibly how much damage it sustained?

Yes, here is something I wrote quick. It is not tested but it should work.
[lua]
Entity:AddCallback(“OnTakeDamage”, function(dmginfo)
local dmgtaken = dmginfo:GetDamage()
end)
[/lua]

Edit:
I realised that there are only a select amount of functions you can call back, tell me if it does not work.

no, but you can override the function instead.


local ent = FindMetaTable("Entity")

local oldOnTakeDamage = ent.OnTakeDamage
function ent:OnTakeDamage(dmginfo)
    oldOnTakeDamage()
    -- code here
end

I’m not sure you can. These are functions that are used by the class itself…

Why don’t you just make an EntityTakeDamage hook with hook.Add? Am I missing something?

this is what I thought too, there must have been a particular reason to not just hook it

I am confused. Why don’t you just use:



function ent:OnTakeDamage(DamageInfo)
	local Dmg = DamageInfo:GetDamage()

end


Because that doesn’t work on engine entities, only sents, and it is also a bad idea in the event the entity already does something else with OnTakeDamage

Ah, I see, didn’t really think he was doing it on an already existing entity, was guessing he is making his own. Didn’t really say that XD