wolfwar 发表于 2022-12-18 04:48:24

lua 小白问题

请教各位大神, 我想在mudlet里做个挑水机器人, 因为要慢慢走, 就想用 tempTimer 每次等一秒再走。
所有步骤在一个数组里, 然后一个个给慢走函数。

在manzou函数里, 如果执行print, 所有步骤都可以正常显示。
但tempTimer(1, [[ send(v1) ]]) 显示下面错误:
      <Lua error:sendRaw: bad argument #1 type (command as string expected, got nil!)>
[错误:] 对象:<error in Lua code> 函数:<no debug data available>

这是怎么回事 ?
=========================================================
function goTS1()
local v_array = {"n","n","w","n","n","w","carry mu tong","e","s","s","e","s","s","sd","s","open gate","s","s","sd","sd","sd","wd","w","sd","sd","eu","fill mu tong"}
for _, v1 in ipairs(v_array) do
   manzou(v1)
   --print(v1)
end
end


function manzou(v1)
   
    print("xxx" ..v1)
    tempTimer(1, [[ send(v1) ]])
end

wolfwar 发表于 2022-12-18 07:49:24



改了一下, 这样不出错, 但是20 个'x' 同时出现。

function goTS1()
local v_array = {"n","n","w","n","n","w","carry mu tong","e","s","s","e","s","s","sd","s","open gate","s","s","sd","sd","sd","wd","w","sd","sd","eu","fill mu tong"}
v_num = 0
for _, v1 in ipairs(v_array) do
   manzou()
end
end

function manzou()   
    tempTimer(1, [[ print("x") ]])
end

dtp 发表于 2022-12-18 07:51:31

manzou(n)

tempTimer(1 * n, print)

wolfwar 发表于 2022-12-18 07:52:08

读了一些论坛,看来lua不允许这样的操作
动作 1
等一秒
动作2
等一秒

怎么做头一个最简单的机器人就遇到这么倒霉的问题{:7_282:}

wolfwar 发表于 2022-12-18 08:59:45

终于找到解决办法了,有点笨,一次发n个tempTimer,哪位大神有更好的办法请告知。

function goTS1()
local v_array = {"n","n","w","n","n","w","carry mu tong","e","s","s","e","s","s","sd","s","open gate","s","s","sd","sd","sd","wd","w","sd","sd","eu","fill mu tong"}
t1 = 0
for _, v1 in ipairs(v_array) do
   t1 = t1 +1
   manzou(t1,v1)
end
end

function manzou(t1,v1)   
    tempTimer(t1, [[ send(" ]]..v1..[[") ]])
end

creat 发表于 2022-12-18 09:07:26

这样设计
1 定义变量
v_array = {"n","n","w","n","n","w","carry mu tong","e","s","s","e","s","s","sd","s","open gate","s","s","sd","sd","sd","wd","w","sd","sd","eu","fill mu tong"}
v_num = 1
v_chk = true
v_fuc = function ()
if v_chk then
print (v_array)
v_num=v_num+1
end
end

2 创建一个计时器,计时器负责发送v_fuc()

3改变控制变量v_chk的布尔值可以停止发送命令

wolfwar 发表于 2022-12-18 16:12:57

@dtp
多谢回复!这样可以,但也是创建了n个timer, 优化不好而且觉得别扭。

@creat
感觉这是最好的方法,还没学到mudlet里面的timer功能,太感谢了!
页: [1]
查看完整版本: lua 小白问题