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

[Question][Lua] Computer access program

Started by exploder2013, 12 September 2012 - 11:01 AM
exploder2013 #1
Posted 12 September 2012 - 01:01 PM
Hello everyone.

Could you please assist me in my problem.

I want to make computer security system, so you need to enter password to gain access, everything is working fine, except the password entering part and exit part.

When I press 1 to enter the password, computer displays the cursor in correct position, except there is menu text in the screen,so term.clear() doesn't even work. Only after you've entered correct or incorrect password it shows the text. So I'm trying to figure out, how to make text appear when you press 1, and how to make computer exit from program correctly, because exit() leads me to an error, but I would like that when program exits then it's plain black screen, nothing else.

Here is the code.

os.pullEvent = os.pullEventRaw --Disables CTRL + T.
term.clear()
term.setCursorPos(1,5)
x = 1
-- Menu screen.
while x > 0 do

print("		   +---------------------------------+")
print("		   :Exploder's Security System:")
print("		   +---------------------------------+")
print("		   :1.Access Computer.		   :")
print("		   :2.Exit.								 :")
print("		   +---------------------------------+")
term.setCursorPos(1,5)
event, param1, param2 = os.pullEvent()
if event == "char" and param1 == "1" then access() end
if event == "char" and param1 == "2" then quit() end

function access()
x = x-1
pass = "exploder"
input = read("*")
term.clear()
term.setCursorPos(1,1)
print("Enter Password")
term.setCursorPos(1,3)
write("Password:")
if input == pass then
term.clear()
term.setCursorPos(1,5)
print("Password is correct, unlocking computer.")
sleep(1)
term.setCursorPos(1,1)
exit()
else
term.setCursorPos(1,5)
print("Password is incorrect, computer is shuting down!")
sleep(2)
os.shutdown()
end
end
function quit()
term.clear()
term.setCursorPos(1,1)
exit()
end
end

Here are some pics:
Spoiler

Main Menu, if that helps.


This happens after I press 1.


Exit problem.


Text appearing after I've pressed enter.



Thanks for reading, and sorry for my bad English.
KaoS #2
Posted 12 September 2012 - 01:11 PM

os.pullEvent = os.pullEventRaw --Disables CTRL + T.
term.clear()
term.setCursorPos(1,5)
x = 1
-- Menu screen.
while x > 0 do
print("		    +---------------------------------+")
print("		    :Exploder's Security System:")
print("		    +---------------------------------+")
print("		    :1.Access Computer.			 :")
print("		    :2.Exit.															  :")
print("		    +---------------------------------+")
term.setCursorPos(1,5)
event, param1, param2 = os.pullEvent()
if event == "char" and param1 == "1" then access() end
if event == "char" and param1 == "2" then quit() end
function access()
x = x-1
pass = "exploder"
term.clear()
term.setCursorPos(1,1)
print("Enter Password")
term.setCursorPos(1,3)
write("Password: ")
input = read("*")
if input == pass then
term.clear()
term.setCursorPos(1,5)
print("Password is correct, unlocking computer.")
sleep(1)
term.setCursorPos(1,1)
exit()
else
term.setCursorPos(1,5)
print("Password is incorrect, computer is shuting down!")
sleep(2)
os.shutdown()
end
end
function quit()
term.clear()
term.setCursorPos(1,1)
error()
end
end

Give it a try :)/>/>
exploder2013 #3
Posted 12 September 2012 - 01:24 PM

os.pullEvent = os.pullEventRaw --Disables CTRL + T.
term.clear()
term.setCursorPos(1,5)
x = 1
-- Menu screen.
while x > 0 do
print("			+---------------------------------+")
print("			:Exploder's Security System:")
print("			+---------------------------------+")
print("			:1.Access Computer.			 :")
print("			:2.Exit.															  :")
print("			+---------------------------------+")
term.setCursorPos(1,5)
event, param1, param2 = os.pullEvent()
if event == "char" and param1 == "1" then access() end
if event == "char" and param1 == "2" then quit() end
function access()
x = x-1
pass = "exploder"
term.clear()
term.setCursorPos(1,1)
print("Enter Password")
term.setCursorPos(1,3)
write("Password: ")
input = read("*")
if input == pass then
term.clear()
term.setCursorPos(1,5)
print("Password is correct, unlocking computer.")
sleep(1)
term.setCursorPos(1,1)
exit()
else
term.setCursorPos(1,5)
print("Password is incorrect, computer is shuting down!")
sleep(2)
os.shutdown()
end
end
function quit()
term.clear()
term.setCursorPos(1,1)
error()
end
end

Give it a try :)/>/>


Thank you, now it works.
KaoS #4
Posted 12 September 2012 - 01:57 PM
No problem, you just have to print before you call the input function and use error() rather that exit()