r/ComputerCraft • u/martmists • Oct 16 '14
Ik heb problemen met Computercraft
I want to create a program that gives you access to the computer only if you give the correct password. I have the following:
local pullEvent = os.pullEvent
loop = true
os.pullEvent = os.pullEventRaw
local password = "HALLO"
while loop do
term.clear()
textutils.slowPrint(os.version())
sleep(2)
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Password: ")
local input = read("*")
if input == password then
term.clear()
textutils.slowPrint("Password correct!")
sleep(2)
os.pullEvent = pullEvent
loop = false
else
textutils.slowPrint("Password incorrect!")
sleep(2)
os.shutdown
end <----- ???
end1
u/reilwin Oct 17 '14
There's several things your code will need to handle:
You need to set your computer so that your password program autoruns on startup. Otherwise anybody could use these keyboard shortcuts to reboot your computer and evade the password program.
You have the password program set to loop eternally until the correct password is entered. However, when the incorrect password is entered, you shut down the computer. See (1) above.
Along with (1), you'll also want to ensure that only your programs run on boot. That requires some means of limiting physical access to your computer, particularly preventing other players from placing disk drives adjacent to your computer or inputting their own floppy disks. Unless you have admin access and can insert programs directly into the ComputerCraft ROM directory.
1
u/boxesandcircles Oct 17 '14
adding to the rom can be done via a resource pack http://www.computercraft.info/forums2/index.php?/topic/14049-how-to-make-and-install-lua-resource-packs/
use return or shell.run to return to/launch a shell.
a third option not considered here.
1
u/martmists Nov 05 '14
what if i were to have people insert a password the first time it is used and then it saves it to the pc so it is different every pc?
2
u/Whiteherrin Oct 17 '14 edited Oct 17 '14
Wouldn't it just be simpler if you saved to program as "startup" that way it will be the primary / initial program booted every time you load that computer. Not only that but why shut down the computer when you enter an incorrect value? Do you want to be prompted to re-enter the PW?
while true do
os.clear()
local pw = "HALLO"
term.setCursorPos(1,1)
textutils.slowPrint("Password: ")
local input = read("*")
if input == pw then
os.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Password correct!")
os.sleep(2)
os.exit()
else
os.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Password incorrect!")
os.sleep(2)
end
end1
u/revereddesecration Oct 17 '14
I'm a little confused by your changing of the pullEvent functions, there really should not be any reason to do that.
What is the problem with this code? It seems good mostly.
1
u/boxesandcircles Oct 17 '14
the pullEvent function change is so that the program does not stop when a terminate event is fired.
1
u/revereddesecration Oct 17 '14
I looked at the wiki and found the section that explains why you did this. It also says:
Note that Ctrl+R (the reboot key combo) cannot be "disabled" in such a manner.
The issue here is that a disk drive with a disk that contains a startup program will run the disk's startup program before it runs your computer's startup program so don't forget to surround your computer in warded blocks or bedrock!
Aside from that, what is the problem that you want help with?
1
u/martmists Oct 17 '14 edited Oct 17 '14
the problem is when i type the correct password it says there is a syntax error in line 17. -> startup:17: attempt to call nil
1
1
u/reilwin Oct 17 '14
Instead of using exit(), try using a boolean variable to control your while loop. Then when a correct password is entered, set that boolean variable to false. Your program will exit the loop and end, returning the user to the console.
1
u/martmists Oct 17 '14
loops arent my best point. can you rewrite the program above with that loop?
2
u/reilwin Oct 17 '14
local pullEvent = os.pullEvent os.pullEvent = os.pullEventRaw local password = "HALLO" local keep_looping = true ------------- boolean variable added here while keep_looping do ----------------- changed to use boolean variable term.clear() textutils.slowPrint(os.version()) sleep(2) term.clear() term.setCursorPos(1,1) textutils.slowPrint("Password: ") local input = read("*") if input == password then term.clear() textutils.slowPrint("Password correct!") sleep(2) os.pullEvent = pullEvent keep_looping = false ----- changed to use boolean else textutils.slowPrint("Password incorrect!") sleep(2) os.shutdown end end1
u/martmists Oct 17 '14
thanks :D
2
u/LibreAnon Oct 21 '14
Something important to note is that exit() is not an existing function. You were trying to call the function "exit" which does not exist.
/u/reilwin has already given you the solution, but you need to know this for future. If you ever want to break a loop (that is infinite, don't rely on this if you can help it), use the keyword break. If you ever want to leave a function early or even a program, use the keyword return.
1
u/martmists Nov 05 '14
wait...
it says it expects an '=' at the first end how do i fix this?
2
u/reilwin Nov 05 '14
Could you give me the exact error message you received? You're using the code I gave you, unchanged, or has it been modified since then?
1
u/martmists Nov 05 '14
"bios:366: [string "startup"] :25: '=' expected" 2. ive added two lines: sleep(2) print("shutting down...") and i renamed the variable 'loop' the 'end's are now on line 25 and 26
→ More replies (0)
3
u/drinfernoo Oct 16 '14
Translated into English:
"I want to create a program that gives you access to the computer only if you give the correct password. I have the following:"
code follows