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

Help optimizing my code?

Started by DarkEyeDragon, 13 June 2015 - 07:51 PM
DarkEyeDragon #1
Posted 13 June 2015 - 09:51 PM
http://pastebin.com/T2SejYff –Pastebin link.

--Disable the terminating of programs
os.pullEvent = os.pullEventRaw
--------------------------------------
loginValue = false
value = 0
goToRoot = false
--Startup Function-
function startup()
  term.clear()
  term.setCursorPos(1,1)
  print("Booting System...")
  while value < 17 do
    value = value + 1
    paintutils.drawPixel(value,2, colors.red)
    sleep(0)
  end
  paintutils.drawLine(1,2,17,2, colors.green)
  term.setCursorPos(5,2)
  print("Complete")
  paintutils.drawPixel(0,0,colors.black)
  term.setCursorPos(1,4)
end
--Login Fucntion--
function login()
  loggedInAs = null
  if loginValue == false then
    print("Username:")
    local username = read()
    print("Password:")
    local password = read("*")
    if username == "Dark" and password == "Dark" then
	  print("Logged in as ", username)
	  loggedInAs = username
	  loginValue = true
    else
	  print("Invalid password.")
	  sleep(2)
	  startup()
    end
  else
    print("logging in as: " , username)
  end
end
----------------------
-- end of functions --
----------------------
startup()
print("Available actions: terminal, login, logout, root")
while goToRoot == false do
  input = read()
  if input == "terminal" then
    print("Enabeling AE Terminal")
    login()
  end
  if input == "login" then
    login()
  end
  if input == "logout" then
    loginValue = false
    print("Successfully logged out.")
  end
  if input == "root" then
    if loginValue == false then
	  print("Root access requires you to be logged in.")
	  login()
    end
    if loginValue == true then
	   goToRoot = true
    end
  end
end
startup()

How could I improve this code? I am quite sure there are way 'cleaner' and easier ways of doing this. Any suggestions?
Bomb Bloke #2
Posted 14 June 2015 - 03:01 AM
  • You should undo your change to os.pullEvent() as the script ends.
  • Don't forget about "elseif"!
  • Implement variable localisation.
  • You could simply use the "break" keyword instead of checking your "goToRoot" variable.
  • "loggedInAs" doesn't appear to serve any purpose.
  • "terminal" and "login" both do the same thing.
  • Your "while value < 17 do" loop would be better phrased as "for value = 1, 17 do", ditching the "value = 0" and "value = value + 1" lines.
  • "<variable> == true" is redundant; just use "<variable>". If it equals true, then that means it was already true!
  • "not <variable>" is a neater method of writing "<variable> == false".
Edited on 14 June 2015 - 01:01 AM
DarkEyeDragon #3
Posted 14 June 2015 - 09:12 AM
  • You should undo your change to os.pullEvent() as the script ends.
  • Don't forget about "elseif"!
  • Implement variable localisation.
  • You could simply use the "break" keyword instead of checking your "goToRoot" variable.
  • "loggedInAs" doesn't appear to serve any purpose.
  • "terminal" and "login" both do the same thing.
  • Your "while value < 17 do" loop would be better phrased as "for value = 1, 17 do", ditching the "value = 0" and "value = value + 1" lines.
  • "<variable> == true" is redundant; just use "<variable>". If it equals true, then that means it was already true!
  • "not <variable>" is a neater method of writing "<variable> == false".
Thanks. I'll be get that fixed. You're a great help to nubs like me :P/>
DarkEyeDragon #4
Posted 14 June 2015 - 09:28 AM
  • You should undo your change to os.pullEvent() as the script ends.
  • Don't forget about "elseif"!
  • Implement variable localisation.
  • You could simply use the "break" keyword instead of checking your "goToRoot" variable.
  • "loggedInAs" doesn't appear to serve any purpose.
  • "terminal" and "login" both do the same thing.
  • Your "while value < 17 do" loop would be better phrased as "for value = 1, 17 do", ditching the "value = 0" and "value = value + 1" lines.
  • "<variable> == true" is redundant; just use "<variable>". If it equals true, then that means it was already true!
  • "not <variable>" is a neater method of writing "<variable> == false".

Ok i altered the script. Theres 1 issue i noticed now. Previously the var value would stay equal to 17 as long as the computer was on. This draws a progressbar. But once the system was completely start up it wouldn't have to fill the progress bar again. Because the value was already 17. So when i would use the clear command it would trigger the startup() function. The progress bar would already be filled when its drawn again. Now that I changed it to "for value = 1,17 do" it would refill the progress bar every time. Would in that case using a variable to store the value in be a better option?
Edited on 14 June 2015 - 07:32 AM
Bomb Bloke #5
Posted 14 June 2015 - 09:36 AM
Yes, I suppose it would. I'd assumed you'd've wanted the bar to fill every time.
DarkEyeDragon #6
Posted 15 June 2015 - 12:47 PM
Yes, I suppose it would. I'd assumed you'd've wanted the bar to fill every time.

Thats fine. Its not like it has any function. Its more of an astetic thingy. Thanks for you help though! Finally starting to get my around in coding :P/>