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

read redstone input issue

Started by krethyan, 09 December 2017 - 07:30 AM
krethyan #1
Posted 09 December 2017 - 08:30 AM
I'm making a basic security door which functions perfectly however,
once I get into my base I cannot get out so my solution was to have
the computer read a redstone input (button) and open the door once more

-all blank sections are not mistakes just things I've removed as they may be offensive.
-the door is hidden by pistons so the code both deactivates the piston and opens the door
-the security door section functions properly, the rs.getInput does not work but does not cause errors
-if I get rid of the first "end" the program treats the rs.Input as part of the "else"
-never used lua or computercraft before

any solutions would be appreciated

Spoiler
rs.setOutput("back",true)
print("L O A D I N G  O S")
sleep(1)
print("")
print("Welcome")
sleep(1)
print("Input Correct Password")
password = read()

if password == "" then
	term.clear()
	print("Password Accepted")
	redstone.setOutput("back",false)
	print("Welcome")
	sleep(5)
	redstone.setOutput("back",true)
	os.reboot()
else
	print("Incorrect Password")
	print("")
	sleep(2)
	os.shutdown()
end

rs.getinput("right")

if true then
	rs.setOutput("back",false)
	sleep(5)
	rs.setOutput("back",true)
end
Edited by
Bomb Bloke #2
Posted 10 December 2017 - 12:27 AM
rs.getInput() works like read(), in that it returns a value for you to use. You're not attempting to store that value or otherwise check it - the "if true then" line you've got there is literally checking if "true" is "true", and since that'll always be the case, the code inside that block will always execute.

Or it would, if the code above didn't either restart the system or shut it down. Whether the password is correct or not, the script will be interrupted before it reaches those lower lines.

To make this work the way you're wanting, you'd first want to make the script repeat sections of your code, without restarting. This is most easily done using while loops:

Spoiler
rs.setOutput("back",true)

while true do  --# A loop that'll repeat indefinitely.
	term.clear()
	term.setCursorPos(1, 1)
	
	print("L O A D I N G  O S")
	sleep(1)
	print("")
	print("Welcome")
	sleep(1)
	print("Input Correct Password")
	
	password = read()

	if password == "" then
		print("Password Accepted")
		redstone.setOutput("back",false)
		print("Welcome")
		sleep(5)
		redstone.setOutput("back",true)
	else
		print("Incorrect Password")
	end
	
	sleep(5)
end

while true do
	if rs.getinput("right") then  --# Checks the redstone state instead of just "true".
		rs.setOutput("back",false)
		sleep(5)
		rs.setOutput("back",true)
	end

	os.pullEvent("redstone")  --# Waits here for a redstone state change.
end

Of course, that still leaves the issue where only the first block of code will be executed - since it repeats forever, the lower section is never reached, and never runs. But the parallel API offers an easy way to run two bits of code "side-by-side":

Spoiler
rs.setOutput("back",true)

local function checkPassword()  --# Defines this block of code as a function, but doesn't run it yet.
	while true do
		term.clear()
		term.setCursorPos(1, 1)

		print("L O A D I N G  O S")
		sleep(1)
		print("")
		print("Welcome")
		sleep(1)
		print("Input Correct Password")

		password = read()

		if password == "" then
			print("Password Accepted")
			redstone.setOutput("back",false)
			print("Welcome")
			sleep(5)
			redstone.setOutput("back",true)
		else
			print("Incorrect Password")
		end

		sleep(5)
	end
end

local function checkButton()
	while true do
		if rs.getinput("right") then
			rs.setOutput("back",false)
			sleep(5)
			rs.setOutput("back",true)
		end

		os.pullEvent("redstone")
	end
end

parallel.waitForAny(checkPassword, checkButton)  --# Runs the two functions at the same time.
Edited on 09 December 2017 - 11:28 PM
krethyan #3
Posted 10 December 2017 - 02:19 AM
Works perfectly thank you.
Just a quick question about printing
how would you go about either;
printing a message, clearing it, then writing it on the same line
or
printing a message then having a blank line such as
>Hello
>
>World
KingofGamesYami #4
Posted 10 December 2017 - 04:09 AM
To print a message, clear it, and then write to the same line is fairly simple.

term.setCursorPos( 1, 1 )
print( "Hello, World!" )
term.setCursorPos( 1, 1 )
sleep( 1 ) --# delay added for emphasis of what individual commands do.
term.clearLine()
sleep( 1 )
print( "This line has changed" )
You may want to use term.write instead of print, as print will automatically add a new line (something you may or may not want).

To print and have a blank line,

print( "Hello\n\nWorld" )
'\n' is the new line character.
Edited on 10 December 2017 - 03:09 AM
krethyan #5
Posted 10 December 2017 - 04:59 AM
\n\n does work thank you but term.clearLine() does not and the write function does indeed write on the same line,
it doesn't write in the same spot, it jumps slightly to the right
KingofGamesYami #6
Posted 10 December 2017 - 05:32 AM
Hmm… I'm not sure why clearLine would not work.

term.write does move the cursor, as it is supposed to. It just doesn't add a new line.

Are you using the exact code I posted to test these things or could something in your usage of the functions be to blame? I haven't actually run the code in MC so it could be flawed.
Edited on 10 December 2017 - 04:32 AM
krethyan #7
Posted 11 December 2017 - 11:12 PM
tested the exact code first then and kind of variation that could work
my mc version is 1.7.10 if that helps
KingofGamesYami #8
Posted 11 December 2017 - 11:35 PM
Just tried it out in MC*, got the expected result. Make sure the cursor is positioned on the line you want to clear.

*1.12, but term is an extremely stable API.