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

[LUA] How to return to previous line of code?

Started by dasangrypanda, 10 May 2012 - 07:00 PM
dasangrypanda #1
Posted 10 May 2012 - 09:00 PM
Id like to know how i can return to a previous segment of the code. For example this is a simple door lock i made. (I'm LUAtarded)

pass = "diggydiggyhole"

os.pullEvent = os.pullEventRaw
term.clear()
print ("At this time please enter your level 5 security code.")
print (" ")
write "Security Code: "
input = read("*")
if input == pass then
term.clear()
term.setCursorPos(1,1)
print ("Access Granted")
redstone.setOutput("left", true )
sleep(5)
os.reboot()
else
term.setCursorPos(1,1)
term.clear()
print ("Security Code Invalid")
print (" ")
print ("Terminal Will Now Reboot")
sleep(2)
os.reboot()
end

in this part here, how can i make it return to the first line, or any other line, without re-typing the code? Instead of rebooting, id just like it to return to the top line, clear the screen, and go from there like the code says. is this possible?

else
term.setCursorPos(1,1)
term.clear()
print ("Security Code Invalid")
print (" ")
print ("Terminal Will Now Reboot")
sleep(2)
os.reboot()
end
Loopin #2
Posted 10 May 2012 - 09:40 PM
Well, you can simple do functions!

Spoiler


local pass = "diggydiggyhole"
os.pullEvent=os.pullEventRaw

function login()--When pass is correct, this is called.
term.clear()
term.setCursorPos(1,1)
print ("Access Granted")
redstone.setOutput("left", true )
sleep(5)
os.reboot()
end

function bdlogin()--When pass is not correct, This is called.
term.setCursorPos(1,1)
term.clear()
print ("Security Code Invalid")
print (" ")
print ("Terminal Will Now Reboot")
sleep(2)
start()
end

function start()--Asks for password.
term.clear()
print ("At this time please enter your level 5 security code.")
print (" ")
write "Security Code: "
input = read("*")
if input == pass then
login()
else
bdlogin()
end
end

start()

MysticT #3
Posted 10 May 2012 - 10:10 PM
Well, you can simple do functions!

Spoiler


local pass = "diggydiggyhole"
os.pullEvent=os.pullEventRaw

function login()--When pass is correct, this is called.
term.clear()
term.setCursorPos(1,1)
print ("Access Granted")
redstone.setOutput("left", true )
sleep(5)
os.reboot()
end

function bdlogin()--When pass is not correct, This is called.
term.setCursorPos(1,1)
term.clear()
print ("Security Code Invalid")
print (" ")
print ("Terminal Will Now Reboot")
sleep(2)
start()
end

function start()--Asks for password.
term.clear()
print ("At this time please enter your level 5 security code.")
print (" ")
write "Security Code: "
input = read("*")
if input == pass then
login()
else
bdlogin()
end
end

start()

Well, it's good to use functions, but using recursive functions to repeat code is not always the best solution. You need to use a loop, there's three types of loops:
while loop:

while <condition> do
  -- code
end
for loop:

for <var> = <start>, <end> do
  -- code
end
repeat loop:

repeat
  -- code
until <condition>
Search for some tutorials to see how they work. Check out the lua manual.