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

parallel.waitForAll() not running

Started by margeobur, 12 May 2013 - 09:51 PM
margeobur #1
Posted 12 May 2013 - 11:51 PM
Hi, i'm new to Lua and CC and I have a sort of ambitious first program I'm trying to make for a mining turtle. It will hopefully be able to make a full mine automatically while taking commands from a computer. So far I've made a function to dig a shaft and done a few commands.

My problem is that when I try to dig a shaft and receive commands at the same time using this:

parallel.waitForAll(DigShaft, receive)

it does nothing. If I include the parentheses in the arguments (are you supposed to?), it gives the error: "attempt to call nil"


here is my full program so far (forgive any strange inefficiencies that usually come from a novice programmer like me):
Spoiler

--[[ Title: Advanced Wireless Mining Turtle (TurtleSide)

Description: Mines a shaft and places ladders.
Excavates a 3 wide and 16 high passage for mining.
It knows the pattern to expose all blocks in a chunk.
and is able to make a walk way for accessing each tunnel.
It will hopefully do this while taking commands from
a computer with a wireless modem.

Author: margeobur (some code is based on default programs
for things like digging the mineshaft) ]]

local tArgs = { ... }
if #tArgs ~= 1 then
print("Usage: TurtleMining <Monitor Computer's ID> ")
return
end

local monitorID = tonumber( tArgs[1] )
if monitorID < 0 then
print("Error: Cannot have negative ID")
end


-- ***********************Functions**********************************

--*******************Monitor Communication*****************

local function receive()
while true do
local sender, message, distance = rednet.receive()
if sender ~= monitorID then
rednet.send(sender, "You are not my monitor!")

end

if message == "CheckStatus" then
local Status = getStatus()
local sStatus = textutils.serialze(Status)
rednet.send(monitorID, sStatus)

elseif message == "StopMining" then
command = 2
break

else rednet.send(monitorID, "Unknown command")

end
return
end

local function getStatus()
local x, y, z = gps.locate(4)
if not x then
x, y, z = "Could not locate", "Could not locate", "Could not locate"
end
local fuel = turtle.getFuelLevel()
local inv_room = 0
for i = 4, 9 do
inv_room = inv_room + turtle.getItemSpace(i)
end
local tStatus = readonlytable { fuel, blocks_mined, stage, inv_room, x, y, z }

return tStatus
end


--*********Digging Mineshaft/Making Mine Room*************


local function collect()
blocks_mined = blocks_mined + 1

for n=1,9 do
if turtle.getItemCount(n) == 0 then
return true
end
end

print( "No empty slots left." )
return false
end


local function tryForwards()
while not turtle.forward() do
if turtle.dig() then
if not collect() then
return false
end
   else
-- give sand a chance to fall
  sleep(0.8)
	if turtle.dig() then
	if not collect() then
return false
end
	else
	return false
   end
   end
end
xPos = xPos + xDir
zPos = zPos + zDir
return true
end


local function tryDown()
if not turtle.down() then
if turtle.digDown() then
if not collect() then
return false
end
   end
if not turtle.down() then
return false
end
end
depth = depth - 1
return true
end


local function turnLeft()
turtle.turnLeft()
xDir, zDir = -zDir, xDir
end

local function turnRight()
turtle.turnRight()
xDir, zDir = zDir, -xDir
end

local function placeLadders()
print("place de laddars")
end

local function DigShaft()
stop = false
depth = 0
blocks_mined = 0

xPos,zPos = 0,0
xDir,zDir = 0,1
alternate = 0

stage = "Digging Shaft"

print("Digging de shaft...")

while not stop do
for n = 1,3 do
for m = 1,2 do
if not tryForwards() then
stop = true
break
end
end
if stop then
break
end
if n < 3 then
if math.fmod(n + alternate,2) == 0 then
turnLeft()
if not tryForwards() then
stop = true
break
end
turnLeft()
else
turnRight()
if not tryForwards() then
stop = true
break
end
turnRight()
end
end
end

if stop then
break
end


if alternate == 0 then
turnLeft()
else
turnRight()
end

alternate = 1 - alternate

if not tryDown() then
stop = true
break
end

if command == 2 then
print("Master has told us to stop. Stopping...")
stop = true
break
end

end
end
return
end


--*********************Driver Program:*******************************


print("Welcome to margeobur's Advanced Turtle ")
print("mining program (turtleside interface)  ")
print()

rednet.open("right")
command = 0

parallel.waitForAll (DigShaft, receive)


O_o it wouldn't let me have tabs for the different levels for some reason, sorry about that.
Edited by
Lyqyd #2
Posted 13 May 2013 - 02:55 PM
Split into new topic.
W00dyR #3
Posted 13 May 2013 - 03:33 PM
Some tips:

- Functions must be ended with an "end"
- Same goes for any kind of loop (while, if, for), all need an "end"
- Declare functions before calling them (not sure if thats required, but I think it is)

Post if you need more help :)/>
Smiley43210 #4
Posted 13 May 2013 - 05:32 PM
He did end the functions and statements. And yes, functions must be declared before you call them.

I'll take a look.

What I've done so far:
Indented program: http://pastebin.com/ApLibLBN

I think it works now, haven't tested it.
You had put one too few ends in the beginning (to close function receive) and one too many at the end, so all the other functions were 'inside' receive(). Just had to move one 'end'.
margeobur #5
Posted 13 May 2013 - 09:28 PM
Well, thank you for your posts. Thanks for fixing that Smiley, and I did notice that I need to declare and define my functions before calling them so thanks W00dyR. It did work better (it displayed the "Digging de shaft…" message but didn't fully run) but I think I need to read up about coroutines to get the program to work the way I want it too.

Once again thanks for your help. I'll probably post this when I'm done so I hope I can finish soon. :)/>
D3matt #6
Posted 14 May 2013 - 12:19 AM
Also, you want to use waitForAny() not waitForAll. Wait for all will wait for both a rednet message AND the digging. Wait for any will just wait for one of the two.
margeobur #7
Posted 15 May 2013 - 04:59 AM
Edit: sorry, delete this post. I need to ask another question at a later time. (Made the post but it did something weird. Then I ran out of time)