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

Build a Better Startup program

Started by Resistance128, 12 May 2012 - 09:29 PM
Resistance128 #1
Posted 12 May 2012 - 11:29 PM
So The first program I tried to make was a login program that would require a password before any content on the computer could be accessed. The code is as follows;

textutils.slowPrint("Welcome User")
write("enter password: ")

answer = read()
if answer == "password"
then
shell.run("clear")
sleep(.1)
texutils.slowPrint("CraftOS 1.3")
sleep(1.5)
textutils.slowPrint("Welcome ResistAnce")
else
textutils.slowPrint("Wrong Password")
sleep(2)
textutils.slowPrint("shutting down computer")
sleep(1.3)
os.shutdown()
end

But there is an obvious problem with this is the fact that you can hold ctrl+T to terminate the program and then access all of the content on that computer. What I'm trying to do is prevent the user from being able to terminate the program, but I honestly have no clue how to go about doing that… Any suggestions?
MysticT #2
Posted 12 May 2012 - 11:39 PM
Check the tutorials forum, there's already a post on how to do this. And there's a lot of post with the same question, so check out some of them.
EmTeaKay #3
Posted 13 May 2012 - 03:19 AM
Here ya go! Hope you enjoy.

local oldPull = os.pullEvent;
os.pullEvent = os.pullEventRaw;
--Code here
os.pullEvent = oldPull;
Mendax #4
Posted 13 May 2012 - 11:13 AM
Here ya go! Hope you enjoy.

local oldPull = os.pullEvent;
os.pullEvent = os.pullEventRaw;
--Code here
os.pullEvent = oldPull;
Why overcomplicate it?

-- Here is your password variable. Change it to whatever you like.
password = "whatever"

textutils.slowPrint("Welcome User")
write("enter password: ")

status, answer = pcall, (read,'*')
if answer == "password" then
shell.run("clear")
sleep(.1)
texutils.slowPrint("CraftOS 1.3")
sleep(1.5)
textutils.slowPrint("Welcome ResistAnce")
else
textutils.slowPrint("Wrong Password")
textutils.slowPrint("shutting down computer")
sleep(0.1)
os.shutdown()
end
The differences are:
  1. Instead of os.pullEventRaw() (Or whatever), I used pcall.
  2. I made it print out as * instead of letters.
  3. I changed the sleep at the end. It might still be terminable
Hope I helped (And didn't confuse you (Like him ^))
Lyqyd #5
Posted 13 May 2012 - 03:21 PM
Your version is the one overcomplicating things. His will affect read(), sleep() and os.pullEvent() calls, so it is simpler and more effective.
Resistance128 #6
Posted 13 May 2012 - 06:53 PM
Ok, that makes some sense, so basically the pcall is what replaces read(). And also, would using a line of code like;textutils.slowPrint("………………..") instead of sleep(#) help prevent the user from terminating the command? It could serve the same purpose of delaying code but couldn't textutils.slowPrint also cause the user to be able to terminate the program?

If that even made any sense…
Cloudy #7
Posted 13 May 2012 - 06:56 PM
slowPrint uses sleep() so there would be no advantage. I hate to say it, but the simplest is the os.pullEventRaw one, provided you put it back after you've finished running your code.
Mendax #8
Posted 13 May 2012 - 06:59 PM
Your version is the one overcomplicating things. His will affect read(), sleep() and os.pullEvent() calls, so it is simpler and more effective.
His is, however, terminateable.
Resistance128 #9
Posted 13 May 2012 - 08:01 PM
Well, I can do some testing once I actually get on minecraft, (Can't at the moment) But I will try all of these different ways of going about writing the code and see which ones can be terminated.
Lyqyd #10
Posted 14 May 2012 - 12:36 AM
Your version is the one overcomplicating things. His will affect read(), sleep() and os.pullEvent() calls, so it is simpler and more effective.
His is, however, terminateable.

Nope. Try it sometime. Just use the code he put up there, put a call to read() in there. While it's waiting for input, try to terminate it. Ctrl-R and -S will always work, of course, but that will bypass the Ctrl-T detection.