我试着为我的采矿海龟做一个控制面板,所以我开始使用Lua协同器,因为我需要海龟听,即使它目前正在挖掘,但是每次我调用函数call (),它都不会继续。我试图将print('test')放在接近的代码行之间,我发现圆形()函数直到turtle.turnLeft()部分才能工作。
海龟有以下守则:
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
我找不到任何关于合作伙伴不能支持海龟运动之类的信息
我也试着把它转化成协同线,但它恰好卡在了同一点上。
发布于 2022-12-04 12:31:04
看起来你试图在你的海龟的代码中使用协同线,但是你没有正确地使用它们。Coroutines是一种允许代码暂停和恢复的方法,但它们不会像线程那样自动并行运行。
在您的代码中,您调用coroutine.resume来启动coroutine,但是在任何时候都不会将控制返回到主线程。这意味着您的协同机制将启动,然后立即停止,因为它们尚未将控制权返回到主线程。
要解决这个问题,您需要在圆形函数中添加一个coroutine.yield调用。这将允许协同线暂停并将控制权返回到主线程,以便其他协同线可以运行。下面是您的代码在进行此更改时的外观:
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()
coroutine.yield() -- Add this line to yield control back to the main thread
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
这应该允许您的协同运行并行运行,并允许您的乌龟移动时,它正在监听信息。
https://stackoverflow.com/questions/74663014
复制相似问题