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

LUA Question -- Task Queue

Started by Saiyt, 27 December 2012 - 08:47 PM
Saiyt #1
Posted 27 December 2012 - 09:47 PM
I am attempting to create a central computer that controls several function in my base. Unfortunately several of the functions require loops that last for x seconds, which holds the computer for that time. Is there a way to process the input simultaneously? The only solution I've come up with so far is having one computer monitoring input, and then farming the output tasks to other computers that would process the output functions.

Example:
Player presses Button 1,
Button 1 sends signal to Computer 1
Computer 1 activates function Button1
Button1 loops for 10 seconds
Player presses Button 2
Button1 returns
Player is angry because Button 2 doesn't do anything

Ideally I would like 1 computer to send the outputs simultaneously, but a task queue would work as well.
tesla1889 #2
Posted 27 December 2012 - 10:09 PM
you could use string.dump on functions to get the bytecode and send it to other computers via rednet message. just set up the other computers to get the function from the bytecode via loadstring and execute it

returning a value to the central computer can be a pain though, especially when there are possible simultaneous responses
ChunLing #3
Posted 27 December 2012 - 10:23 PM
Remote control works fine, parallel api also is an option depending on the structure of the other tasks. An os.pullEvent loop is the heart of getting your remote control to work reliably, and it might also allow you to multitask with just the one computer, depending.
Saiyt #4
Posted 27 December 2012 - 10:45 PM
This is an example of the program, I realize I probably missed an end, I'm just providing it to give an example of how I've structured it.. the problem comes when I have a frame motor set that needs to pulse about 25 times. It'll take quite a bit of time to complete and I'm worried that a door won't open appropriately if someone is elsewhere in the castle trying to activate a door.

local doorState = 0
local function mainDoor()
  if(doorState==0) then --check whether door is up or down
	for n=1, 8 do -- redstone pulse for frame motors to open door
	  rs.setBundledOutput("right", colors.yellow)
	  sleep(0.5)
	  rs.setBundledOutput("right", 0)
	  sleep(0.3)
	end
	doorState = 1
   else
	 for n=1, 8 do -- pulse for closing door
		  rs.setBundledOutput("right", colors.blue
		  sleep(0.5)
		  rs.setBundledOutput("right", 0)
		  sleep(0.3)
	  end
	 doorState = 0 --- set doorState
   end
while true do -- monitor for a redstone input
  doorMonitor = {os.pullEvent()}
  if(doorMonitor[1]=="redstone") then
	 inputColor = rs.getBundledInput("left")
	 if(inputColor==colors.yellow) then
		mainDoor()
	 end
   end
sleep(.1)
end
edit: I would also appreciate some clarification on how string.dump might work in the way you are suggesting, I'm not familiar with it.
ChunLing #5
Posted 27 December 2012 - 11:54 PM
I doubt that is necessary for this task.

I think that you should be able to handle this by simplifying your function into a single pulse, then using timers to send multiple pulses while also being able to get other inputs with an os.pullEvent loop.

Parallel should also work fine with this, and some prefer that solution.