How to check which item is in alcove?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Betalove
Posts: 10
Joined: Wed Oct 10, 2012 5:30 am

Re: How to check which item is in alcove?

Post by Betalove »

Now I have a seperate script for each altar. Each altar has a connector pointed to areitemsthere function. I have also tried pointing each altar to the altarhasitem function with the door not opening after all items are in place. I have tried adding connectors to both functions for each altar with the same result. The door does not open. ACK! maybe there is an easier way... I have checked all of the names and ID's. The door ID is amordoor. Altars are altar_1 - altar_4. Scripts are ID'd amoralcove_1 - armoralcove_4.

function altarHasItem(altar_1,chitin_boots_1)

for i in altar_1:containedItems() do
if i.name == "chitin_boots" 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
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 »

Please, use Code formatting, thanks :)

Again - link every altar with Lua entity (function CheckAltars)

Lua should have this code (not tested, i'm not sure about the multiple-row if syntax...)

Code: Select all

function altarHasItem(altar,item_name)
	
	print("checking for "..item_name)

	for i in altar:containedItems() do
		if i.name == item_name then
			print(item_name.."is there")
			return true
		end
	end

	print(item_name.."is not there")
	return false
end


function CheckAltars()

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
Betalove
Posts: 10
Joined: Wed Oct 10, 2012 5:30 am

Re: How to check which item is in alcove?

Post by Betalove »

I have it responding correctly with the first item placed. Then it checks the second item before it is place and returns a false. Each item placed after that is returning a false for the first item. Maybe a counter? Not sure how to set one up though. Also I have 4 scripts. One for each altar. Should this all be in one script entity?

Code: Select all

function altarHasItem(altar_1,item_name)

     print("checking for "..item_name)
   
     for i in altar_1:containedItems() do
           if i.name == "chitin_boots" then
                print(item_name.." is there")
        	return true
           end
     end
	
	
	print(item_name.." is not there")
   
  	return false
      
   end



function checkAltars()
   
   
   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
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 »

Betalove wrote:... Also I have 4 scripts. One for each altar. Should this all be in one script entity?
Yes, all in one entity
Betalove
Posts: 10
Joined: Wed Oct 10, 2012 5:30 am

Re: How to check which item is in alcove?

Post by Betalove »

It's comming along now. However, all I have to do is place mask on altar_4 and the door opens. Also the answers are still comming back false.

Code: Select all

function altarHasItem(altar,item_name)

	print("checking for "..item_name)
   
   	for i in altar_1:containedItems() do

      	     if i.name == "chitin_boots" then

	         print(item_name.." is there")
			
        	         return true
			
	   end
		
               end
	
			
	       print(item_name.." is not there")
   
  	        return false
	
end
      

function altarHasItem(altar,item_name)

                print("checking for "..item_name)
   
   	for j in altar_2:containedItems() do

      	    if j.name == "chitin_greaves" then

	         print(item_name.." is there")
			
        	         return true

	    end
		
	end
	
	        print(item_name.." is not there")
   
  	        return false 

	end
	
function altarHasItem(altar,item_name)

                print("checking for "..item_name)
   
   	for k in altar_3:containedItems() do

      	     if k.name == "chitin_mail" then

	          print(item_name.." is there")
			
        	          return true

	    end

	end
	
	        print(item_name.." is not there")
   
  	        return false
      
   	end

function altarHasItem(altar,item_name)

                print("checking for "..item_name)
   
   	for l in altar_4:containedItems() do

      	      if l.name == "chitin_mask" then

	           print(item_name.." is there")
			
        	           return true
	     end
	end
	
	           print(item_name.." is not there")
   
  	           return false
      
   	end


function checkAltars()
   
   
   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
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 can only have one function per function name.
Let me get back to you more on this in just a minute...
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 »

I think it's better to split the functions into just two parts as some of the previous examples show. Here is something that works well for me:

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 alcoveContents(entity, item)
  for i in entity:containedItems() do
    if i.id == item then
      return true
    end
  end
-- if we get here the item was not found
  return false
end

-- function actually used to check all the alcoves
function alcoveCheck()
  if alcoveContents(coolAlcove1, "coolScroll1") and
     alcoveContents(coolAlcove2, "coolScroll2") and
     alcoveContents(coolAlcove3, "coolScroll3") and
     alcoveContents(coolAlcove4, "coolScroll4")
  then
    coolDoor:open()
  else
    coolDoor:close()
  end
end
The whole thing is MUCH cleaner and shorter than your current approach. In this example I have i.id instead of i.name so I could find out specific items instead of just types of items, but you would probably use i.name for your case.
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 would still need to use the function alcoveContents four differnt times. One for each item right? v I think part of the trouble I am having is knowing where to put the name of the item or the word "item and the name of the alcove or the word entity.
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 right there four times in the second function:

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 alcoveContents(entity, item) -- CUSTOMIZE: PICK A NAME FOR THIS FUNCTION, I PICKED "alcoveContents". LEAVE ENTITY, ITEM AS THEY ARE
  for i in entity:containedItems() do
    if i.id == item then -- CUSTOMIZE: YOU MAY WANT TO USE i.name INSTEAD OF i.id
      return true
    end
  end
-- if we get here the item was not found
  return false
end

-- function actually used to check all the alcoves
function alcoveCheck() -- CUSTOMIZE: PICK A NAME FOR THIS FUNCTION, I PICKED "alcoveCheck"
  if alcoveContents(coolAlcove1, "coolScroll1") and -- CUSTOMIZE: USE THE NAME OF THE ABOVE FUNCTION IN PLACE OF "alcoveContents", USE THE ID OF YOUR FIRST ALCOVE IN PLACE OF "coolAlcove1", USE THE ID OR NAME OF THE ITEM YOU WANT THIS ALCOVE TO CHECK FOR IN PLACE OF "coolScroll1"
     alcoveContents(coolAlcove2, "coolScroll2") and -- CUSTOMIZE: USE THE NAME OF THE ABOVE FUNCTION IN PLACE OF "alcoveContents", USE THE ID OF YOUR SECOND ALCOVE IN PLACE OF "coolAlcove2", USE THE ID OR NAME OF THE ITEM YOU WANT THIS ALCOVE TO CHECK FOR IN PLACE OF "coolScroll2"
     alcoveContents(coolAlcove3, "coolScroll3") and -- CUSTOMIZE: USE THE NAME OF THE ABOVE FUNCTION IN PLACE OF "alcoveContents", USE THE ID OF YOUR THIRD ALCOVE IN PLACE OF "coolAlcove3", USE THE ID OR NAME OF THE ITEM YOU WANT THIS ALCOVE TO CHECK FOR IN PLACE OF "coolScroll3"
     alcoveContents(coolAlcove4, "coolScroll4") -- CUSTOMIZE: USE THE NAME OF THE ABOVE FUNCTION IN PLACE OF "alcoveContents", USE THE ID OF YOUR FOURTH ALCOVE IN PLACE OF "coolAlcove4", USE THE ID OR NAME OF THE ITEM YOU WANT THIS ALCOVE TO CHECK FOR IN PLACE OF "coolScroll4"
  then
    coolDoor:open() -- CUSTOMIZE: PUT YOUR DOOR ID HERE
  else
    coolDoor:close() -- CUSTOMIZE: PUT YOUR DOOR ID HERE
  end
end
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 finally got it. Thank you Filipsan and Komag. Sorry it took so long. I really appreciate your help.
Post Reply