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

Preventing Program Termination

Started by grand_mind1, 02 April 2013 - 11:27 AM
grand_mind1 #1
Posted 02 April 2013 - 01:27 PM
I wrote a password program recently and I want to make sure that it can't be terminated. I have had problems with this is the past, but that was when I was just starting Lua so I didn't think it was going to be a problem. However, I am still having problems with it. I tried to read more on os.pullEvent() and os.pullEventRaw() because those are the two involved in preventing termination(as far as I can tell), but I didn't really understand. I read something about 'pcall()' instead of 'read' or 'sleep' and honestly none of it made much sense. I tried to just add 'os.pullEventRaw' or 'os.pullEvent=os.pullEventRaw' to my code but it didn't work and I didn't really expect it to. If someone could tell me how to do this that'd be great!
Help is appreciated!
Thanks! :D/>
SuicidalSTDz #2
Posted 02 April 2013 - 01:31 PM
os.pullEvent = os.pullEventRaw at the top of your code WILL prevent termination. Please post your code if it can be terminated
oeed #3
Posted 02 April 2013 - 01:45 PM
You can catch/prevent termination using os.pullEventRaw.
When the user tries to terminate the program the event 'terminate' is fired. However, os.pullEvent handles this, so you need to use os.pullEventRaw as os.pullEvent uses os.pullEventRaw.

You could add the following to prevent it, but as SuicidalSTDz said, post some code.
You need to be careful where you put this, if your code uses a loop put it in there, the application will lock up otherwise.

local event, p1, p2, p3 = os.pullEventRaw()
if event == 'terminate' then
    print("Attempted to terminate.")
end
grand_mind1 #4
Posted 02 April 2013 - 03:25 PM
Sorry, I didn't realize I needed to post the code. I thought it was just a general bit of code I could put anywhere. Here it is:

function getPass(type)
  rednet.send(58,"Reg")
  id, msg, dis = rednet.receive(10)
  regpass = msg
  rednet.send(58,"Access")
  id, mesg, dis = rednet.receive(10)
  access = mesg
end
rednet.open("top")
while true do
  term.clear()
  term.setCursorPos(1,1)
  print("This computer is protected!")
  print("Password: ")
  term.setCursorPos(10,2)
  input = read()
  getPass()
  if input == regpass then
	redstone.setOutput("left", true)
	sleep(3)
	redstone.setOutput("left",false)
  elseif input == access then
	error()
  end
end
SuicidalSTDz #5
Posted 02 April 2013 - 03:30 PM
Put os.pullEvent = os.pullEventRaw above your function and it should be fine. Works for me, so.

Nice use of rednet to store your password btw ^_^/>
JokerRH #6
Posted 02 April 2013 - 03:47 PM
Just to point out:
os.pullEventRaw will simply do coroutine.yield and it returns any event, including the "terminate" event.
os.pullEvent however will call os.pullEventRaw and if the event is "terminate" it will throw an error.
Therefor you could use pcall(os.pullEvent, "stringFilter") or os.pullEventRaw.
If you write os.pullEvent = os.pullEventRaw you will overwrite os.pullEvent and it is practically os.pullEventRaw
SuicidalSTDz #7
Posted 02 April 2013 - 03:48 PM
That's the idea..