I am using some custom functions within the objects hooks (a "woodcutter" function that checks a "woodcutter" skill in the onDie of a barrel, for example).
Right now, I have to define all the custom functions within the onDie hook. Is there a more convenient way to store all the functions in a common place ?
The code below works great, although it is messy, and I would like to clean it a bit, and make a more convenient use of the functions.
Code: Select all
cloneObject{
name = "woodpile",
uiName = "Wood Pile",
baseObject = "barrel_crate_block",
health = 50,
onDie = function(self)
local randomCheck = math.random(1,10)
local skillbonus = 0
function itemCheck(thisItem)
local thisPlayer = 1
local itemSlot = 1
local currItem
for thisPlayer=1,4,1 do
for itemSlot=1,31,1 do
if party:getChampion(thisPlayer):getItem(itemSlot) ~= nil then
currItem = party:getChampion(thisPlayer):getItem(itemSlot).name
if currItem == thisItem then
return true
end
end
itemSlot = itemSlot + 1
end
thisPlayer = thisPlayer + 1
end
end
function checkWoodcutterLevel()
local skillLevel = -2
if itemCheck("novicewoodcutter") then skillLevel = 1 end
if itemCheck("adeptwoodcutter") then skillLevel = 2 end
if itemCheck("masterwoodcutter") then skillLevel = 3 end
if itemCheck("legendarywoodcutter") then skillLevel = 5 end
return skillLevel
end
function teststat(stat, testmaxvalue, xpmin, xpmax)
local astat = 0
local aname = ""
for i=1,4 do
astat = party:getChampion(i):getStat(stat)
aname = party:getChampion(i):getName()
if astat < math.random(testmaxvalue) then
hudPrint(aname.." doesn't learn anything.")
else
local experiencegained = math.random(xpmin,xpmax)
hudPrint(aname.." succeeds to understand what is going on and gain "..experiencegained.." XP." )
party:getChampion(i):gainExp(experiencegained)
end
end
end
skillbonus = checkWoodcutterLevel()
if randomCheck < 4 + skillbonus then
spawn("wood_branch", self.level, self.x, self.y, party.facing)
else
teststat("willpower", 23, 5, 10)
end
if randomCheck < -1 + skillbonus then
spawn("wood_branch", self.level, self.x, self.y, party.facing)
end
end,
}