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

[Solved] Startup/Autorun And Corouties

Started by Lupus590, 02 March 2015 - 07:25 PM
Lupus590 #1
Posted 02 March 2015 - 08:25 PM
I need a script to run on startup (ideally from the auto run folder) and go into the background.

My intention is that the script will periodically send rednet messages to a server giving details on the computer that it's running on (location, running programs, errors, etc.).

I know I could have a startup program which runs my tracking script in parallel with other scripts that I want running, but I would like it to be called from the autorun folder.

I did try this with a refueling script before.

if(not turtle) then error("script must be run on a turtle",2) end--the 2 tell lua to blame the line that called this script
if(turtle.getFuelLevel() == "unlimited") then return end--script is useless with unlimited fuel, so exit
--write("Warning: automatic refuel is enabled, items may disappear if the turtle runs low on fuel.\n".."To prevent this make sure that the turtles fuel level stays above "..toString(fuelReserveLevel).."\n".."the automatic refuel script will take fuel one at a time until the above value is reached or exceeded.")
local function autoFuelBackground()
	local fuelReserveLevel = 10
	
	while true do
		if(turtle.getFuelLevel() < fuelReserveLevel) then
			turtle.refuel(1)
		end
		sleep(0.1)
	end
end

autoFuelRoutine = coroutine.create(autoFuelBackground)
coroutine.resume(autoFuelRoutine)
return autoFuelRoutine


Edited on 08 March 2015 - 06:49 PM
TheOddByte #2
Posted 02 March 2015 - 09:29 PM
I'm just gonna leave this here.. http://www.computercraft.info/forums2/index.php?/topic/19908-run-code-in-background/
ElvishJerricco #3
Posted 02 March 2015 - 09:38 PM
CraftOS doesn't have any autorun folder except the one in the rom that you can't put programs in (except via resource pack, but that puts it on every computer). Kinda sucks that CraftOS completely lacks the ability to have multiple startup items without a custom startup script to run those files.

Anyway, in the startup file, if you're using an advanced computer, you can use the bg program to run another program in the background.


--startup
shell.run("bg myProgram")
Lupus590 #4
Posted 02 March 2015 - 11:17 PM
So (to double check) if I want my project to be portable (read: non-resource pack) then I will have to move it out of the autorun rom folders (or implement my own non-rom version) other than that, the parallel API will work.

My project is beginning to sound like it will almost be its own OS or at least coroutine manager.
Edited on 02 March 2015 - 10:25 PM