Page 2 of 8

Re: How to check which item is in alcove?

Posted: Tue Sep 25, 2012 10:41 pm
by Emciel
the problem is when checking containedItems, the function basically fails if there's nothing there.
this works:

Code: Select all

function gemCheckblue()
   if alcoveblue:getItemCount() ~= 0 then
   for i in alcoveblue:containedItems() do
      if i.name == "blue_gem" then
		doorblue:open()
	  end
	end
	else
		 doorblue:close()
	end
end

Re: How to check which item is in alcove?

Posted: Tue Sep 25, 2012 10:53 pm
by Komag
AH YES! It works. I in fact had tried to add some version of "getItemCount() ~= 0" or "~= 1", but I had put it within the "for i" instead of around it, thanks

Re: How to check which item is in alcove?

Posted: Wed Sep 26, 2012 1:47 pm
by Grimwold
Emciel wrote:the problem is when checking containedItems, the function basically fails if there's nothing there.
this works:

Code: Select all

function gemCheckblue()
   if alcoveblue:getItemCount() ~= 0 then
   for i in alcoveblue:containedItems() do
      if i.name == "blue_gem" then
		doorblue:open()
	  end
	end
	else
		 doorblue:close()
	end
end
I'm having problems with this script... I want to use an empty flask instead of a blue gem, so I changed the i.name field to "flask" and made sure my alcove and door were named correctly as per the script. and if I have something else on the alcove with the flask (e.g. a blue gem), I can take the flask off and the door stays open. I ticked "activate always" on the alcove and used an "any" connection to the script.

I couldn't reproduce with the gem because it seems impossible to take that off the alcove when other things are there (it gets hidden!)

Re: How to check which item is in alcove?

Posted: Wed Sep 26, 2012 2:17 pm
by Grimwold
OK, this script works for my flask... if the flask is on the alcove, the door is open and if it is off, the door is closed... even when there are multiple items on the alcove, and when there are no items at all. The "break" is used to stop checking items once the flask is found.. otherwise the door would sometimes close with multiple items on the alcove including the flask.

Code: Select all

    function gemCheckblue()
       if alcoveblue:getItemCount() == 0 
       then
           doorblue:close()
       else
         for i in alcoveblue:containedItems() do
           if i.name == "flask" 
           then
             doorblue:open()
             break
           else
             doorblue:close()
           end
         end
       end
    end

Re: How to check which item is in alcove?

Posted: Wed Sep 26, 2012 2:35 pm
by Komag
Ah, that looks like a stronger more versatile alternative, thanks!

Re: How to check which item is in alcove?

Posted: Wed Sep 26, 2012 2:46 pm
by Montis
Grimwold wrote:OK, this script works for my flask... if the flask is on the alcove, the door is open and if it is off, the door is closed... even when there are multiple items on the alcove, and when there are no items at all. The "break" is used to stop checking items once the flask is found.. otherwise the door would sometimes close with multiple items on the alcove including the flask.

Code: Select all

    function gemCheckblue()
       if alcoveblue:getItemCount() == 0 
       then
           doorblue:close()
       else
         for i in alcoveblue:containedItems() do
           if i.name == "flask" 
           then
             doorblue:open()
             break
           else
             doorblue:close()
           end
         end
       end
    end
I would replace the break with a return and place the doorblue:close() after the for-loop. Then the door wouldn't get x "closes" before it get's an "open", depending on how many items are in the alcove.

Re: How to check which item is in alcove?

Posted: Wed Sep 26, 2012 2:51 pm
by Komag
I partially follow, but can you write the script? I'm not quite sure how to "place the doorblue:close() after the for-loop" but still have everything work as intended

Re: How to check which item is in alcove?

Posted: Wed Sep 26, 2012 2:56 pm
by Komag
Hmm, you mean like this?:

Code: Select all

function gemCheckblue()
   if alcoveblue:getItemCount() == 0
   then
      doorblue:close()
   else
      for i in alcoveblue:containedItems() do
         if i.name == "flask"
         then
            doorblue:open()
            return
         end
      end
      doorblue:close()
   end
end
It seems to work well. So it looks for the flask. If it can't find it, then the for-loop closes and the door is closed. But if it finds it, it does the "return", which does what exactly? Just stops doing the script? How is it different from "break"? (does "break" only stop the for-loop?)

Re: How to check which item is in alcove?

Posted: Wed Sep 26, 2012 3:02 pm
by SpacialKatana
I'd assume so....(at least in C+) break drops you from loops, and return, well we know what that does. So, yeah.

Re: How to check which item is in alcove?

Posted: Wed Sep 26, 2012 3:30 pm
by antti
Yeah. Break stops the for-loop and return will stop the function.