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

Daemon API

Started by HDeffo, 07 February 2016 - 10:01 PM
HDeffo #1
Posted 07 February 2016 - 11:01 PM
There have been several of these most of which are buried and hidden in the forums however, I usually notice they aren't very flexible either by not filtering out events passed to them or like the parallel API not dynamic and limiting the number of background processes that can be run. So here is the code i personally use when i need to run several things at once in case anyone on here is ever interested in using it as well

pastebin get nByRT66R



VERY simple to use and very short code.


os.loadAPI("daemon")
--#starts the daemon change the string to whatever you named the file

daemon.add(function[, error])
--#starts a function in the background which will loop until the function reaches its end
--#error is a function that will be ran if the coroutine ends, it is passed the error code 

daemon.rem(table)
--#removes a currently running coroutine
--#pass it the table returned by daemon.add

daemon.getBackground()
--#returns a table of all coroutines currently in the background as well as the event they are waiting for to continue running





example:
Spoiler

os.loadAPI("daemon")
foo = function()
   while true do
	  local _,text = os.pullEvent("paste")
	  if text=="foo" and shell.getRunningProgram()~="rom/programs.lua" then
		 shell.run("lua")
	  end
   end
end
bar = function()
   while true do
	  local _,text = os.pullEvent("paste")
	  if text=="bar" then
		 os.reboot()
	  end
   end
end
daemon.add(foo,"paste to lua")
daemon.add(bar,"paste to reboot")
print("paste (ctrl+v) string 'foo' to enter lua program or paste string 'bar' to reboot computer")
Edited on 11 August 2018 - 01:02 AM
Creator #2
Posted 07 February 2016 - 11:53 PM
Does the function
add
start the whole thing?
Edited on 07 February 2016 - 10:53 PM
HDeffo #3
Posted 07 February 2016 - 11:56 PM

os.loadAPI("daemon")

is what starts the code however, it doesnt actually do anything significant besides rewriting os.pullEventRaw until you add a background function. ill add an example to the top to show you how it works
Edited on 07 February 2016 - 11:01 PM
HDeffo #4
Posted 08 February 2016 - 12:43 AM
Just fixed a bug where it would return events several times to each background function >.> just had to move the inPullEvent variable oops
Lupus590 #5
Posted 08 February 2016 - 08:46 AM
This looks like it could be very useful in an os
HDeffo #6
Posted 08 February 2016 - 01:00 PM
This looks like it could be very useful in an os

Thats actually one of the things I frequently use it for. As well as when I need quite a few functions which require pull events.
Lupus590 #7
Posted 08 February 2016 - 01:17 PM
What's the license? Would I be able to use this in Hive?
HDeffo #8
Posted 08 February 2016 - 05:21 PM
What's the license? Would I be able to use this in Hive?

its very simple code so I didn't bother with a license if you want to put credits in there for me I would appreciate it but feel free to use the code however you please :3
Lupus590 #9
Posted 08 February 2016 - 05:47 PM
What's the license? Would I be able to use this in Hive?

its very simple code so I didn't bother with a license if you want to put credits in there for me I would appreciate it but feel free to use the code however you please :3

default license for things is all rights reserved, unless you explicitly say what we can do with your code we could be breaking laws just by compiling/running it (depending on where the person compiling your code lives) try this site: http://choosealicense.com/
Edited on 08 February 2016 - 04:47 PM
HDeffo #10
Posted 08 February 2016 - 06:58 PM
well i put it in my signature for now :P/> I really feel like I don't need to worry about licensing on something im making in a game so thats just where i leave it.
Lupus590 #11
Posted 08 February 2016 - 08:56 PM
That's very descriptive, but sounds like the MIT license (which is very short so consider reading it)
HDeffo #12
Posted 08 February 2016 - 08:58 PM
Yeah I know what the MIT license is, I just prefer to try and stay out of the bureaucracy as much as possible except on things that I really care if someone stole
HDeffo #13
Posted 11 August 2018 - 03:16 AM
someone recently pointed out this old thing didn't handle coroutines that reached an end well what so ever. so now it does!