Converting Actor.lua code into hooks
Posted: Sat Jul 14, 2012 6:00 pm
Hi, could someone walk me through this problem I've been having? I know nothing about code, so treat me like a total noob. 
I want to convert the code for a skill in Dekar's Ranger addon into a hook. Here's the stuff I stripped out of Dekar's edited Actor.lua file with (what I think) is all the relevant code for the skill that tells the pet to defend the ranger at certain times.
I don't know what most of it means, but what I'm concerned with is how to get it into hook format. Here is the stripped code from the example addon:
Or, a working example from greyswandir's Monk addon:
I'm mostly interested in knowing how to tell which functions to call and how to name them and such. Any help would be greatly appreciated and I think a few other addon authors around here would find this information valuable too. I'm trying to put together a player class pack without any compatibility issues, and I'm pretty close but this is the only thing holding me back right now. Thanks in advance!

I want to convert the code for a skill in Dekar's Ranger addon into a hook. Here's the stuff I stripped out of Dekar's edited Actor.lua file with (what I think) is all the relevant code for the skill that tells the pet to defend the ranger at certain times.
Code: Select all
function _M:onTakeHit(value, src)
if self:attr("protect") then
if rng.percent(self.protect_chance) and not (self.ranger_pet==nil or self.ranger_pet.dead)then
local distance = core.fov.distance(self.x, self.y, self.ranger_pet.x, self.ranger_pet.y )
if (distance < 2) then
game.logSeen(self, "%s protects %s!", self.ranger_pet.name, self.name)
self.ranger_pet:takeHit(value, src)
value = 0
end
end
end
end
Code: Select all
class:bindHook("Actor:takeHit", function(self, data)
if self:hasEffect(self.EFF_DIPLOMATIC_IMMUNITY) then data.value = 0 return true end
end)
Code: Select all
local takeHit_hook = function(self, data)
-- Ki Shield
if self.isTalentActive and
self:knowTalent(self.T_KI_SHIELD) and
self:isTalentActive(self.T_KI_SHIELD)
then
local t = self:getTalentFromId(self.T_KI_SHIELD)
local pct = t.getPercent(self, t)
local absorb = math.min(data.value * 0.5, self:getStamina() / pct)
self:incStamina(-absorb * pct)
data.value = data.value - absorb
end
return true
end
class:bindHook("Actor:takeHit", takeHit_hook)