【问题标题】:Lua error: string expected, got nilLua 错误:需要字符串,结果为零
【发布时间】:2012-11-22 13:17:23
【问题描述】:

我的脚本需要帮助。 我几乎尝试了所有方法,但我无法弄清楚问题所在。 我想look.lua 检查str = str.."\nIt's "..getPokemonAge(thing.uid).." old." 返回 nil,然后忽略它并继续执行脚本。

这是我在控制台上遇到的错误:

[04/12/2012 20:43:42] [Error - CreatureScript Interface] 
[04/12/2012 20:43:42] data/creaturescripts/scripts/look.lua:onLook
[04/12/2012 20:43:42] Description: 
[04/12/2012 20:43:42] data/lib/011-string.lua:16: bad argument #1 to 'find' (string expected, got nil)
[04/12/2012 20:43:42] stack traceback:
[04/12/2012 20:43:42]   [C]: in function 'find'
[04/12/2012 20:43:42]   data/lib/011-string.lua:16: in function '(for generator)'
[04/12/2012 20:43:42]   data/lib/011-string.lua:16: in function 'explode'
[04/12/2012 20:43:42]   data/lib/age system.lua:2: in function 'getPokemonYears'
[04/12/2012 20:43:42]   data/lib/age system.lua:42: in function 'getPokemonAge'
[04/12/2012 20:43:42]   data/creaturescripts/scripts/look.lua:32: in function <data/creaturescripts/scripts/look.lua:1>

011-string.lua

local i, pos, tmp, t = 0, 1, "", {}
        for s, e in function() return string.find(str, sep, pos) end do
            tmp = str:sub(pos, s - 1):trim()
            table.insert(t, tmp)
            pos = e + 1

            i = i + 1

        end

look.lua

str = str.."\nIt's "..getPokemonAge(thing.uid).." old."

年龄system.lua

function getPokemonYears(pokeball)
local data = string.explode(getItemAttribute(pokeball, "pokeballinfo"), "/")
-- data[1] = dia, data[2] = mes, data[3] = ano
local yearnow = math.floor(tonumber(os.date("%Y")))
local monthnow = math.floor(tonumber(os.date("%m")))
local daynow = math.floor(tonumber(os.date("%d")))
local ano = math.floor(tonumber(data[3]))
local mes = math.floor(tonumber(data[2]))
local dia = math.floor(tonumber(data[1]))
local years = 0
if yearnow == ano then years = monthnow-mes end
if yearnow > ano then years = (12-mes) + monthnow end
return years
end

function getPokemonMonths(pokeball)
local data = string.explode(getItemAttribute(pokeball, "pokeballinfo"), "/")
local yearnow = math.floor(tonumber(os.date("%Y")))
local monthnow = math.floor(tonumber(os.date("%m")))
local daynow = math.floor(tonumber(os.date("%d")))
local ano = math.floor(tonumber(data[3]))
local mes = math.floor(tonumber(data[2]))
local dia = math.floor(tonumber(data[1]))

if (yearnow == ano) and (monthnow==mes) and (daynow<dia+2.5) then months = 0 end
if (yearnow == ano) and (monthnow==mes) and (daynow>dia+2.5) then months = (daynow-dia)/2.5 end
if (yearnow == ano) and (monthnow>mes) then months = math.floor((30-dia)/2.5) + daynow/2.5 end
if (yearnow > ano) then
days = math.floor(monthnow*30+daynow)
months = math.floor(days/2.5)
end
if tostring(months):len() > 3 then months2 = tonumber(string.sub(tostring(months), 1, 3))
else months2 = months end
return months
end


function getPokemonAge(pokeball)
return ""..getPokemonYears(pokeball).." year, "..getPokemonMonths(pokeball).." months"
end

【问题讨论】:

  • 听起来getItemAttribute(pokeball, "pokeballinfo") 正在返回nil
  • 但是如果返回 nil 则如何忽略并继续如果不退出 nil 然后查看
  • 但是如果返回 nil 则如何忽略并继续,所以当不是 nil 时返回 ""..getPokemonYears(pokeball).." year, "..getPokemonMonths(pokeball).." months"并且当它返回 nil 时不做 str = str.."\n它是 "..getPokemonAge(thing.uid).." old."
  • 代码太多了,把无用的代码解析出来,你会得到更多的答案。
  • 您对创建的各种文件有何要求?

标签: function console lua null


【解决方案1】:

我想我终于明白了你的问题,所以我将重新表述我的理解方式,你可以判断这是否是你想要的。

据我了解,您知道您的函数getPokemonAge 有时会导致错误。其他几位指出此错误来自getItemAttribute(pokeball, "pokeballinfo") 返回nil

现在我认为您希望程序在生成文本时返回文本,但忽略可能发生的任何错误并在出现错误时返回 nil

这可以通过 pcall (look here) 来完成。

在我对您的 getPokemonAge-Function 的部分重写中,我使用 pcall 调用 getPokemonAgeInternal(这是您的原始函数)。然后我只检查结果并在错误时返回nil

function getPokemonAgeInternal(pokeball)

    return ""..getPokemonYears(pokeball).." year, "..getPokemonMonths(pokeball).." months"
end

function getPokenmonAge(pokeball)
    success, value = pcall( getPokemonAgeInternal, pokeball )
    if ( success )
    then
        return value
    else
        return nil
    end
end

如果您想防止错误发生,您可以将类似的代码应用于您的 getPokemonYears-Function。

如果您的错误总是来自getItemAttribute(pokeball, "pokeballinfo")nil,则不应使用 pcall,而只需检查该条件并在 getItemAttribute(pokeball, "pokeballinfo") == nil 时返回 nil。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 2014-01-21
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多