Page 1 of 8

How to check which item is in alcove?

Posted: Thu Sep 13, 2012 6:26 pm
by Neonox
How exactly would I got about checking which item(s) is/are in an alcove?

The documentation mentions Alcove:containedItems() but I can't for the life of me figure out how to use it.

Re: How to check which item is in alcove?

Posted: Thu Sep 13, 2012 7:03 pm
by antti
You need to iterate through the names of the containedItems. It's a little complicated but here's a small script I whipped up to demonstrate how you can do it/what you can do with it:

Code: Select all

function checkAlcoveForItems()
	-- change these variables to match your alcove's ID and the item you want to test for
	local itemName = "dagger"
	local alcoveID = dungeon_alcove_1
	
	-- iterate through all the contained items on alcove, checking for a matching name
	for i in alcoveID:containedItems() do
		if i.name == itemName then
			itemFound()
		else
			itemNotFound()
		end
	end
end

-- run this script _once for each item_ with a matching name
function itemFound()
	playSound("level_up")
end

-- run this script _once for each item_ without a matching name
function itemNotFound()
	spawn("poison_cloud", party.level, party.x, party.y, 0)
end

Re: How to check which item is in alcove?

Posted: Thu Sep 13, 2012 7:37 pm
by petri
Another example (not tested):

Code: Select all

-- a helper function which returns true if alcove contains the given item
function containsItem(alcove, item)
	for it in alcove:containedItems() do
		if it.name == item then
			return true
		end
	end
	return false
end

-- example usage
if containsItem("alcove1", "dagger") then
	print("alcove contains a dagger")
else
	print("alcove does not contain a dagger")
end

Re: How to check which item is in alcove?

Posted: Thu Sep 13, 2012 8:23 pm
by guigr
First of all, well done for this wonderful editor!
I have a somewhat related problem with the daemon head.
Using the editor I can say that each eye socket can activate a door using a gem. But I can't make the door open on the condition that BOTH gems are inserted.

I know nothing about lua but I wanted to give it a go. The problem is that there's no reference to the daemon head in the ressources and I couldn't guess what term to use.
Is it something like eye_socket_left_1:containedItems() or something else.

Edit: Nevermind, just found out that it works like locks with "counter" :oops:

Re: How to check which item is in alcove?

Posted: Tue Sep 25, 2012 8:53 pm
by Komag
I want a door to open when putting the correct item in an alcove, but for the door to shut when taking it out. when I tried this:

Code: Select all

function gemCheckblue(alcoveblue, blue_gem)
	for i in alcoveblue:containedItems() do
		if i.name == "blue_gem" then
			return true
		end
	end
	return false
end
if gemCheckblue("alcoveblue", "blue_gem") then
	doorblue:open()
else
	doorblue:close()
end
I just get a crash-to-editor with "attempt to call method 'containedItems' (a nil value)" next to the second to top line.
If I try this:

Code: Select all

function gemCheckblue()
	for i in alcoveblue:containedItems() do
		if i.name == "blue_gem" then
			doorblue:open()
		else
			doorblue:close()
		end
	end
end
It works to open the door, but the door won't close when I take out the blue gem (although it will close if it put in some other wrong item)

Re: How to check which item is in alcove?

Posted: Tue Sep 25, 2012 9:50 pm
by petri
The first one crashes because in your code the first argument alcoveblue has a string value when you need an object reference. So you are trying to do "alcoveblue":containedItems() which obviously doesn't work.

Re: How to check which item is in alcove?

Posted: Tue Sep 25, 2012 10:03 pm
by Komag
...[trying to comprehend...]

I'm afraid my attempt to comprehend failed. I'm guessing I need quotes somewhere I don't have them or else I need to remove quotes somewhere I have them?

If I remove the quotes in the "if gemCheckblue(alcoveblue, blue_gem) then" line, it will run, but as soon as I place a blue gem, it crashes with "Attempt to index local 'alcoveblue' (a nil value)"

Re: How to check which item is in alcove?

Posted: Tue Sep 25, 2012 10:10 pm
by Komag
using the simpler script:

Code: Select all

function gemCheckblue()
   for i in alcoveblue:containedItems() do
      if i.name == "blue_gem" then
         doorblue:open()
      else
         doorblue:close()
      end
   end
end
this works to open the door, but not to close it when removing the gem. I have the alcove lined to the script with "any". If I add a link from the alcove directly to the door with "deactivate-close" it works, but why doesn't the script do it?

Re: How to check which item is in alcove?

Posted: Tue Sep 25, 2012 10:24 pm
by Neikun
I have two mouth sockets attached to a counter with activation as decrement and deactivation as increment.
Counter intial state at 2.
Hook the counter to the door(s) and voila, requires both sockets to be filled before the door will open.
I believe this is can be adapted to your problem.

Re: How to check which item is in alcove?

Posted: Tue Sep 25, 2012 10:30 pm
by Komag
Yes I'm very familiar with that general solution (a good one usually!). Unfortunately that doesn't quite apply to my problem in this case. In fact, if I set it up with a counter, the deactivate-increment never happens, so putting in the gem the first time decrements and opens the door, but taking out doesn't increment, thus the door stays open, and then putting back in decrements further, closing the door, never to open again

so the problem is, why on earth doesn't the

Code: Select all

else
     dosomething
ever do the something???