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

Problem with a code locked door

Started by KOD_28, 15 February 2013 - 07:47 AM
KOD_28 #1
Posted 15 February 2013 - 08:47 AM
Title : Problem with a code locked door

Hi there,

Today, I've made a code locked door but it doesn't work…

I used the code


local pullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
-- If the desired conditions are met, you can run this line to allow the program to be terminated.
os.pullEvent = pullEvent

to make people unavaliable to stop the program whitout an admin password…

But when the line


os.pullEvent = pullEvent

is censed to run, it doesn't and I can't stop the program…

If someone could help me it would be very kind ;-P

Thanks and have a good day !

Here is my code : (french parts are just printed texts)


local pullEvent = os.pullEventRaw
os.pullEvent = os.pullEventRaw
local side1 = "left"
local side2 = "right"
local opentime = 5
local musictime = 30
local pswd = "ftb"
local guest = "guest"
local admin = "admin"
local adminpswd = "..."
while true do
  term.clear()
  term.setCursorPos(1,1)
  term.setTextColor(colors.white)
  print("Entrez le mot de passe")
  term.setTextColor(colors.lightGray)
  print("(Si vous ne connaissez pas le code tapez 'guest' !)")
  term.setTextColor(colors.white)
  local input = read("*")
  if input == pswd then
	term.clear()
	term.setCursorPos(1,1)
	term.setTextColor(colors.lime)
	print("Bienvenue !")
	rs.setOutput(side1,true)
	sleep(opentime)
	rs.setOutput(side1,false)
  elseif input == guest then
	term.clear()
	term.setCursorPos(1,1)
	term.setTextColor(colors.blue)
	print("Attendez que l'on vienne vous ouvrir !")
	disk.playAudio(side2)
	sleep(musictime)
	disk.stopAudio(side2)
  elseif input == admin then
	term.clear()
	term.setCursorPos(1,1)
	term.setTextColor(colors.yellow)
	print("ADMIN MODE")
	print("Please enter Admin password:")
	input = read(*)
	  if input == adminpswd then
		term.clear()
		term.setCursorPos(1,1)
		term.setTextColor(colors.lime)
		print("You can now stop this program !")
		os.pullEvent = pullEvent
		sleep(opentime)
	  else
		term.clear()
		term.setCursorPos(1,1)
		term.setTextColor(colors.red)
		print("Wrong admin password !")
		sleep(opentime)
	  end
  else
	term.clear()
	term.setCursorPos(1,1)
	term.setTextColor(colors.red)
	print("Mauvais mot de passe !")
	sleep(opentime)
  end
end
Doyle3694 #2
Posted 15 February 2013 - 10:13 AM
In your actual code, you have pullEvent set to os.pullEventRaw rather than os.pullEvent.
mrbaconbitts #3
Posted 15 February 2013 - 10:14 AM

os.pullEvent = pullEvent

This line does not terminate the program. I forgot how I used to terminate my programs and I don't have the time to check up what the best way is to, you can simply use error("Some random text you want here") and it will terminate the program.

The previously posted code is used for people to check what events have fired while a program is running, for example, the event "redstone" is fired when the computer has gotten a redstone signal, etc etc etc
KOD_28 #4
Posted 15 February 2013 - 10:21 AM
@mrbaconbitts your method is a bit hardcore xD But it works thanks ;-)
@doyle3694 ok and what should I do ? local pullEvent = os.pullEvent ?
KOD_28 #5
Posted 15 February 2013 - 10:25 AM
new code actually looks like


if input == adminpswd then
		term.clear()
		term.setCursorPos(1,1)
		error("You stoped th program !")
:P/>
mrbaconbitts #6
Posted 15 February 2013 - 04:03 PM
Deleted
Bubba #7
Posted 15 February 2013 - 04:40 PM
What the line os.pullEvent = pullEvent is supposed to do is allow the user to terminate using ctrl+t, not automatically terminate the program. Is that what you are doing?

The easiest way to terminate the program would be to put a "break" in after the user enters a correct password like so:

while true do
  local input = read("*")
  if input=="password" then
     print("Stopping!")
     break
  else
     print("Incorrect password!")
     sleep(5)
  end
end
Another way to do it would be to put the while true do loop in a function and then have the admin password return a value like so:

local function run()
  while true do
    if read("*") == "password" then
      return true
    else
      print("Bad password!")
      sleep(5)
    end
  end
end

run()

Although using error("message") works, it was not really designed for this purpose.
Doyle3694 #8
Posted 15 February 2013 - 06:39 PM
Oh and yep KOD_28, "local pullEvent = os.pullEvent" should be just fine. However, if you want a direct exit there are a few options.


exit() -- Exits one step in the shell
error() -- Exits to the top shell, the difference between those 2 only make sense if you use shell.run() I believe.

There also is

os.pullEventRaw("terminate") - Which will wait for them to terminate and then move on in the code
KOD_28 #9
Posted 23 February 2013 - 12:59 PM
Hi there I just finished my program and it now works perfectly so I paste it here if you want to try it ;)/>

I also added an Admin Mode which alows you to stop the program or edit the password :)/>

Here is the pastebin link to my LockDoor program : http://pastebin.com/hRkMdpe0

Thank you for your help !

Have a nice day !