How to check which item is in alcove?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
Neonox
Posts: 1
Joined: Thu Sep 13, 2012 6:09 pm

How to check which item is in alcove?

Post 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.
User avatar
antti
Posts: 688
Joined: Thu Feb 23, 2012 1:43 pm
Location: Espoo, Finland
Contact:

Re: How to check which item is in alcove?

Post 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
Steven Seagal of gaming industry
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: How to check which item is in alcove?

Post 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
guigr
Posts: 16
Joined: Wed Apr 11, 2012 10:58 pm

Re: How to check which item is in alcove?

Post 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:
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: How to check which item is in alcove?

Post 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)
Finished Dungeons - complete mods to play
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: How to check which item is in alcove?

Post 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.
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: How to check which item is in alcove?

Post 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)"
Finished Dungeons - complete mods to play
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: How to check which item is in alcove?

Post 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?
Finished Dungeons - complete mods to play
User avatar
Neikun
Posts: 2457
Joined: Thu Sep 13, 2012 1:06 pm
Location: New Brunswick, Canada
Contact:

Re: How to check which item is in alcove?

Post 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.
"I'm okay with being referred to as a goddess."
Community Model Request Thread
See what I'm working on right now: Neikun's Workshop
Lead Coordinator for Legends of the Northern Realms Project
  • Message me to join in!
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: How to check which item is in alcove?

Post 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???
Finished Dungeons - complete mods to play
Post Reply