Page 1 of 1

Hook functions

Posted: Wed Sep 19, 2012 9:10 am
by cromcrom
Hi all,

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,
	
	
    }
TX in advance for any help, people :-)

Re: Hook functions

Posted: Wed Sep 19, 2012 9:19 am
by antti
Check out this tutorial I added yesterday: http://www.grimrock.net/modding/how-to- ... ider-eggs/

You can contain the actual functionality mostly in script entities in the dungeon editor and just refer to those scripts, and maybe pass some parameters to them, from the hooks.

Re: Hook functions

Posted: Wed Sep 19, 2012 9:23 am
by cromcrom
Ow, it works there too. I already created a "crom_library", it works really great, I will check this immediately. Thanks for the fast answers, you rock :-)

Re: Hook functions

Posted: Wed Sep 19, 2012 9:29 am
by cromcrom
It works great, a thousand thanks :-)

Re: Hook functions

Posted: Wed Sep 19, 2012 9:37 am
by cromcrom
Looks much cleaner like that ^^

Code: Select all

cloneObject{
   name = "TLCSnail",
   baseObject = "snail",
   lootDrop = {},
	onDie = function(self)
	local randomCheck = math.random(1,10)
	local skillbonusbutcher = 0
	local butcher_loot = "snail_slice"

		skillbonusbutcher = crom_library.checkButcherLevel()
		if randomCheck < 7 + skillbonusbutcher then
		   spawn(butcher_loot, self.level, self.x, self.y, party.facing)
		else 
			crom_library.teststat("willpower", 23, 5, 10)
		end
		if randomCheck < 4 + skillbonusbutcher then
		   spawn(butcher_loot, self.level, self.x, self.y, party.facing)
		end	
		if randomCheck < 0 + skillbonusbutcher then
		   spawn(butcher_loot, self.level, self.x, self.y, party.facing)
		end	
    end,
}