You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CC:Tweaked海龟Lua代码卡循环:未执行取物逻辑求助

CC:Tweaked 海龟自动化合成取物死循环问题排查与修复

核心问题定位

你的程序陷入等待物品的while死循环、未执行取物逻辑,本质是两个关键问题:

  • getDirection()函数未正确执行或返回有效方向,导致直接进入else分支
  • else分支未正确修改循环退出变量,或缺少重试/修正逻辑,无法跳出循环

具体排查与修复步骤

1. 检查getDirection()的调用与返回逻辑

首先要确认该函数能正确解析你输入的箱子方向:

  • 在调用getDirection()后立即添加日志,验证返回值:
    local dir = getDirection(matLoc)
    print("[DEBUG] Slot "..slot1.." direction received: "..tostring(dir)) -- 新增日志
    
  • 检查MaterialLocation[slot1]的赋值是否正确,确保用户输入的方向字符串被正确存入该变量
  • 若函数存在大小写匹配问题,统一转为小写处理:
    function getDirection(input)
        local validDirs = {north=true, south=true, east=true, west=true, up=true, down=true}
        input = string.lower(input or "") -- 处理空输入
        return validDirs[input] and input or nil
    end
    

2. 修复while循环的退出逻辑

你提到else分支未修改循环变量导致死循环,需针对性调整:

  • 如果希望用户修正方向后重试:
    else
        print("Invalid direction for slot "..slot1..", enter correct direction (north/south/east/west/up/down):")
        local newDir = read() -- 获取用户新输入
        MaterialLocation[slot1] = newDir -- 更新存储的方向
        -- 无需修改hasItem,循环会重新尝试获取方向与物品
    end
    
  • 如果需超时退出(避免无限等待):
    local waitCount = 0
    while not hasItem and waitCount < 10 do -- 最多等待10次
        -- ... 原有逻辑 ...
        else
            print("Waiting for input in slot "..slot1.." ("..(10-waitCount).." tries left)")
            waitCount = waitCount + 1
            sleep(1)
        end
    end
    if waitCount >=10 then
        print("Timeout: Failed to get item for slot "..slot1)
        return false -- 终止函数,避免死循环
    end
    

3. 验证取物与槽位操作逻辑

确保海龟能正确从箱子取物并放入目标槽位:

  • 单独测试取物逻辑:选中空槽位,转向箱子方向,调用turtle.suck(),检查是否能取到物品
  • 确认物品转移逻辑顺序正确:取物后需先选中目标槽位,再执行转移:
    if success then
        turtle.select(slot1) -- 选中目标槽位
        turtle.transferTo(slot1, 1) -- 转移1个物品到该槽位
        hasItem = true
    end
    

内容的提问来源于stack exchange,提问作者Unnamed Protagonist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 13:55:19