More pit woes

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
mwoodman
Posts: 3
Joined: Mon Jan 13, 2014 7:42 pm

More pit woes

Post by mwoodman »

Hi everyone - you'll be pleased to know I'm a total newbie to modding and Lua, so expect a few daft questions....although I have done my best to scour the forums for the answers beforehand. Have to say, I've really enjoyed reading the forum posts and have found this resource really useful. I'm still lifting chunks of code, but the undrlying logic is starting to become clear...

Anyway, I have a puzzle problem I can't work out:

1) I'm setting up a puzzle whereby stepping on a series of pressure plates closes pits in an area behind a grating, (although stepping on the wrong ones opens them all again).
2) When the final 'correct' pressure plate is used, a gate opens allowing access to that area; however the pits do not all have to be closed in order for the player to get to the centre - they're really just a visual cue to show them from behind the grating how far they are through the puzzle.

The problem I have is that I was trying to set up connectors from pits to a script entity; I started by imagining I could steal the 'combination puzzle' script and have a script entity looking at the state of 8 pits, only activting the door when they were all closed. But then I realised that pits don't have connectors in the inspector, and I came unstuck when trying to code this.

I also thought about using a counter linked to the plates to decrement the counter - but then the player could just step on the plate 8 times. I see the inspector features an option to 'activate once', but ideally I want the pressure plates to reset as well, (and start decrementing again) if the player steps on the WRONG pressure plate, (with the result of opening all pits and sending them back to square 1).

I'm sure there's a very simple explaination but I can't see the wood for the trees at present!
User avatar
Leki
Posts: 550
Joined: Wed Sep 12, 2012 3:49 pm

Re: More pit woes

Post by Leki »

You can do it in many ways. One of them is to connect each plate to script entity with following script.
I wrote it without testing, so there may be a typo etc, but the idea is that on each step on the plate it will check all pits defined in list in script and if all of them are closed, it will open door. Script does not care about pit reset etc.

Code: Select all

function OpenPitDoor
   pitList = {"pit_1, "pit_2"} -- list of pits in puzzle, write their names
   local c = 0 -- variable counting closed pits
   for i = 0, #pitList do
      if pitList[i]:isClosed() then -- it returns true if yes, check lua how to do if it's true
          c = c + 1 --if its closed then increase counter
      else
          break
      end
   end

   if c == #pitList then -- if all are closed, c must be equal to number of pits in list of pits
      pitDoor:open()
   end

end
I'm not sure about that if pitList:isClosed() then
maybe you have to go through all pits in level and if pit.name is in list you can c=c+1 etc...

[/color]
I'm the Gate I'm the Key.
Dawn of Lore
User avatar
mwoodman
Posts: 3
Joined: Mon Jan 13, 2014 7:42 pm

Re: More pit woes

Post by mwoodman »

Thanks! I'll give that a go!
User avatar
Diarmuid
Posts: 807
Joined: Thu Nov 22, 2012 6:59 am
Location: Montreal, Canada
Contact:

Re: More pit woes

Post by Diarmuid »

To slightly fix that code:

Code: Select all

pitList = {"pit_1, "pit_2"} -- list of pits in puzzle, write their names

function OpenPitDoor()
   local c = 0 -- variable counting closed pits
   for i = 1, #pitList do
      if findEntity(pitList[i]):isClosed() then -- it returns true if yes, check lua how to do if it's true
          c = c + 1 --if its closed then increase counter
      else
          break
      end
   end

   if c == #pitList then -- if all are closed, c must be equal to number of pits in list of pits
      pitDoor:open()
   end

end
- global variables like the pitList table should be declared outside of functions, always only local variables inside functions!
- missing () after the function name.
- Lua tables start from 1, so it's for i = 1, not for i = 0.
- The table stores the pit's id, but you need to retrieve the actual entity befor checking if it's closed, so findEntity(pitList) instead of pitList.

Enjoy!
User avatar
Leki
Posts: 550
Joined: Wed Sep 12, 2012 3:49 pm

Re: More pit woes

Post by Leki »

Thanks for fixing code m8, I wrote it from my tablet and it was terrible exeriance haha - on the other hand, it was some kind of hint, to motivate him to investigate etc :twisted:
I'm the Gate I'm the Key.
Dawn of Lore
User avatar
mwoodman
Posts: 3
Joined: Mon Jan 13, 2014 7:42 pm

Re: More pit woes

Post by mwoodman »

Great guys - I really appreciate the help! :D
Post Reply