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

[Lua] Problem with detecting redstone input

Started by Kattaroten, 16 October 2012 - 03:28 PM
Kattaroten #1
Posted 16 October 2012 - 05:28 PM
I've been trying to get a computer that opens a password-protected door (frame motors) for me and then after a set time closes again.
The problem is getting out again after it has closed. So I tried to make it that a button, placed on the inside, would activate the door, but this doesn't work and I have no idea why.
I tried several versions of a fix, among them, the os.pullEvent but that made the program unable to detect a password since it would stop until it detected a redstone input.

Any clue on how to make this work would be fantastic!


while true do
term.clear()
term.setCursorPos(1,1)
print("Loading OS")
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("Loaded")
sleep(1)
term.clear()
term.setCursorPos(1,1)
password = "100%"
print("Insert Password")
input = read()

if
  rs.getInput("right",true)
then
  rs.setOutput("back",false)
  sleep(1)
  rs.setOutput("back",true)
  sleep(1)
  rs.setOutput("back",false)
  sleep(10)
  rs.setOutput("back",true)
  sleep(1)
  rs.setOutput("back",false)
  sleep(8)
elseif
   input == password
  then
   term.clear()
   term.setCursorPos(1,1)
   print("Access Granted")  
   print("Confirmed")
   sleep(1)
   print()
   write("Status: ")
   textutils.slowWrite("You in, brotha'")
   rs.setOutput("back",false)
   sleep(1)
   rs.setOutput("back",true)
   sleep(1)
   rs.setOutput("back",false)
   sleep(10)
   rs.setOutput("back",true)
   sleep(1)
   rs.setOutput("back",false)
   sleep(8)
  else
   term.clear()
   term.setCursorPos(1,1)
   print("Access denied")
   rs.setOutput("back",false)
   sleep(1)
   print()
   write("Status: ")
   textutils.slowWrite("Dissed, punk")
   sleep(2)

end
end
Ditto8353 #2
Posted 16 October 2012 - 05:33 PM
You could try something like this:


while true do
   local evt = {os.pullEvent()}
   if evt[1] == "redstone" then
      openDoor()
   elseif evt[1] == "key" and evt[2] == ENTER   --'Replace ENTER with the code for the Enter Key'
      requestPassword()
   end
end
Kattaroten #3
Posted 16 October 2012 - 05:40 PM
The code for the Enter key? What do you mean?

I understand what the evt[1] does here, but what does evt[2] do?
Ditto8353 #4
Posted 16 October 2012 - 05:43 PM
os.pullEvent() returns 2 values if the event is a keypress, it returns the string "key" and a number representing the key pressed. I believe Enter = 28. If you just want to request a password on ANY keypress, then you don't need to check evt[2]. Just check if evt[1] == "key"
Doyle3694 #5
Posted 16 October 2012 - 05:44 PM
he just uses a table for simplicity sakes :P/>/>

edit: ninja'd :D/>/>
Kattaroten #6
Posted 16 October 2012 - 05:52 PM
I modified the code thusly


while true do
term.clear()
term.setCursorPos(1,1)
print("Loading OS")
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("Loaded")
sleep(1)
term.clear()
term.setCursorPos(1,1)
password = "100%"
print("Insert Password")
input = read()
local evt = {os.pullEvent()}
if
  evt[1] == "redstone"
then
  rs.setOutput("back",false)
  sleep(1)
  rs.setOutput("back",true)
  sleep(1)
  rs.setOutput("back",false)
  sleep(10)
  rs.setOutput("back",true)
  sleep(1)
  rs.setOutput("back",false)
  sleep(8)
elseif
   evt[1] == "key"
  then
   term.clear()
   term.setCursorPos(1,1)
   print("Access Granted")  
   print("Confirmed")
   sleep(1)
   print()
   write("Status: ")
   textutils.slowWrite("You in, brotha'")
   rs.setOutput("back",false)
   sleep(1)
   rs.setOutput("back",true)
   sleep(1)
   rs.setOutput("back",false)
   sleep(10)
   rs.setOutput("back",true)
   sleep(1)
   rs.setOutput("back",false)
   sleep(8)
  else
   term.clear()
   term.setCursorPos(1,1)
   print("Access denied")
   rs.setOutput("back",false)
   sleep(1)
   print()
   write("Status: ")
   textutils.slowWrite("Dissed, punk")
   sleep(2)

end
end

This returns me the message :19: unexpected symbol

I probably made some VERY stupid mistake in there somewhere..
Ditto8353 #7
Posted 16 October 2012 - 05:52 PM
he jsut uses a table for simplicity sakes :P/>/>

edit: ninja'd :D/>/>

