[Script] Button Puzzle

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
Inferno986return
Posts: 2
Joined: Mon Nov 05, 2012 11:32 am

[Script] Button Puzzle

Post by Inferno986return »

Hey everyone, I made this script in Lua but it keeps returning errors. Aside from that it should be fine:

Code: Select all


function ButtonPuzzle() --Controls the entire button puzzle
 
  local Hundreds, Tens, Units = 0,0,0 --Creates the Hundreds, Tens and Units variables.

--Variable assignment

  Hundreds_Counter:getValue() = Hundreds --Assigns the value from the Hundreds_Counter to the variable Hundreds.

  Tens_Counter:getValue() = Tens --Assigns the value from the Tens_Counter to the variable Tens.

  Units_Counter:getValue() = Units --Assigns the value from the Units_Counter to the variable Units.

--Main Method

  if Hundreds == 1 and Tens == 1 and Units == 3 then --Checks that all the buttons were pressed the correct amount of times.
  
  hudPrint ("Correct Answer!)
  ButtonPuzzle_Door:Open --Opens the door at the end of the room so the player can progress the level.
  
  else
  
  hudPrint ("Incorrect Answer!)
  hudprint("Hundreds ="Hundreds" Tens="Tens" and Units ="Units) --Helps the player by telling them what they inputted.
  hudPrint ("Resetting Puzzle...")

  Hundreds_Counter:reset() --Resets the Hundreds_Counter back to 0.
  Tens_Counter:reset() --Resets the Tens_Counter back to 0.
  Units_Counter:reset() --Resets the Units_Counter back to 0.

end --End ButtonPuzzle

So far I get gameplay errors when the door should open and I can't seem to output the numbers on the counters to the player when they get the answer wrong.

What I have done badly here?
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: [Script] Button Puzzle

Post by Grimwold »

The first thing I notice is this line:

Code: Select all

hudprint("Hundreds ="Hundreds" Tens="Tens" and Units ="Units) --Helps the player by telling them what they inputted.
where it should be hudPrint, and you need to use the .. string join operator, like so:

Code: Select all

hudPrint("Hundreds =" .. Hundreds .. " Tens=" .. Tens .. " and Units =" .. Units)
3socks
Posts: 11
Joined: Sun Nov 04, 2012 11:29 pm

Re: [Script] Button Puzzle

Post by 3socks »

You're assigning your local variables to the results of the functions, which has no effect.
I think you misunderstood how assignment works. A variable on the left side of = gets the value of a variable on the right side. Not otherwise. So your code should look like this:

Code: Select all

Hundreds = Hundreds_Counter:getValue() --Assigns the value from the Hundreds_Counter to the variable Hundreds.

Tens = Tens_Counter:getValue() --Assigns the value from the Tens_Counter to the variable Tens.

Units = Units_Counter:getValue() --Assigns the value from the Units_Counter to the variable
And you're missing end quotes in hubPrint

Code: Select all

hudPrint ("Correct Answer!")
hudPrint ("Incorrect Answer!")


And lastly I think you should use string concatenation when using hudPrint. To concatenate string with other string or variables use two dots.

Code: Select all

hudprint("Hundreds ="..Hundreds.." Tens="..Tens.." and Units ="..Units)
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: [Script] Button Puzzle

Post by Grimwold »

Also... your variable assignments seem to be wrong... Should be

Code: Select all

Hundreds = Hundreds_Counter:getValue()
Tens = Tens_Counter:getValue()
Units = Units_Counter:getValue()

I would also recommend using lower case for everything (except possibly function names themselves) and draw your attention to this mantra from the One-Room thread:
Skuggasveinn wrote:there is only one case - lower case
there is only one case - lower case
there is only one case - lower case

If everyone chants this while doing there dungeon we should be fine :D
EDIT - Ninja'd because I took too long finding the lower case mantra!
Inferno986return
Posts: 2
Joined: Mon Nov 05, 2012 11:32 am

Re: [Script] Button Puzzle

Post by Inferno986return »

EDIT: I still can't seem to get an output on the hundreds, tens and units when the user gets the answer wrong. This is the final script however. It's so close, it hurts. ;)

Thanks for all your replies, I did some rectifying and this is the result:

Code: Select all


function ButtonPuzzle() --Controls the entire button puzzle
 
  local hundreds, tens, units = 0,0,0 --Creates the Hundreds, Tens and Units variables.

--Variable assignment for hundreds, tens, units

  hundreds = Hundreds_Counter:getValue() 
  tens = Tens_Counter:getValue()
  units = Units_Counter:getValue()

--Main Method

  if hundreds == 1 and tens == 1 and units == 3 then --Checks that all the buttons were pressed the correct amount of times.
  
    hudPrint ("Correct Answer!")
    ButtonPuzzle_Door:open() --Opens the door at the end of the room so the player can progress the level.
  
  else
  
    hudPrint("Incorrect Answer!")
    hudPrint("Hundreds = " .. hundreds) --Helps the player by telling them what they inputted.
    hudPrint("Tens = " .. tens)
    hudPrint("Units = " .. units) 
    hudPrint("Resetting Puzzle...")

    Hundreds_Counter:reset() --Resets the Hundreds_Counter back to 0.
    Tens_Counter:reset() --Resets the Tens_Counter back to 0.
    Units_Counter:reset() --Resets the Units_Counter back to 0.
  
  end --End If
end --End ButtonPuzzle


I'll compile it at home and see how it runs.
Post Reply