How to check which item is in alcove?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Emciel
Posts: 34
Joined: Fri Sep 14, 2012 2:19 am

Re: How to check which item is in alcove?

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

Re: How to check which item is in alcove?

Post 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
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 »

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!)
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 »

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

Re: How to check which item is in alcove?

Post by Komag »

Ah, that looks like a stronger more versatile alternative, thanks!
Finished Dungeons - complete mods to play
User avatar
Montis
Posts: 340
Joined: Sun Apr 15, 2012 1:25 am
Location: Grimrock II 2nd playthrough (hard/oldschool)

Re: How to check which item is in alcove?

Post 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.
When destiny calls, the chosen have no choice.

My completed dungeon (LoG1): Hypercube
User avatar
Komag
Posts: 3654
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: How to check which item is in alcove?

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

Re: How to check which item is in alcove?

Post 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?)
Finished Dungeons - complete mods to play
SpacialKatana
Posts: 163
Joined: Fri Sep 14, 2012 6:20 pm

Re: How to check which item is in alcove?

Post 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.
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 »

Yeah. Break stops the for-loop and return will stop the function.
Steven Seagal of gaming industry
Post Reply