Edited: Added the exclusive check option, that only opens when it's the exactly specified items and no others
Edited: Fixed the code, there where some flaws in it.
Or you could do it in a more generic and nifty way:
Code: Select all
function checkForItems()
-- You cant use the alcove and altars connections to trigger, as they do only activate for the first
-- item added, or when the last one is removed, so use a lever or a pressure plate
-- to activate the check instead
if( checkForContainedItem(dungeon_alcove_3, "chitin_boots") ) then
porte1:open()
else
porte1:close()
end
end
function checkForContainedItem(container, itemName)
for i in container:containedItems() do
if( i.name == itemName ) then
return true
end
end
return false
end
But this only checks for one item, so let's expand it to check for multiple items:
Code: Select all
function checkForItems()
-- You cant use the alcove and altars connections to trigger, as they do only activate for the first
-- item added, or when the last one is removed, so use a lever or a pressure plate
-- to activate the check instead
-- Let's make an table of the item names to check
-- Tables in LUA are a bit like array's in c, but still quite different, check the LUA docs
item_name_table = {"chitin_boots","dagger","torch"}
if( checkForContainedItem(dungeon_alcove_3, item_name_table, true ) ) then
porte1:open()
else
porte1:close()
end
end
function checkForContainedItem(container, itemNameTable, exclusive)
if( exclusive ) then
return checkForContainedItemExclusive(container, itemNameTable)
else
return checkForContainedItemUnExclusive(container, itemNameTable)
end
end
function checkForContainedItemUnExclusive(container, itemNameTable)
local items_found = 0
-- The outer loop needs to be the names,
-- else we will trigger the items_found count several times for the same name.
for id,names in ipairs(itemNameTable) do
for i in container:containedItems() do
if( i.name == names) then
-- If the item name and the item matches, add to the counter
items_found = items_found+1
-- Now we need to break out of this check to avoid triggering the same name twice
-- A break exits the current/inner loop, but the outer loop will continue
break
end
end
end
-- Now we check if we have found as many items as there are names in the table
if( items_found == table.getn( itemNameTable ) ) then
-- Return the value true, this also ends the function
return true
end
-- Return false in all other cases, no need for an else in the above "if" as the function end's as soon
-- it hit's a "return"
return false
end
function checkForContainedItemExclusive(container, itemNameTable)
items_found = 0
-- Here we use the container as outerloop as we will return false as soon we get a missmatch
for i in container:containedItems() do
name_check = false
for id,names in ipairs(itemNameTable) do
if( i.name == names) then
-- If the item name and the item matches, add to the counter, and set the name_check to true
items_found = items_found+1
name_check = true
-- Now we need to break out of this check to avoid triggering the same name twice
-- A break exits the current/inner loop, but the outer loop will continue
break
end
end
-- If name_check is false, then we found a item not named in our table, so return false
if( name_check == false ) then
return false
end
end
-- Now we check if we have found as many items as there are names in the table
-- This check wont pass if there are more of one item type then allowed.
if( items_found == table.getn( itemNameTable ) ) then
-- Return the value true, this also ends the function
return true
end
-- Return false in all other cases, no need for an else in the above "if" as the function end's as soon
-- it hit's a "return"
return false
end
And there we have it, this functions works for all container objects, so both Alcove and Altar.
The code is tested and works, after some fixes