Lua中遍历数组并对nil值执行算术操作

我对lua很陌生,但已经开始编写lua脚本。我编写了以下脚本,借助数组打印出我到结构中每个可用对象的距离和角度,但是我遇到了一个大问题:

me = {'["game.exe"+2A]','["game.exe"+A8]','["game"+6A8]'}

botallx = {'["game.exe"+01]','["game.exe"+02]','["game.exe"+03]',
'["game.exe"+04]','["game.exe"+05]','["game.exe"+06]','["game.exe"+07]',
'["game.exe"+08]','["game.exe"+09]','["game.exe"+10]','["game.exe"+11]',
'["game.exe"+12]'}

botally = {'["game.exe"+31]','["game.exe"+32]','["game.exe"+33]',
'["game.exe"+34]','["game.exe"+35]','["game.exe"+36]','["game.exe"+37]',
'["game.exe"+38]','["game.exe"+39]','["game.exe"+30]','["game.exe"+31]',
'["game.exe"+32]'}

botallz = {'["game.exe"+51]','["game.exe"+52]','["game.exe"+53]',
'["game.exe"+54]','["game.exe"+55]','["game.exe"+56]','["game.exe"+57]',
'["game.exe"+58]','["game.exe"+59]','["game.exe"+50]','["game.exe"+51]',
'["game.exe"+52]'}

function sight_angle()
     if (isKeyPressed(VK_E)) then

     for i=1, 12 do
     if (botallx[i] ~= nil or botally[i] ~= nil or botallz[i] ~= nil ) then
     vecX=readFloat(me[1])-readFloat(botallx[i])
     vecY=readFloat(me[2])-readFloat(botally[i])
     vecZ=readFloat(me[3])-readFloat(botallz[i])

     distance=math.sqrt(math.pow(calcZ,2)+math.pow(calcX,2))

     angleX=math.deg(math.atan2(calcZ, calcX))

     print ("当前距离为 " .. distance)
     print ("当前角度为 " .. angleX)
     else --botallx,botally和botallz中的一个值为nil
     print ("由于空值无法执行计算")
     end
     end
end

这些地址称为\ ["game.exe"+...]包含一个浮点值,但是botally,botallx和botallz的容器中的某些对象为nil或具有nil值。因此,当我执行脚本时,从i=1到12的数组开始计算第一个,给出角度和距离,但继续计算直到击中具有nil值的容器中的对象,然后不想继续计算其他对象。

例如:botally,botallx和botallz中的1,3,6个对象具有值,其他所有对象均为空。 它开始计算第一个...输出距离和角度,但是然后停在第二个上并开始抱怨无法执行计算,因为由于nil值,所以跳回第一个对象并再次给出其距离和角度。

但是我希望它跳过第二个空的nil对象并继续进行第三个对象的计算并为我打印出距离和角度,然后跳过空的第四个和第五个对象,并计算第六个对象,然后可以跳回第一个对象,因为7到12的其余内容均为空。

但它不想这样做......有人能帮我吗?

点赞
用户7253993
用户7253993

一般来说,在Lua中,你应该使用 ~= nil来检查值是否为 nil。因此,你可以将你的代码修改为以下代码:

  for i=1, 12 do
    --只有当 botallx、botally 或 botallz 不为 nil 时才执行以下计算
    if (botallx[i] ~= nil or botally[i] ~= nil or botallz[i] ~= nil ) then
       vecX=readFloat(me[1])-readFloat(botallx[i])
       vecY=readFloat(me[2])-readFloat(botally[i])
       vecZ=readFloat(me[3])-readFloat(botallz[i])

       distance=math.sqrt(math.pow(calcZ,2)+math.pow(calcX,2))

       angleX=math.deg(math.atan2(calcZ, calcX))

       print ("current distance is " .. distance)
       print ("current angle is " .. angleX)
    else --其中一个 botallx、botally、botallz 的值为 nil
       print ("Can't perform calculations due to a nil value")
 end

另外,上面的 if 语句也可以像这样写:

if (botallx[i] not nil or botally[i] not nil or botallz[i] not nil )
2018-01-12 23:19:05
用户9769594
用户9769594

我认为你在这里想要使用 and 而不是 or。使用 or,你是在检查它们中至少有一个是否存在,然后无论哪个都会使用。

if (botallx[i] ~= nil and botally[i] ~= nil and botallz[i] ~= nil ) then

或者:

if botallx[i] and botally[i] and botallz[i] then

虽然以你设置的表格方式,这些 botall 地址总是非 nil。

calcXcalcZ 是什么?你是不是想说那里应该是 vecXvecZ如果是这样,那就是你的 nil 问题了。如果不知道,未声明的变量将具有 nil 值,而不是像大多数其他语言中那样导致 "未知符号" 错误。

假设 calcX 问题只是一次复制粘贴错误,你可能需要对那些 readFloat(...) 调用进行一些 nil 检查。

function sight_angle()
    if (isKeyPressed(VK_E)) then
        local meX = readFloat(me[1])
        local meY = readFloat(me[2])
        local meZ = readFloat(me[3])
        if not meX or not meY or not meZ then
            print ("Can't perform calculations due to a nil value")
            return
        end
        for i=1, 12 do
            -- Ditch the botall nil checks
            local bX = readFloat(botallx[i])
            local bY = readFloat(botally[i])
            local bZ = readFloat(botallz[i])

            if bX and bY and bZ then
                local vecX=meX-bX
                local vecY=meY-bY
                local vecZ=meZ-bZ

                -- Assuming calcX and calcZ meant vecX and vecZ
                local distance=math.sqrt(math.pow(vecZ,2)+math.pow(vecX,2))

                local angleX=math.deg(math.atan2(vecZ, vecX))

                print ("current distance is " .. distance)
                print ("current angle is " .. angleX)
            else
                print ("Can't perform calculations due to a nil value")
            end
        end
    end
end

当然,我完全不知道这些东西都是干什么用的!这是 Cheat Engine 插件吗?我只是根据你的代码片段看到的内容进行推断。

2021-07-04 09:07:16