Weird stuff with hooks?

I have an entity, and a function hooked to **[Gamemode.EntityTakeDamage

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.EntityTakeDamage)**.

My issue is that

[lua]

function ArmorScaleDamage( ent, inflictor, attacker, amount, dmginfo )
//stuff
end

hook.Add(“EntityTakeDamage”,“ArmorScaleDamage”, ArmorScaleDamage)
[/lua]

works, but

[lua]
function ENT:ArmorScaleDamage( ent, inflictor, attacker, amount, dmginfo )
//stuff
end

hook.Add(“EntityTakeDamage”,“ArmorScaleDamage”, ENT.ArmorScaleDamage)
[/lua]

doesn’t.

What happens is that the “ent” variable is removed if I add the "ENT"s to ArmorScaleDamage’s name. So the “real” arguments being used with ENT.ArmorScaleDamage are not (ent, inflictor, attacker, amount, dmginfo), but rather (inflictor, attacker, amount, dmginfo, nil).

While I can live with using the first example, I want to know why adding “ENT” to my function’s name randomly gets rid of the first argument.

Thanks.

Sorry if this was horribly confusing.

It’s because when you use a colon on a function it automatically creates an invisible first argument called self. Replace the colon with a period and you’ll do fine.

Here’s what I think, in your second function you (probably) can call the entity using self.Entity but in your first one you can’t, hence the use of the ent argument.

Correct, thank you.

When you hook to an entity you must hook to it in ENT:Init() like so:

[lua]
function ENT:Init()
hook.Add(“TheHook”, “TheHook-”…self:EntIndex(), self.MyFunction)
end
[/lua]

No. The hook should be added outside any function (or else it’ll be added every time a new entity is spawned) and giving it a unique hook ID would just serve to clutter the hook table with several instances of the same function.

In most cases though hooks should be added like in your example, but not in this case:wink: