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

[Solved] parallel.waitForAny() using

Started by icehaunter, 11 September 2012 - 07:54 PM
icehaunter #1
Posted 11 September 2012 - 09:54 PM
So. The main idea of what I am trying to achieve is CC/RP2 sorting/crafting machine. Basically, I have got an arm, moving under chest and pulling out itemes. Near my autocrafting table I have got Item Detector setup, so I can move arm to the next chest only after all current itemes have been delivered. I need to simultaneously pull out itemes AND wait for them to come through.
Summing up, I need these two functions to run at the same time, until both finish, only then program can continue. Note - q stands for quantity everywhere

--[[ Other functoins:
		 mwrite - set up to write for monitor via rednet
		 armFw & armBw - moving arm forward and backward
		 pulse - puling redstone on some colored cable
--]]


function pullItem( pos, q)

	readPos()
	--monitor start
	mwrite("2Arm state: waiting")
	mwrite("3Items state: waiting")
	print("See monitor for the process progress")
	--moving arm to position
	while pos ~= currPos do

		mwrite("2Arm state: moving")
		if pos > currPos then
			armFw(pos-currPos)
		elseif pos < currPos then
			armBw(currPos-pos)
		end
	end
	--monitor info
	mwrite("2Arm state: in place")
	mwrite("3Items state: Pulling Item #"..pos)
	--pulling itemes
	for x = 1, q, 1 do
		pulse(colors.white)	  
		os.sleep(0.4)
		write("Pulling "..x.." ")
	end
	writePos()
	mwrite("3Items state: Success!")
	return
end



function itemCheckCraft(q)
	local count = 0
	while count < q do
		os.pullEvent("redstone")
		if colors.test(rs.getBundledInput("bottom"), colors.red) then
		count = count + 1
--		print("Current count on crafting is "..count.." out of "..q)
		end
	end
--	print("Part complete!")
	return
end

and this is couple of tries to make it work:


	parallel.waitForAll(pullItem(1, q*4), itemCheckCraft(q*4))
	parallel.waitForAll(pullItem(2, q), itemCheckCraft(q))  
	parallel.waitForAll(pullItem(3, q), itemCheckCraft(q))  
	parallel.waitForAll(pullItem(4, q*3), itemCheckCraft(q*3))


	parallel.waitForAll(loadstring("pullItem(1, q*4)"), loadstring("itemCheckCraft(q*4)"))
	parallel.waitForAll(loadstring("pullItem(2, q)"), loadstring("itemCheckCraft(q)"))  
	parallel.waitForAll(loadstring("pullItem(3, q)"), loadstring("itemCheckCraft(q)"))  
	parallel.waitForAll(loadstring("pullItem(4, q*3)"), loadstring("itemCheckCraft(q*3)"))
Mad try


	local q1 = 4 * q
	local q2 = 3 * q
	local s1 = "pullItem(1, "..q1..")"
	local s2 = "pullItem(2, "..q..")"
	local s3 = "pullItem(3, "..q..")"
	local s4 = "pullItem(4, "..q2..")"


	parallel.waitForAll(loadstring(s1), loadstring("itemCheckCraft(q1)"))
	parallel.waitForAll(loadstring(s2), loadstring("itemCheckCraft(q)"))  
	parallel.waitForAll(loadstring(s3), loadstring("itemCheckCraft(q)"))  
	parallel.waitForAll(loadstring(s4), loadstring("itemCheckCraft(q2)"))
(I need to use functions with argumens in parallel - this is part of requesting different materials for crafting recipe, and i will eventually have a lot of these, so creating function, which would call needed function with arguments will not work)
So nothing is working. Any ideas how to maintain it? Thanks in advance.
P.S. I would appreciate, if you would also tell me more about using this parallel API. Thanks ^^
Lyqyd #2
Posted 12 September 2012 - 01:12 AM
You can't call the functions as you pass them to the parallel API, you must pass them by reference.
GopherAtl #3
Posted 12 September 2012 - 10:14 AM
Lyqyd is correct; fortunately lua makes that easy


parallel.waitForAll(function() pullItem(q1) end, function() itemCheckCraft(q1) end)
icehaunter #4
Posted 12 September 2012 - 10:18 PM
Oh, thank you very much, GopherAtl!