It's ok :3 I don't usually explain things like that for 2 reasons:
1. If I don't explain it, people will just use it, which is good for their code.
2. It's on, like, page 9 of the manual.
Doyle3694 #8
Posted 16 October 2012 - 05:54 PM
well I'm assuming he know tables becuase he's into redstone inputs and all that good stuff :D/>/> Though I have seen some ppl go very narrow in their learning, and learn some really advanced stuff before the easy stuff
Cranium #9
Posted 16 October 2012 - 05:57 PM
you can just do

local event,p1 = os.pullEvent()
if event == "redstone" then
  --if redstone
elseif event == "key" then
 local input -- read()  --password
  --rest of password code
end
Kattaroten #10
Posted 16 October 2012 - 05:59 PM
Could you explain what you are doing here?

Just so I can use it in my future programs.
Cranium #11
Posted 16 October 2012 - 06:06 PM
os.pullEvent() returns several variables. Usually a maximum of thhree if I am not mistaken.
event, parameter1, parameter2 = os.pullEvent() will take an event, and add the first parameter into parameter1, and the second into parameter2. This is usually all you will need.
You are looking for two types of events. "key" events, and "redstone" events. When you receive a "key" event, you will then switch immediately to the read() function, which takes in all characters you type, and puts it into the "input" variable. Check if input == password, and you are good to go.
When you receive a "redstone" event, there are no additional parameters, however you can add an additional argument to your statement if you want to "filter" out some of the redstone events.

if event == "redstone" and rs.getInput(side) then
This time, we are not only checking if redstone is given, but if that redstone occurs on a specific side.
Kattaroten #12
Posted 16 October 2012 - 06:16 PM
This does not seem to work.

Everything works fine, except that it doesn't react to the redstone.

I shall, once again post the modified code


while true do
term.clear()
term.setCursorPos(1,1)
print("Loading OS")
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("Loaded")
sleep(1)
term.clear()
term.setCursorPos(1,1)
password = "100%"
print("Insert Password")
input = read()
local event,p1 = os.pullEvent()
if
  event == "redstone" and rs.getInput("right")
then
  rs.setOutput("back",false)
  sleep(1)
  rs.setOutput("back",true)
  sleep(1)
  rs.setOutput("back",false)
  sleep(10)
  rs.setOutput("back",true)
  sleep(1)
  rs.setOutput("back",false)
  sleep(8)
elseif
   event == "key" and input == password
  then
   term.clear()
   term.setCursorPos(1,1)
   print("Access Granted")  
   print("Confirmed")
   sleep(1)
   print()
   write("Status: ")
   textutils.slowWrite("You in, brotha'")
   rs.setOutput("back",false)
   sleep(1)
   rs.setOutput("back",true)
   sleep(1)
   rs.setOutput("back",false)
   sleep(10)
   rs.setOutput("back",true)
   sleep(1)
   rs.setOutput("back",false)
   sleep(8)
  else
   term.clear()
   term.setCursorPos(1,1)
   print("Access denied")
   rs.setOutput("back",false)
   sleep(1)
   print()
   write("Status: ")
   textutils.slowWrite("Dissed, punk")
   sleep(2)
 
end
end

..and again, it might just be a sloppy mistake
Ditto8353 #13
Posted 16 October 2012 - 06:21 PM
You need to move your code around a little.

while true do   --'Run your code forever.'
   local event,p1 = os.pullEvent()   --'Wait for an event.'
   if event == "key" then
	  --'Request for password.'
	  --'If password is correct, open door.'
   elseif event == "redstone" then
	  --'Open door.'
   end
end
Ditto8353 #14
Posted 16 October 2012 - 06:24 PM
On a sidenote:

I don't understand why the code
local event,p1 = os.pullEvent()
works for him, but the code using a single 'evt' table does not. Does anyone know?
Doyle3694 #15
Posted 16 October 2012 - 06:32 PM
because you are a bad programer, dah… no but seriously, I have no idea…

Maybe the table doesn't like to have var's inserted like that, what if you typed evt[1], evt[2] = os.pullEvent?
Kattaroten #16
Posted 16 October 2012 - 06:34 PM
My GOD this was harder to fix than I thought.

I moved around the code, like you said but it tells me that I have not ended my if (at line 18) block


while true do
term.clear()
term.setCursorPos(1,1)
print("Loading OS")
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("Loaded")
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("Insert Password")
input = read()
password = "100%"
local event,p1 = os.pullEvent()
if
   event == "key"
  then
   term.clear()
   term.setCursorPos(1,1)
   print("Access Granted")  
   print("Confirmed")
   sleep(1)
   print()
   write("Status: ")
   textutils.slowWrite("You in, brotha'")
   rs.setOutput("back",false)
   sleep(1)
   rs.setOutput("back",true)
   sleep(1)
   rs.setOutput("back",false)
   sleep(10)
   rs.setOutput("back",true)
   sleep(1)
   rs.setOutput("back",false)
   sleep(8)
  else
   term.clear()
   term.setCursorPos(1,1)
   print("Access denied")
   rs.setOutput("back",false)
   sleep(1)
   print()
   write("Status: ")
   textutils.slowWrite("Dissed, punk")
   sleep(2)
