Here's how to save a var.
local sStatus = "OFFLINE" -- i'll use this for example
--check to make sure file exists, good to do at start of the program
if not fs.exists("status") then
local file = io.open("status", "w") -- open file in write mode
file:write()
file:close() -- always close the handle.
end
--save the var
local file = io.open("status", "w')
file:write(sStatus)
file:close()
--read the var
local file = io.open("status", "r") --open in read mode
sStatus = file:read()
file:close()
For more info read up on the fs and io API's. Note that CC does not have all of the io functions available.
To make a code go run a specific task make it a function.
local function getPass()
write("Enter Password")
input = read("*")
end
getPass() -- to call it.
Make sure the function is above the code that is calling it or it will error.
Thanks, going to try implement it now :D/>/>
Yes save them to a file and then load them when the programs starts.
Another question: How do i make a code go somewhere specifically within the code? Ex: After entering the password for my code and then doing an action it will go back to 'Please enter the password'. How do i make it go to the point after entering the password?
Functions. That is what you're looking for. Example:
function login()
write("Please Enter Password: ")
pass = read()
if pass = "mypass" then
stuff()
elseif
login()
end
end -- Ends the function login()
function stuff()
<Your Program, or whatever>
end -- Ends the function stuff()
login()
Yes i know how to do this, but i use a while true do.
This is my code now. If you guys could help me, after entering the password I wan't it to stay logged in until you manually logout yourself. So you don't have to re-enter the password after choosing an option.
-- Configuration
-- Strings
local sBypasspw = "xxx"
local sDoorpw = "derp"
local sDoorStatus = ""
local sRedstoneSideClose = "back"
local sRedstoneSideOpen = "bottom"
local sName = "The Sorting Facility"
local wp = "print"
-- Values
local nTimer = 1
-- Do not edit from here
-- Functions
local function menu(...)
local sel = 1
local list = {...}
local offX,offY = term.getCursorPos()
local curX,curY = term.getCursorPos()
while true do
if sel > #list then sel = 1 end
if sel < 1 then sel = #list end
for i = 1,#list do
term.setCursorPos(offX,offY+i-1)
if sel == i then
print("["..list[i].."]") -- very customisible example print(">"..list[i])
else
print(" "..list[i].." ") -- very customisible
end
end
while true do
local e,e1,e2,e3,e4,e5 = os.pullEvent()
if e == "key" then
if e1 == 200 then -- up key
sel = sel-1
break
end
if e1 == 208 then -- down key
sel = sel+1
break
end
if e1 == 28 then
term.setCursorPos(curX,curY)
return list[sel],sel
end
end
end
end
end
function clearPrint(string, x, y)
if not x or x < 0 then x = 1 end
if not y or y < 0 then y = 1 end
term.clear()
term.setCursorPos(x,y)
print(string)
end
function skipLine()
print(" ")
end
function Welcome(string)
clearPrint("+-----------------------------------------------+")
print("+---- Welcome to "..sName.." ----+")
print("+-----------------------------------------------+")
skipLine()
if wp == "print" then
print(string)
elseif wp == "write" then
write(string)
end
end
function Open()
if sDoorStatus == "opened" then
Welcome("The doors are already open!")
sleep(1.5)
elseif sDoorStatus == "closed" then
Welcome("Opening doors...")
while nTimer <=4 do
rs.setOutput(sRedstoneSideOpen,true)
sleep(.425)
rs.setOutput(sRedstoneSideOpen,false)
sleep(.425)
nTimer = nTimer + 1
end
nTimer = 0
Welcome("Doors have been opened!")
sDoorStatus = "opened"
sleep(1)
else
Welcome("ERROR.")
print("CURRENT DOOR STATUS: "..sDoorStatus..". What is wrong. WHAT THE HELL IS WRONG?")
sleep(60)
end
end
function Close()
Welcome("Closing garage doors...")
sleep(1.5)
if sDoorStatus == "closed" then
Welcome("The doors are already closed!")
sleep(1.5)
elseif sDoorStatus == "opened" then
Welcome("Closing doors...")
while nTimer <=4 do
rs.setOutput(sRedstoneSideClose,true)
sleep(.425)
rs.setOutput(sRedstoneSideClose,false)
sleep(.425)
nTimer = nTimer + 1
end
nTimer = 0
Welcome("Doors have been closed!")
sDoorStatus = "closed"
sleep(1)
else
Welcome("ERROR.")
print("CURRENT DOOR STATUS: "..sDoorStatus..". What is wrong. WHAT THE HELL IS WRONG?")
sleep(60)
end
end
function Logout()
Welcome("Logging out...")
sleep(1.5)
end
-- if statement
function checkPW(PW)
if PW == sDoorpw then
Welcome("Password correct!")
sleep(1.5)
Welcome("The door is currently "..sDoorStatus..".")
print("Choose an option:")
local selection = menu("Open", "Close", "Logout")
if selection == "Open" then
Open()
elseif selection == "Close" then
Close()
elseif selection == "Logout" then
Logout()
end
elseif PW == sBypasspw then
Open()
else
Welcome("Password invalid.")
sleep(1.5)
end
end
while true do
if sDoorStatus == "closed" or sDoorStatus == "opened" then
wp = "write"
Welcome("Enter password to access garage: ")
local selection = ""
wp = "print"
if checkPW(read("*")) == sBypasspw then break end
else
Welcome("Door has an unknown status. opened or closed?")
print(sDoorStatus)
input = read()
if input == "opened" or input == "closed" then
sDoorStatus = input
else
Welcome("Unknown status.")
sleep(2)
end
end
end