How to check which item is in alcove?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
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 »

Generally speaking

this compares a with string "xxx":
If a == "xxx" then ...

while this compares a with the contents of variable xxx:
if a == xxx then ...
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, it's working!

Code: Select all

function weaponCheck()
  local weaponlist = { "dagger", "legionary_spear", "arrow" }
  if weaponalcove:getItemCount() == 0
  then
    weapondoor:close()
  else
    for j=1,3 do
      for i in weaponalcove:containedItems() do
        if i.name == weaponlist[j]
        then
          weapondoor:open()
          return
        end
      end
    end
    weapondoor:close()
  end
end
now to fill up that big weapon list (or at least the possible weapons the party could have by that point)
Finished Dungeons - complete mods to play
uggardian
Posts: 75
Joined: Sat May 19, 2012 5:53 pm

Re: How to check which item is in alcove?

Post by uggardian »

Could somebody help:

How to make a trap action when alcove is completely empty?
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 »

It's already there in the first part of my script:

Code: Select all

  if weaponalcove:getItemCount() == 0
  then
    weapondoor:close()
You can make anything you want happen at that point
Finished Dungeons - complete mods to play
Betalove
Posts: 10
Joined: Wed Oct 10, 2012 5:30 am

Re: How to check which item is in alcove?

Post by Betalove »

I am trying to set up a puzzle that seems to have been mentioned several times now. I have been typing script and reading this thread for 2 days and still can not get it to work right. There are 4 altars. There are 4 peices of chitin armor. Boots should be on altar 1, mask on 2, greaves and mail on 3 and 4. Everything I have tried opend the door with the placement of the first item without checking the final 3. Here's an example of the most recent one I tried.

Code: Select all

function armordoor()
   -- iterate through all contained items on alcoves, checking for a matching name

local itemFound = false
  for i in altar_1:containedItems() do
    if i.name == "chitin_boots" then 
      for j in altar_2:containedItems() do		
        if j.name == "chitin_greaves" then
          for k in altar_3:containedItems() do		
            if k.name == "chitin_mail" then
              for l in altar_4:containedItems() do
                if l.name == "chitin_mask" then
                  itemFound = true
                end
              end
            end
          end
        end
      end
    end
  end
  if itemFound then	    
    armordoor:open()
  else		
    armordoor:close()	
  end
end
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 »

You may need to check also whether the altars are empty. You might also consider doing each altar separately, and just connecting them all to a counter set to 4 which opens the door on 0
Finished Dungeons - complete mods to play
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: How to check which item is in alcove?

Post by Grimwold »

@betalove... this post earlier may help you.
viewtopic.php?f=14&t=3083&start=30#p35816

Basically... Create function to check if altar contains item:

Code: Select all

    -- This function returns true if the entity contains a given item.
    -- It works for any entity that implements the containedItems() method.
    function containsItem(entity, item)
       for i in entity:containedItems() do
          if i.name == item then
             return true
          end
       end
       -- if we get here the item was not found
       return false
    end
in the same script create a function to check all altars

Code: Select all

    function aretheitemsthere()
      if containsItem(altar_1, "chitin_boots") and containsItem(altar_2, "chitin_greaves")  and  containsItem(altar_3, "chitin_mail")  and containsItem(altar_4, "chitin_mask") 
      then
        armordoor:open()
      else      
        armordoor:close() 
      end
    end
Then in theory you would link every altar to the function aretheitemsthere in the script for ANY event.. and set each altar to ACTIVATE ALWAYS... then every time an item is added to or removed from any of the altars the check is done to see if all altars contain their item.. if so, open the door. otherwise. close the door.
User avatar
Kuningas
Posts: 268
Joined: Wed Apr 11, 2012 10:29 pm
Location: Northern Finland

Re: How to check which item is in alcove?

Post by Kuningas »

Grimwold wrote:@betalove... this post earlier may help you.
viewtopic.php?f=14&t=3083&start=30#p35816

Basically... Create function to check if altar contains item:

Code: Select all

    -- This function returns true if the entity contains a given item.
    -- It works for any entity that implements the containedItems() method.
    function containsItem(entity, item)
       for i in entity:containedItems() do
          if i.name == item then
             return true
          end
       end
       -- if we get here the item was not found
       return false
    end
in the same script create a function to check all altars

Code: Select all

    function aretheitemsthere()
      if containsItem(altar_1, "chitin_boots") and containsItem(altar_2, "chitin_greaves")  and  containsItem(altar_3, "chitin_mail")  and containsItem(altar_4, "chitin_mask") 
      then
        armordoor:open()
      else      
        armordoor:close() 
      end
    end
Then in theory you would link every altar to the function aretheitemsthere in the script for ANY event.. and set each altar to ACTIVATE ALWAYS... then every time an item is added to or removed from any of the altars the check is done to see if all altars contain their item.. if so, open the door. otherwise. close the door.
This, this pure product of genius is what I've been trying the whole day! The other ways worked somewhat, but it was with this that I found the answer at last! Thank you, thank you! I kind of wish I'd found the solution myself, but this saved me a ton of work.
BASILEUS
Betalove
Posts: 10
Joined: Wed Oct 10, 2012 5:30 am

Re: How to check which item is in alcove?

Post by Betalove »

OK, this is what I have so far and no errors. Door does not open after all items are placed. The Altars are pointed to the function areitemsthere. Do I need to addConnector from script to door?



function armordoor()
-- iterate through all contained items on alcoves, checking for a matching name

for i in altar_1:containedItems() do
if i.name == "chitin_boots" then
return true
end

for j in altar_2:containedItems() do
if j.name == "chitin_greaves" then
return true
end

for k in altar_3:containedItems() do
if k.name == "chitin_mail" then
return true
end

for l in altar_4:containedItems() do
if l.name == "chitin_mask" then
return true
end

return false
end
end
end

function areitemsthere()
if containedItems(altar_1, "chitin_boots") and containedItems(altar_2, "chitin_greaves") and containedItems (altar_3, "chitin_mail") and containedItems(altar_4, "chitin_mask")
then
armordoor:open()
else
armordoor:close()
end
end
end
end
User avatar
Filipsan
Posts: 84
Joined: Fri Mar 16, 2012 10:18 am

Re: How to check which item is in alcove?

Post by Filipsan »

Try this:

Code: Select all

function altarHasItem(altar,item_name)
	
	for i in altar:containedItems() do
		if i.name == item_name then
			return true
		end
	end
	
	return false
	
end


function areitemsthere()
	
	
	if altarHasItem(altar_1, "chitin_boots") and altarHasItem(altar_2, "chitin_greaves") and altarHasItem (altar_3, "chitin_mail") and altarHasItem(altar_4, "chitin_mask") then
		armordoor:open()
	else
		armordoor:close()
	end
end
You have to kook every altar with lua entity
Post Reply