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

[Lua][Question] Function failing after success?

Started by nateracecar5, 23 May 2013 - 07:14 PM
nateracecar5 #1
Posted 23 May 2013 - 09:14 PM
My centerPrint(text, yLoc2) function is failing after the second attempt. It works the first time you use it then it spits out an error. Any help? I think the error is somewhere down near the main code. It only says the error in the function that I'm using. Oh, I am getting tired of debugging this thing.


--Variables (For random stuff)
local yPos = 3
local xPos = 1
local currentVersion = 1.0
--Functions
function setColors(backColor, textColor)
term.setBackgroundColor(backColor)
term.setTextColor(textColor)
end
function printY(text, yLoc2)
term.setCursorPos(1, yLoc2)
print(text)
if yLoc2 == yPos+1 then
  yPos = yPos+1
end
end
function centerPrint(text, yPos)
  w, h = term.getSize()
  term.setCursorPos(math.floor(w-text:len())/2, yPos)
  print(text)
end
local function downloadFiles(url, path)
for i = 1, 3 do
  local response = http.get(url)
  if response then
   local data = response.readAll()
   if path then
	local f = io.open(path, "w")
	f:write(data)
	f:close()
	sleep(2)
	installing = "false"
	centerPrint("Download success!")
   end
   return true
  end
end
centerPrint(error("download: Download Failed"))
return false
end
function writeY(text, yLoc)
term.setCursorPos(1, yLoc)
write(text)
if yLoc == yPos+1 then
  yPos = yPos+1
end
end
--Main Code
w, h = term.getSize()
setColors(colors.white, colors.cyan)
term.clear()
centerPrint("Checking for updates...", 1)
paintutils.drawLine(1, 2, w, 2, colors.blue)
downloadFiles("https://raw.github.com/redstonefreak589/redos/master/version", ".redos/os/version")
h = fs.open(".redos/os/version", "r")
versionNumber = h.readAll()
h.close()
if versionNumber ~= currentVersion then
term.setCursorPos(1,1)
term.clearLine()
centerPrint("New update available! Update now?", 1)
event, key = os.pullEvent("key")
if key == 21 then
  fs.delete(".redos/os/version")
  shell.run("/installer")
elseif key == 49 then
  term.setCursorPos(1,1)
  term.clearLine()
  fs.delete(".redos/os/version")
end
else
term.setCursorPos(1,1)
term.clearLine()
centerPrint("No updates avaible", 1)
term.setCursorPos(1,1)
term.clearLine()
end
centerPrint("Welcome To RedOS Login!", 1)
setColors(colors.white, colors.cyan)
centerPrint("RedOS Login System", 8)
sleep(2)
term.setCursorPos(1,3)
printY("RedOS Login", 2)
printY("Username", 3)
writeY("> ", yPos+1)
input = read()
if fs.exists(".redos/login/user") then
h = fs.open(".redos/login/user", "r")
userName = h.readAll()
h.close()
if input == userName then
  term.setCursorPos(1,3)
  print("Password")
  yPos = yPos-1
  term.setCursorPos(1,4)
  term.clearLine()
  writeY("> ", yPos+1)
  input2 = read("*")
  if fs.exists(".redos/login/pass") then
   h = fs.open(".redos/login/pass", "r")
   password = h.readAll()
   h.close()
   if input2 == password then
	printY("Login Confirmed! Loading desktop...", yPos+1)
	sleep(2)
	if fs.exists(".redos/os/desktop") then
	 shell.run(".redos/os/desktop")
	else
	 term.setBackgroundColor(colors.black)
	 term.clear()
	 term.setCursorPos(1,1)
	 error("desktop: OS corrupt; destop file not present")
	end
   else
	printY("Password incorrect!", yPos+1)
   end
  end
else
  printY("Username incorrect!", yPos+1)
end
else
if not fs.exists(".redos/login") then
  fs.makeDir(".redos")
  fs.makeDir(".redos/login")
end
h = fs.open(".redos/login/user", "w")
printY("Creating new user file", yPos+1)
h.write(input)
h.close()
printY("Type in your new password", yPos+1)
writeY("> ", yPos+1)
newP = read("*")
h = fs.open(".redos/login/pass", "w")
h.write(newP)
h.close()
term.setCursorPos(1,8)
term.clearLine()
printY("Password File generated!", yPos+1)
printY("OS rebooting...", yPos+1)
sleep(2)
os.reboot()
end
Orwell #2
Posted 23 May 2013 - 09:39 PM
This is the line where it happens. error() doesn't return a string, it actually throws an error. So that's where you get the error, right?

centerPrint(error("download: Download Failed"))

If you want it to print the error centered and properly exit afterwards, you could use this function:

function centerPrintError(text, yPos)
  w, h = term.getSize()
  term.setCursorPos(math.floor(w-text:len())/2, yPos)
  printError(text)
  error()
end
nateracecar5 #3
Posted 23 May 2013 - 10:02 PM
This is the line where it happens. error() doesn't return a string, it actually throws an error. So that's where you get the error, right?

centerPrint(error("download: Download Failed"))

If you want it to print the error centered and properly exit afterwards, you could use this function:

function centerPrintError(text, yPos)
  w, h = term.getSize()
  term.setCursorPos(math.floor(w-text:len())/2, yPos)
  printError(text)
  error()
end

I will try
nateracecar5 #4
Posted 23 May 2013 - 10:08 PM
This is the line where it happens. error() doesn't return a string, it actually throws an error. So that's where you get the error, right?

centerPrint(error("download: Download Failed"))

If you want it to print the error centered and properly exit afterwards, you could use this function:

function centerPrintError(text, yPos)
  w, h = term.getSize()
  term.setCursorPos(math.floor(w-text:len())/2, yPos)
  printError(text)
  error()
end
Not working. Another reason? Try running the actual code.
nateracecar5 #5
Posted 23 May 2013 - 10:19 PM
Okay, I just did a total rewrite of the update code and fixed it
Orwell #6
Posted 23 May 2013 - 10:40 PM
Ah, good. By now I have fixed it as well. I had to fix 3 or 4 errors to get through it. The first one was that you didn't create the directory explicitely if it wasn't there yet (e.g. .redos/os/). Then you had the problem of not specifying the y value for centerPrint some times and finally there was the problem with the centerPrint(error("foobar")). Also, you didn't clear lines on centerPrint, thus the text overlapped.

Here is my outcome: http://pastebin.com/6iN0xuWy
nateracecar5 #7
Posted 23 May 2013 - 10:52 PM
Thanks, but by now I have made it look cleaner and stuff. I hope you liked because I will be using this as my login system for my new OS. Thanks for your help!