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

Stop Terminating And Custom Terminating

Started by jay5476, 09 October 2013 - 02:35 AM
jay5476 #1
Posted 09 October 2013 - 04:35 AM
Hi this tutorial is supposed to help you learn how to stop terminating and create a custom terminate.
Note: there is no way to stop Ctrl+S or Ctrl+R

Stop Terminating
well stopping someone from terminating your program can be really useful and the fact that you only need this line to do it makes it better

os.pullEvent = os.pullEventRaw
now for an explaination
The way ComputerCraft handles terminates is if Ctrl+T is held for 1 second(correct me if im wrong) it will send a 'terminate' event
now os.pullEvent acts on this event and then terminates. how it terminates:
Spoileros.pullEvent handles terminates like this

error("Terminated",0)
which will stop the exit the program and causes red(advanced) or white text 'terminated' to appear after exiting
and by doing that line of code it overrides os.pullEvent (which handles terminates) with os.pullEventRaw which doesn't check for the terminate event, basically os.pullEvent was built in to check for terminate and os.pullEventRaw wasn't even though they provide same functionality

Custom Terminates
well this is different you have to code os.pullEvent to handle the terminate how you want so the way I like to do it is

we want to override os.pullEvent to handle the terminate how we want so we could do something like this

os.pullEvent = function( _sFilter )
  eventData = {os.pullEventRaw( _sFilter } -- os.pullEventRaw will give us the raw event data and store it in this table
  if eventData[1] == "terminate" then -- identify the event and see if its terminate
	programName = shell.getRunningProgram()
	error("User has terminated the program: "..programName,0)
	 -- this example changes it to say the text between the quotes and then the name of the program
	 -- change the code after the 'if' to do whatever you want with the terminate even if its nothing
  end
end
now basically this code above can be changed to handle your events however you want as long as you code it to do so

I hope this tutorial helped and provided the information you needed if it didn't please leave a comment
be sure to also leave some feedback (good or bad I don't mind)
Zudo #2
Posted 09 October 2013 - 12:51 PM
Hmm, what uses would this have?
jay5476 #3
Posted 09 October 2013 - 04:49 PM
custom terminate? well you could log a terminate or give a notification to the person
AgentE382 #4
Posted 09 October 2013 - 07:06 PM
Save state on user intervention.

For example, I'm working on a program that maintains a mapping of files to a certain type of ID, saving the database to disk after every session and loading it before the next session. Normal users can call my program from the command-line, which is no problem. Each call is a single session. But, developers can also load it as an API, for use in their own programs.

I could use something like this to safeguard developers from noob users who will whine about the other developers' programs losing their data after they press Control-T.
awsmazinggenius #5
Posted 17 October 2013 - 05:41 PM
I have a screensaver that only resets with Ctrl + T (yeah, I'm a noob), so it would be a nice thing to do to reset the monitor to default colours on exit.