Page 1 of 1

Detect that item is food?

Posted: Mon Jul 01, 2013 10:39 pm
by jsi7iv
I've just started working on my very first LoG custom maps, and I've hit a snag. I want to create an alcove that requires food to be placed on it before the party is able to progress through my map; however, I can't find a way to see if an item has a nutritionValue!

I know I can do this

Code: Select all

function itemPuzzle()
   -- iterate through all contained items on alcove, checking for a matching name
   for i in itemPuzzleAlcove:containedItems() do
      if i.name == "pitroot_bread" then
         -- do something like open a door or activate a counter
         break
      end
   end
end
I want to do this (or something like it to detect food)

Code: Select all

function itemPuzzle()
   -- iterate through all contained items on alcove, checking for a matching name
   for i in itemPuzzleAlcove:containedItems() do
      if i.nutritionValue  then
         foodCounter:increment()

         break
      end
   end
end

Any Ideas?

Thanks in advance!

Re: Detect that item is food?

Posted: Tue Jul 02, 2013 1:29 am
by Isaac
Well... since most of the definition values are nil for the check (except 'Class') I didn't see how to do it that way.
But the brute force method (your first example) does work; just add the full list.

Code: Select all

function itemPuzzle()
-- iterate through all contained items on alcove, checking for a matching name
for i in itemPuzzleAlcove:containedItems() do
	
-- Supported foods (add any new food names to the list)
	food = {
		"pitroot_bread",
		"rotten_pitroot_bread",
		"rat_shank",
		"boiled_beetle",
		"baked_maggot",
		"ice_lizard_steak",
		"mole_jerky",
		"blueberry_pie",
		"grim_cap",
		"herder_cap",
		"snail_slice",
		}
		
--Check if it's food
		for x in pairs(food) do
			if i.name == food[x] then
			print("It's food!")
                        foodCounter:increment()
			return
		end
		end
		
--Default case for when it's not food
	print("It's not food!")

end
end

Re: Detect that item is food?

Posted: Tue Jul 02, 2013 5:04 am
by jsi7iv
Isaac,

Thanks a bunch for the post! I was hoping to avoid the brute force approach as I was thinking about altering the puzzle to require that players place food items totaling some nutritional value. That way they can't just get rid of a couple of there "baked_maggot"'s; however, your solution will do exactly what I need.

Thanks :)

Re: Detect that item is food?

Posted: Tue Jul 02, 2013 8:23 am
by msyblade
indeed, the brute force approach is always effective, you should always weigh the downside of such an aspect, but in this case, the only sacrifice is blood, sweat and tears. so, iterate every item and DONE. Bravo! GREAT solution!