elseif
  event == "redstone" and rs.getInput("right")
then
  rs.setOutput("back",false)
  sleep(1)
  rs.setOutput("back",true)
  sleep(1)
  rs.setOutput("back",false)
  sleep(10)
  rs.setOutput("back",true)
  sleep(1)
  rs.setOutput("back",false)
  sleep(8)
end
end

I didn't put in anything to request password because I was unsure if I should use "and" for it
Doyle3694 #17
Posted 16 October 2012 - 06:43 PM
Just a tip in the terms of indentation, put some blank lines when there is a new "subject", kinda like paragraphing when you're writing a normal text :D/>/> It gets really hard to read otherwise… though I think this is personal preference I think 1 space indentations are abit painful too, but as I said, I think it's personal preference :P/>/>
Kattaroten #18
Posted 16 October 2012 - 06:44 PM
Yeah, I do that, it just shows up weird when I copy it.
Doyle3694 #19
Posted 16 October 2012 - 06:47 PM
oh yeah, don't like the forum code function, it kills indentation so badly :S

Didn't think of that, just ignore my last post :D/>/>
Ditto8353 #20
Posted 16 October 2012 - 06:48 PM
because you are a bad programer, dah… no but seriously, I have no idea…

Maybe the table doesn't like to have var's inserted like that, what if you typed evt[1], evt[2] = os.pullEvent?

I was thinking the same thing (the table part, not the bad programmer part >:L). It's a Lua syntax though, so if that is the case it must be specific to CC or the Lua version CC uses.
Kattaroten #21
Posted 16 October 2012 - 07:38 PM
Ok, this is getting a tiny bit frustrating.

It no longer gives me an error message but it still does not work.
When I press a key, it doesn't execute the "then" code


while true do
term.clear()
term.setCursorPos(1,1)
print("Loading OS")
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("Loaded")
sleep(1)
input = read()
password = "100%"
local event,p1 = os.pullEvent()
if
   event == "key"
  then
   term.clear()
   term.setCursorPos(1,1)
   print("Insert Password")
  if
   input == password
  then
   term.clear()
   term.setCursorPos(1,1)
   print("Access Granted")  
   print("Confirmed")
   sleep(1)
   print()
   write("Status: ")
   textutils.slowWrite("You in, brotha'")
   rs.setOutput("back",false)
   sleep(1)
   rs.setOutput("back",true)
   sleep(1)
   rs.setOutput("back",false)
   sleep(10)
   rs.setOutput("back",true)
   sleep(1)
   rs.setOutput("back",false)
   sleep(8)
  else
   term.clear()
   term.setCursorPos(1,1)
   print("Access denied")
   rs.setOutput("back",false)
   sleep(1)
   print()
   write("Status: ")
   textutils.slowWrite("Dissed, punk")
   sleep(2)
  end
elseif
  event == "redstone" and rs.getInput("right")
then
  rs.setOutput("back",false)
  sleep(1)
  rs.setOutput("back",true)
  sleep(1)
  rs.setOutput("back",false)
  sleep(10)
  rs.setOutput("back",true)
  sleep(1)
  rs.setOutput("back",false)
  sleep(8)
end
end
Lyqyd #22
Posted 16 October 2012 - 07:46 PM
because you are a bad programer, dah… no but seriously, I have no idea…

Maybe the table doesn't like to have var's inserted like that, what if you typed evt[1], evt[2] = os.pullEvent?

I was thinking the same thing (the table part, not the bad programmer part >:L). It's a Lua syntax though, so if that is the case it must be specific to CC or the Lua version CC uses.

That method of filling table key with values works just fine.
Cranium #23
Posted 16 October 2012 - 08:05 PM
On a sidenote:

I don't understand why the code
local event,p1 = os.pullEvent()
works for him, but the code using a single 'evt' table does not. Does anyone know?

actually, os.pullEvent will work with tables if you want. You can do evt[1], evt[2] = os.pullEvent(). That will automatically put the two values returned into a table. I see no reason why you would want to do that, but it's possible. The only thing with lua, is that when an item returns multiple values not inserted into a table, you have to separate them into different variables with a comma.
Ditto8353 #24
Posted 16 October 2012 - 08:26 PM
It's just easier for me to understand my code when I use
local evt = {os.pullEvent()}
so I can have all event items wrapped in a single table, instead of having evt, p1, p2, p3
PixelToast #25
Posted 16 October 2012 - 08:37 PM
It's just easier for me to understand my code when I use
local evt = {os.pullEvent()}
so I can have all event items wrapped in a single table, instead of having evt, p1, p2, p3
same here, it gets annoying when you have to make all those variables