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

Program status / termination

Started by DaRc, 27 May 2015 - 05:36 AM
DaRc #1
Posted 27 May 2015 - 07:36 AM
Good morning! I have been working with ComputerCraft's API for BigReactors to control a BigReactor and read date remotely from it. It has been rather helpful thus far, but I am trying to do a few things I am not really sure how to accomplish as I am fairly new to Lua. Below is a general example of what I am trying to accomplish. As always any/all help is appreciate and thanks in advance!!!!

I have a program called reactor which contains all the methods for controlling and monitoring the reactor. I would like to use another program such as the basic startup file to check if that program is actually running, and if it is, print to the terminal (not monitor). I would also like to be able to read if the script halted due to an error, and of course, print the normal error date on screen without printing the custom terminal information as per the above request.

Now to the next part, sounds simple in my head… I simply would like to be able to detect if the program was TERMINATED with ctrl + t, and if so, have it perform a specific set of commands (clearing the terminal for example).
valithor #2
Posted 27 May 2015 - 07:55 AM
I would like to use another program such as the basic startup file to check if that program is actually running, and if it is, print to the terminal (not monitor).

The problem with this is only one program is really ever running at a time (will come back to coroutines in a second). You could very easily just put print statements throughout your original program to relay the information, no reason for two programs. If you really want 2 programs you could even use shell.run to run the second program from the original.

Now the one exception to all of this is coroutines. When you run a program in CC all that is happening is the code is being read, and then the entire program is being loaded as a function. Now coroutines are just a way to easily manage events across multiple functions. You could technically load both files as functions, and then use the parallel api or manually handle the coroutines to run both at the same time.

I would also like to be able to read if the script halted due to an error, and of course, print the normal error date on screen without printing the custom terminal information as per the above request.

You might want to look into pcall link: http://www.lua.org/pil/8.4.html
pcall will run the code passed to it, and will either return 1 or 2 things.
If the code does not error it will return true, if the code does error it will return false, and the error
an example of pcall:

function thiswillerror()
  thisisnotafunction()
end
ok,err = pcall(thiswillerror)

if ok==false then
  print(err)
else
  -- it didn't error
end

Now to the next part, sounds simple in my head… I simply would like to be able to detect if the program was TERMINATED with ctrl + t, and if so, have it perform a specific set of commands (clearing the terminal for example).

This one is much simpler than the other two requests. When you hold ctrl+t in a computer the computer will que a "terminate" event. The thing about terminate events is you can capture them and decide how you want to react to them, instead of the computer automatically stopping the script.


local oldpull = os.pullEvent -- saving a copy of the old os.pullEvent
os.pullEvent = os.pullEventRaw -- os.pullEvent terminates when it receives terminate event, pullEventRaw does not

while true do
  evt = {os.pullEvent()}
  if evt[1] == "terminate" then
	print("terminate event was qued")
	-- do other stuff here
  end
end

os.pullEventRaw = oldpull -- always reset what you changed


Just know I made this post at 2 am, and I am half asleep, so some of the information might be slightly off.
Edited on 27 May 2015 - 06:09 AM