4 posts
Posted 24 October 2012 - 06:03 PM
Hello all. I've made this code myself, but I can't find a way to make the program "reset" to the point
function main()
function main()
print("**Restricted Access**")
print("Insert Password")
x = io.read()
term.clear()
term.setCursorPos(1,1)
print("Processing...")
sleep(1)
print("Processing...")
sleep(1)
print("Processing...")
sleep(1)
if x == "pascal" then
print("Access Granted")
sleep(0.5)
print("Activating door...")
sleep(1)
redstone.setOutput("back", true)
sleep(6)
print ("Door will now close...")
sleep(1)
redstone.setOutput("back", false)
sleep(2)
term.clear()
term.setCursorPos(1,1)
else print("Access Denied")
sleep(2)
term.clear()
term.setcursor(1,1)
main()
end
The point is, if the person uses the wrong code it will return to the beginning, so that he may attempt again to insert the correct password. Are there any errors on the function?
Another question, is there anyhow to make a console to keep running only one program and not be able to edit it or open other programs? Maybe making it unbreakable too.
Thanks.
52 posts
Location
Scotland
Posted 24 October 2012 - 06:59 PM
You may have to call main() after the whole function itself
818 posts
Posted 24 October 2012 - 07:00 PM
Use while true loops rather than calling a function inside a function. Otherwise you'll get stack overflow
52 posts
Location
Scotland
Posted 24 October 2012 - 07:00 PM
In answer to your other question: Yes, there is a way. You have to put this line at the start of your program - "os.pullEvent = os.pullEventRaw" this will stop the user from terminating the program. then all you need to do is make some sort of loop in your program.
2088 posts
Location
South Africa
Posted 24 October 2012 - 07:14 PM
while true do
term.clear()
term.setcursor(1,1)
print("**Restricted Access**")
print("Insert Password")
x = io.read()
term.clear()
term.setCursorPos(1,1)
print("Processing...")
sleep(1)
print("Processing...")
sleep(1)
print("Processing...")
sleep(1)
if x == "pascal" then
print("Access Granted")
sleep(0.5)
print("Activating door...")
sleep(1)
redstone.setOutput("back", true)
sleep(6)
print ("Door will now close...")
sleep(1)
redstone.setOutput("back", false)
sleep(2)
else print("Access Denied")
sleep(2)
end
231 posts
Posted 24 October 2012 - 08:24 PM
Lua has proper tail calls, so you can also just use return main() on the second-last line, instead of just main(), and it will never stack overflow.
4 posts
Posted 24 October 2012 - 10:26 PM
while true do
term.clear()
term.setcursor(1,1)
print("**Restricted Access**")
print("Insert Password")
x = io.read()
term.clear()
term.setCursorPos(1,1)
print("Processing...")
sleep(1)
print("Processing...")
sleep(1)
print("Processing...")
sleep(1)
if x == "pascal" then
print("Access Granted")
sleep(0.5)
print("Activating door...")
sleep(1)
redstone.setOutput("back", true)
sleep(6)
print ("Door will now close...")
sleep(1)
redstone.setOutput("back", false)
sleep(2)
else print("Access Denied")
sleep(2)
end
This code worked pretty well, but it took me almost an hour to find that the "term.setcursor()" was retuning me a NIL call… Everything works fine now, thanks guise
4 posts
Posted 24 October 2012 - 10:28 PM
In answer to your other question: Yes, there is a way. You have to put this line at the start of your program - "os.pullEvent = os.pullEventRaw" this will stop the user from terminating the program. then all you need to do is make some sort of loop in your program.
This worked, but you can still just reboot or shutdown the system. Is there anyhow to prevent that as well?
231 posts
Posted 24 October 2012 - 10:38 PM
This worked, but you can still just reboot or shutdown the system. Is there anyhow to prevent that as well?
You can't prevent people from restarting it, but you can make startup run it, so it will automatically restart if someone reboots it.
4 posts
Posted 24 October 2012 - 11:25 PM
This worked, but you can still just reboot or shutdown the system. Is there anyhow to prevent that as well?
You can't prevent people from restarting it, but you can make startup run it, so it will automatically restart if someone reboots it.
How so?
2005 posts
Posted 25 October 2012 - 12:57 AM
You put shell.run("nameofprogram") in your startup file.
I also recommend a repeat until loop rather than a while true do … if then break end … end loop for this kind of situation. You'll always want to prompt the user for a password and get the input at least once, and you want to exit the loop only if the password entered is correct, which you can only check after receiving the user input at least once, so it's more efficient and readable to use repeat until.
248 posts
Posted 25 October 2012 - 01:20 AM
You put shell.run("nameofprogram") in your startup file.
Easier way of making this: rename your password file to startup, and it'll run when you reboot the computer
2005 posts
Posted 25 October 2012 - 02:15 AM
Yes, that too. Unfortunately, I have a lot of programs that write to the startup file so they can reboot themselves after a chunk un/load event, so this wouldn't work for me. But for a password protected terminal it isn't such a bad idea, presumably you don't want to let any other programs alter the startup file on that computer.
1688 posts
Location
'MURICA
Posted 25 October 2012 - 02:42 AM
I thought it'd be helpful to know that there's actually goto statement in lua 5.2.
::top::
print 'Would you like to exit this program?'
if read() ~= 'yes' then
print 'Okay, I'll ask the question again.'
goto top
end
print 'Goodbye.'
I don't think CC is using 5.2 though.
231 posts
Posted 25 October 2012 - 02:44 AM
I thought it'd be helpful to know that there's actually goto statement in lua 5.2.
::top::
print 'Would you like to exit this program?'
if read() ~= 'yes' then
print 'Okay, I'll ask the question again.'
goto top
end
print 'Goodbye.'
Except that ComputerCraft doesn't use 5.2
1688 posts
Location
'MURICA
Posted 25 October 2012 - 02:44 AM
I was just stating it for future reference.
2005 posts
Posted 25 October 2012 - 03:03 AM
I have mixed feelings about goto. But most of them aren't warm and fuzzy.
1688 posts
Location
'MURICA
Posted 25 October 2012 - 03:12 AM
I have mixed feelings about goto. But most of them aren't warm and fuzzy.
I felt the same way, but that was before I realized hOW USEFUL IT COULD'VE BEEN IN 90% OF MY PROGRAMS
2005 posts
Posted 25 October 2012 - 03:47 AM
It's a useful function, no doubt about that. I love reusing code, and nothing does that like having a goto type command.
But it's hell on debugging, particularly when dealing with someone else's code. And once you get past the simplest programs, debugging is 99 and nine 9's of the actual work.
8543 posts
Posted 25 October 2012 - 04:08 AM
I can't think of any genuinely good use cases for goto. All that would happen if they decided to use a LuaJ with Lua 5.2 is that we'd get ridiculous amounts of insanely spaghetti'd code.
2005 posts
Posted 25 October 2012 - 05:54 AM
Honestly can't think of anything more likely to kill the Ask a Pro section than goto support in the implementation. But I would be using that all the time, and suffering the consequences of course.
2088 posts
Location
South Africa
Posted 25 October 2012 - 07:06 AM
I thought it'd be helpful to know that there's actually goto statement in lua 5.2.
::top::
print 'Would you like to exit this program?'
if read() ~= 'yes' then
print 'Okay, I'll ask the question again.'
goto top
end
print 'Goodbye.'
I don't think CC is using 5.2 though.
This would be so helpful lol. Will CC ever use 5.2?
1688 posts
Location
'MURICA
Posted 25 October 2012 - 11:45 AM
Someone said a while ago CC was going to use the C/C++ version of Lua, so it's possible.
1548 posts
Location
That dark shadow under your bed...
Posted 25 October 2012 - 11:56 AM
Ah this brings back memories ^_^/>/> in DOS there are no functions or anything, only files. I used to use the goto function for loops, functions and many other things. the goto function is useful but there is really not much you can do with it that we can't already do in lua
1688 posts
Location
'MURICA
Posted 25 October 2012 - 12:56 PM
That's basically the same as saying we only really need one type of loop, since a while ago I made a post that shows that all loop types (while, for and repeat) can all serve the basic purpose of one another. Plus, I never really saw the appeal of the repeat loop anyway - it's been less helpful for me than using while.
In any case, why not give multiple options to satisfy different user preferences? One of lua's most important qualities is flexibility, is it not?
EDIT: I remembered that the repeat loop can actually access local variables within itself, so it does have a use.
1548 posts
Location
That dark shadow under your bed...
Posted 25 October 2012 - 02:06 PM
very much so, flexibility is always good ^_^/>/>
2005 posts
Posted 25 October 2012 - 06:06 PM
goto is the basis of loops and functions. These simply codify the essentially different structures that can be produced using goto. I emphasize essentially because there really are irreducible differences between the different kinds of loops (and between loops and functions).
You can try all you want to code a while do end loop to behave as a repeat until or for do end. You can get the same output, but the processing methods are simply different. If you tried writing them using only goto, you'd quickly realize that a while do end loop with breaks or is simply structurally different from a repeat until loop.
Or, you'd end up with code that neither you nor anyone else could decipher.
1688 posts
Location
'MURICA
Posted 25 October 2012 - 09:33 PM
You stress the point that the code would be unreadable, but I can't really see a code using goto being any harder to read than the scripts being posted now.
EDIT: If you mean as a replacement for a loop, then I can understand that perfectly, depending on the situation, but my point stands, as it always depends on the situation, haha.
2005 posts
Posted 26 October 2012 - 02:37 AM
Well, you can try debugging the code of people who resorted to goto because they think that they have invented a new kind of loop or function. But you're on your own, I don't got your back in that situation.