This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TEKRULEZ's profile picture

[Lua] [Question] Door Password + Button... I can not do what I want

Started by TEKRULEZ, 09 April 2013 - 07:20 PM
TEKRULEZ #1
Posted 09 April 2013 - 09:20 PM
Hi guys! First, i'm very new on ComputerCraft, and i have 0 skill in programming language, and this is my FIRST code!

I have a little trouble with my code, for now the code run very well, but i want to add a "feature", and can't figure out the problem… i have spent 4 hours trying figure out by myself, and the problem is not solved….

[Scenario]
I have my room, and the computer ask password to people to open the door, if the user insert wrong password for three times under her feet, a trapdoor opens, and the user fall down…

[Problem]
The code work fine, but i want add a button on back of my computer, to open the door when i'm in my room, and i do not want to type password again.

[Suspects]
Reading over the web, and figure out by myself, I've understand it is the [read] command, which blocks my code.

This is my code:



os.pullEvent = os.pullEventRaw
local password = "bacon"
local tempoPorta = 5
local tempoTrappola = 3
local tentativo = 0

function clear()
  term.clear()
  term.setCursorPos (1,1)
end

function bypass()
  rs.setBundledOutput("left", colors.lightBlue)
  sleep(tempoPorta)
  rs.setBundledOutput("left", colors.lightBlue+colors.white)
  accesso()
end

function login()
  if tentativo == 3 then
   maxTentativi()
  else
   clear()
   write("Inserire Password:")
   local input = read ("*")
  if input == password then
   clear()
   print("Passowrd Corretta!")
   rs.setBundledOutput("left", colors.lightBlue)
   sleep(tempoPorta)
   rs.setBundledOutput("left", colors.lightBlue+colors.white)
  else
   clear()
   tentativo = tentativo + 1
   print("Password Sbagliata!")
   print("Tentativo: ",tentativo)
   sleep(2)
  end
 end
end

function maxTentativi()
  rs.setBundledOutput("left", colors.white)
  sleep(tempoTrappola)
  tentativo = 0
  accesso()
end

function accesso()
rs.setBundledOutput("left", colors.lightBlue+colors.white)
 while true do
  login()
 end
end

accesso()

[Notes:]
The door is formed by sticky pistons and the closed state is "true", the color of cable is white
The trap is formed by sticky pistons and the deactivated state is "true", the color of cable is lightBlue

Well, that is all… Anyone can help me? Sorry for my very bad english i'm italian, but I hope I explained the problem well…

Thanks in advance for the help!
Espen #2
Posted 09 April 2013 - 10:52 PM
If you're using the built-in read() function, then in order to have the computer listen to redstone inputs AND user input (via the read() function) at the same time, you have to make use of the "parallel" API.
With it you can start two (or more) functions at the "same time" (it doesn't really work this way, but for all intends and purposes it does) and have them running in parallel (*wink* *wink*).

Another possibility would be to copy the code for the read() function and add redstone-listening functionality into it, but that is much more complicated and clumsy.
I'd suggest typing "help parallel" in CC or search for "parallel api" in the forums, it has been explained a few times as far as I remember.
Also look at the wiki page for it here: http://www.computerc.../Parallel_(API) (includes small examples)

There might be other ways, it's been a while since I last wrote a CC-Lua program.^^
Edited on 09 April 2013 - 08:54 PM
TEKRULEZ #3
Posted 09 April 2013 - 11:01 PM
Thanks for (the fast) reply! I will put to work immediately! I write here the progress… ;)/>
Spongy141 #4
Posted 10 April 2013 - 02:04 PM
If you want to have a button activate the door, you can do two things, put the button next to the door so it opens it with the button (easiest way) or use rs API to get the computer to detect a redstone signal on the back of your computer (were I'm guessing the button is) so just add this


function detectRedstone()
  if rs.getInput("back") then
    bypass() -- I called your function "bypass" so all you need to do is add this function and call it in your program
  end
end
TEKRULEZ #5
Posted 13 April 2013 - 05:51 AM
If you're using the built-in read() function, then in order to have the computer listen to redstone inputs AND user input (via the read() function) at the same time, you have to make use of the "parallel" API. With it you can start two (or more) functions at the "same time" (it doesn't really work this way, but for all intends and purposes it does) and have them running in parallel (*wink* *wink*). Another possibility would be to copy the code for the read() function and add redstone-listening functionality into it, but that is much more complicated and clumsy. I'd suggest typing "help parallel" in CC or search for "parallel api" in the forums, it has been explained a few times as far as I remember. Also look at the wiki page for it here: http://www.computerc.../Parallel_(API) (includes small examples) There might be other ways, it's been a while since I last wrote a CC-Lua program.^^

If you want to have a button activate the door, you can do two things, put the button next to the door so it opens it with the button (easiest way) or use rs API to get the computer to detect a redstone signal on the back of your computer (were I'm guessing the button is) so just add this
 function detectRedstone() if rs.getInput("back") then bypass() -- I called your function "bypass" so all you need to do is add this function and call it in your program end end 

Can't get code to work! If i modified the code with parallel API, the computer in-game remains locked and can't shutdown with CTRL+S….and of course the code does not work :(/>

the code modified is this:


os.pullEvent = os.pullEventRaw
local password = "bacon"
local tempoPorta = 5
local tempoTrappola = 3
local tentativo = 0
function clear()
  term.clear()
  term.setCursorPos (1,1)
end
function bypass()
  rs.setBundledOutput("left", colors.lightBlue)
  sleep(tempoPorta)
  rs.setBundledOutput("left", colors.lightBlue+colors.white)
  accesso()
end
function backButton()
if rs.getInput("back") then
  bypass()
end
end
function login()
  if tentativo == 3 then
   maxTentativi()
  else
   clear()
   write("Inserire Password:")
   local input = read ("*")
  if input == password then
   clear()
   print("Passowrd Corretta!")
   rs.setBundledOutput("left", colors.lightBlue)
   sleep(tempoPorta)
   rs.setBundledOutput("left", colors.lightBlue+colors.white)
  else
   clear()
   tentativo = tentativo + 1
   print("Password Sbagliata!")
   print("Tentativo: ",tentativo)
   sleep(2)
  end
end
end
function maxTentativi()
  rs.setBundledOutput("left", colors.white)
  sleep(tempoTrappola)
  tentativo = 0
  accesso()
end
function accesso()
rs.setBundledOutput("left", colors.lightBlue+colors.white)
while true do
  login()
end
end
while true do
parallel.waitForAny (accesso, backButton)
end

where is my fault?
oedze #6
Posted 13 April 2013 - 06:49 AM
i see what your going for,
i think you should use the os.pullEvent() feature so it will detect when you do something, after you did something it will return some parameters, if you test what parameter there is and say that if there is a redstone signal then open the door, else it will alk for a password

it sounds complicated and my english is not so well so i hope you understand :)/>