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

Parallel inputs

Started by Doyle3694, 05 October 2012 - 03:36 PM
Doyle3694 #1
Posted 05 October 2012 - 05:36 PM
Hey everyone, I'm making a advanced password program taking many users and passwords and so on and so forth. My prblem is, I want users to be able to ,when they are going to enter their password, either input a string or press the back key to input another username. I discovered parallel api won't do this. Will I have to check for os.pullEvent and then break it down and insert it all into a string, like making my own read()
KaoS #2
Posted 05 October 2012 - 07:06 PM
a simple but not very clean method is to make one program ask for the username and then call a second program that asks for the password while having a coroutine listen for key presses, when the requred key is pressed it calls error() which terminates the current program - sending you back to the user prompt. there are more complicated but cleaner methods (developing your own function :(/>/>) and if you would prefer that let us know
jag #3
Posted 05 October 2012 - 07:15 PM
I don't know if I exactly know what you mean, but I think that I've made one before: C1wgp6ex

Raw code:
Spoiler
function checkLogin(uname, pass)
  -- Yes, very easy to hack...
  if uname ~= "admin" and pass ~= "password" then
		return true
  else
		return false
  end
end

function login()
  local inputUname = ""
  local inputPass = ""

  while not checkLogin(inputUname, inputPass) do
		term.clear()
		if inputUname ~= "" or inputPass ~= "" then
		  term.setCursorPos(3,2)
		  print("Wrong username or password!")
		end
		term.setCursorPos(3,3)
		print("Username: ") -- 10 char long
		term.setCursorPos(3,4)
		print("Password: ") -- 10 char long

		-- Now comes the input
		term.setCursorPos(13,3)
		inputUname = read()
		term.setCursorPos(13,4)
		inputPass = read("*")
  end
  term.clear()
  term.setCursorPos(3,2)
  print("Correct login!")
  sleep(2)
end

Ofc if you want to change the way the program checks the password, just change the function checkLogin() to whatever you desire.

Or if you wan't the login() function to return true/false when done (you know, with a limited amount of tries): F4hgGUfL

Raw code:
Spoiler
function checkLogin(uname, pass)
  -- Yes, very easy to hack...
  if uname ~= "admin" and pass ~= "password" then
	return true
  else
	return false
  end
end

function login()
  local inputUname = ""
  local inputPass = ""
  local tries = 0

  while not checkLogin(inputUname, inputPass) do
	term.clear()
	if inputUname ~= "" or inputPass ~= "" then
	  term.setCursorPos(3,2)
	  print("Wrong username or password!")
	end

	if tries > 4 then
	  term.setCursorPos(3,3)
	  print("No more tries left!")
      sleep(2)
	  return false
	end

	term.setCursorPos(3,3)
	print("Username: ") -- 10 char long
	term.setCursorPos(3,4)
	print("Password: ") -- 10 char long
	term.setCursorPos(3,5)
	print("Tries left: "..5-tries)

	-- Now comes the input
	term.setCursorPos(13,3)
	inputUname = read()
	term.setCursorPos(13,4)
	inputPass = read("*")
	tries = tries + 1
  end
  term.clear()
  term.setCursorPos(3,2)
  print("Correct login!")
  sleep(2)
  return true
end
Doyle3694 #4
Posted 05 October 2012 - 10:34 PM
will look into coroutine. jagenummerett's code didn'thelp at all, wasn't really waht I ment.
Doyle3694 #5
Posted 05 October 2012 - 10:35 PM
Also, can someone explain coroutine?