0

I've tried to make a control panel for my mining turtle, so I got into using Lua coroutines, because I needed the turtle to listen even if it's currently mining, but every time I call function round() it won't continue. I tried to place print('test') in between every near lines of code and I found that the round() function works untill the turtle.turnLeft() part.

The turtle has following code:

rednet.open('right')
local status = 'None'

io.output(io.stdout)
io.input(io.stdin)

io.write('Control Panel ID: ')
local baseId = tonumber(io.read())
io.write('Blocks: ')
local blcks = tonumber(io.read())

function round()
    turtle.dig()
    --- Here the function stops ---
    turtle.turnLeft()
    turtle.dig()
    turtle.forward()
    turtle.turnRight()
    turtle.dig()
    turtle.digDown()
    turtle.down()
    turtle.dig()
    turtle.turnRight()
    turtle.dig()
    turtle.forward()
    turtle.turnLeft()
    turtle.dig()
    sleep(0.2)
end

function listen()
    if true then
        id, message = rednet.receive()
        if message == 'down' then
            print('Down!')
            coroutine.resume(downCo, blcks)
        elseif message == 'up' then
            coroutine.resume(upCo, blcks)
        elseif message == 'refuel' then
            turtle.refuel()
        elseif message == 'status' then
            rednet.send(baseId, status)
        elseif message == 'stop' then
            print('Lol')
        elseif message == 'fuel' then
            rednet.send(baseId, turtle.getFuelLevel())
        end
    end
end

downCo = coroutine.create(function(blcks)
    local a = 0
    print('Going down! '..blcks)
    status = 'Going down'
    while a <= blcks do
        a = a + 1
        round()
    end
    local status = 'Done'
end)

upCo = coroutine.create(function(blcks)
    local status = 'Going up'
    for i = blcks,1,-1 do
        turtle.digUp()
        turtle.up()
    end
    local status = 'Done'
end)

while true do
    listen()
end

I couldn't find any information about that the coroutines can't support turtle movement or anything like that

I tried to turn it into coroutine too, but it just happened to stuck at the same point.

  • Why do you use coroutines here? Since you never yield the coroutine it basically behaves the same as a function. `co.resume` returns false, followed by an error message in case of error, this may be helpful. – Luke100000 Dec 03 '22 at 20:18
  • I use coroutunes because i need the turtle to listen and mine at the same time, I didn’t paste the base code here, but when it detects monitor touch it sends the command to the turtle, and then, while the turtle is running, the base sends requests to the turtle to get fuel level and status. But the main problem is the stop command, whenever I need to stop the turtle it’s probably running, this is why coroutines. Simply, the turtle has to do some multitasking. – MDdiamond Dec 04 '22 at 09:41
  • I see. Just remember that coroutines are no threads, they do NOT run in parallel. Did the return value of resume provide you with any news? – Luke100000 Dec 04 '22 at 10:41
  • The return value from coroutine.resume() was: true turtle_response. Someone suggested to use coroutine.yield() in the round() func, but it didn't work and I can't see the answer now, so i guess the he deleted it. – MDdiamond Dec 06 '22 at 23:56
  • `coroutine.yield()` pauses the coroutine to give time to your networking. But "turtle_response" gave one important hint, it seems like the turtle calls pause your coroutine already. Check out https://computercraft.info/wiki/Coroutine_(API) on the very bottom. That means everything is working fine and you just need to continue the coroutine, if I understood that correctly. – Luke100000 Dec 07 '22 at 07:57
  • If I understand that correctly, we can't use built-in functions that take some time to give response in coroutines, so basically have I to find a way to get instant response from turtle.turnLeft? It seems close to impossible to me – MDdiamond Dec 09 '22 at 22:34
  • Hmm, that's correct, but why would you need instant response? Since you cant abort a turtle action there is no need to react before that action has been completed. – Luke100000 Dec 10 '22 at 08:27

0 Answers0