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

How to gracefully exit a script?

Started by dngrzone, 20 January 2014 - 07:21 PM
dngrzone #1
Posted 20 January 2014 - 08:21 PM
I am making a startup script for turtles.

Very new to Lua and computer craft.

The script displays the label, fuel, any active redstone signals, gps etc…

The problem :

at the tail end of the script I am using the parallel.waitForAny() function to either wait indefinitely for a rednet message, or upon an os.pullEvent("key")

it exits out into the shell. The rednet part of the parallel process seems to work fine. I can't figure out how to exit out of the script on a key press cleanly.

shell.exit() resulted in a black screen of death.

Right now I have it set to display the OS version, which quits out to a shell afterwards. Which does work, but it feels like a band aid fix.

Is there a better way to do it?



-- TurtleStart
-- Version 0.0.1
-- Build Status : Alpha
-- TurtleOS Version: 1.5
-- Author : Dngrzone

--Label

local label = os.getComputerLabel()
print("Name : ", label)

--Fuel
local gas = turtle.getFuelLevel()
print("Fuel Level:",gas)

--Peripheral
local wifi = peripheral.wrap('right')
rednet.open("right")
if rednet.isOpen("right") then
   print("Wifi: ON")
   else
   print("Wifi: OFF")
end

--Check for active redstone signals
local redstoneLeft = redstone.getInput("left")
local redstoneRight = redstone.getInput("right")
local redstoneTop = redstone.getInput("top")
local redstoneBottom = redstone.getInput("bottom")
local redstoneFront = redstone.getInput("front")
local redstoneBack = redstone.getInput("back")

--Then Display them
print("Active Redstone: ")
print("T: ",redstoneTop)
print("L: ",redstoneLeft," F: ",redstoneFront," R: ",redstoneRight)
print("Bm: ",redstoneBottom," Bk: ",redstoneBack)

--GPS
local x, y, z = gps.locate(2)
if x ~= nil then
print("GPS: Active")
else
print("GPS: Out of Range")
end

-- The following section is not implemented due to the GPS towers not yet being deployed.
-- Getting Location
--local home = vector.new(home.x, home.y, home.z)
--local position = vector.new(gos.locate(5))
--local displacement = position - home
--print("Location: ", displacement.tostring(), " from home.")

--Display the console and wait for commands over wifi in the background
local wifiRX = function()
local TXid, message, distance = rednet.receive()
print("Received: ",message," From :", TXid)
print("Executing :", message)
shell.run(message)
end
local consoleInput = function()
print("Awaiting Rednet, press any key to override")
local event, key = os.pullEvent("key")
local version = os.version()
print(version)
end
parallel.waitForAny(wifiRX,consoleInput)

Also, any help on how to clean up the script, display the text better, etc.. would be greatly appreciated.

Constructive Criticism welcome.
Edited on 20 January 2014 - 09:09 PM
CometWolf #2
Posted 21 January 2014 - 12:19 AM
Im guessing you know how os.pullEvent works, since you have it in your code. So a better option here instead of parallel, would be a while true do loop with pullEvent at the top and some ifs afterwards. Use the ifs to check for key events or rednet events, run the code you want in each event and put a return at the end of each. I suppose a return would do the trick for exiting the program in your current code aswell however.
Edited on 20 January 2014 - 11:21 PM
CoLDarkness #3
Posted 21 January 2014 - 07:46 AM
Just to post a more simpler thing:


function addmetotehparallel()
e,v = os.pullEventRaw("INSERTKEYPRESSEVENT")
if v == keyyoudesire then
return
end
end
--[[
parallel.waitForAny will wait for ANY function to end for the whole process. So a return will make it all quit.
--]]
Zudo #4
Posted 21 January 2014 - 12:47 PM
To quit, use this snippet:


term.clear()
term.setCursorPos(1,1)
error()

Error isn't exactly graceful, but there isn't really a graceful way to do it :P/>
subzero22 #5
Posted 22 January 2014 - 12:25 PM
well hmm. you could do shell.run("shell") and it will be just like the computer boot back up without the actual boot up.
Bomb Bloke #6
Posted 22 January 2014 - 05:50 PM
Error isn't exactly graceful, but there isn't really a graceful way to do it :P/>
I dunno, return codes used to be called "error levels" back in the day.

Programs typically throw an error level of 0 on exit (to indicate a straight-forward success), or other values which might indicate problems or perhaps other things. Batch files and whatnot can use these codes to determine what to do next.
Edited on 22 January 2014 - 04:51 PM
albrat #7
Posted 23 January 2014 - 05:58 AM
I would use the following to exit a script with a message of completion.
printError("Program Complete") --// sets the error message for the error
error()
I was just checking with bios.lua on the 1.58 version of CC .
error("Program Complete")
Should achieve the same effect looking at the way this is handled (I have not tested this method of exiting.)