简体   繁体   English

Lua错误:预期为字符串,为零

[英]Lua error: string expected, got nil

I need help with my scripts. 我的脚本需要帮助。 I tried nearly everything, but I can't figure it out what the problem is. 我几乎尝试了所有方法,但无法弄清楚问题出在哪里。 I want look.lua to check if str = str.."\\nIt's "..getPokemonAge(thing.uid).." old." 我要look.lua检查str = str.."\\nIt's "..getPokemonAge(thing.uid).." old." Returns with nil, then ignores it and goes on with the script. 返回nil,然后将其忽略并继续执行脚本。

This is the error I get on console: 这是我在控制台上得到的错误:

[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 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 look.lua

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

age 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

I think I finally understand your question, so I will rephrase how I understand it and you can tell whether that was what you wanted. 我想我终于理解了您的问题,因此我将重新说明一下我的理解方式,您可以说出这是否是您想要的。

As I understand it you know that your function getPokemonAge sometimes causes an error. 据我了解,您知道您的函数getPokemonAge有时会导致错误。 Several others pointed out that this error is from getItemAttribute(pokeball, "pokeballinfo") returning nil . 其他几个指出此错误是由于getItemAttribute(pokeball, "pokeballinfo")返回nil

Now I think you want the program to return the text if a text was produced, but to ignore any error that might occur and return nil in case of error. 现在,我认为您希望程序在生成文本时返回文本,但忽略任何可能发生的错误,并在出现错误的情况下返回nil

This can be done with pcall ( look here ). 这可以通过pcall完成( 请看此处 )。

In my partial rewrite of your getPokemonAge-Function I call getPokemonAgeInternal (which is your orginal function) with pcall. 在我对getPokemonAge-Function的部分重写中,我用pcall调用了getPokemonAgeInternal (这是您的原始函数)。 Then I just check the result and return nil on error. 然后,我只检查结果并在错误时返回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

You can apply similar code to your getPokemonYears -Function instead if you want to protect that against errors. 如果要防止错误,可以将类似的代码应用于getPokemonYears

If your error always comes from getItemAttribute(pokeball, "pokeballinfo") being nil you should not use pcall, but instead just check that condition and return nil if getItemAttribute(pokeball, "pokeballinfo") == nil . 如果您的错误始终来自于getItemAttribute(pokeball, "pokeballinfo")nil您不应使用pcall,而只是检查该条件,如果getItemAttribute(pokeball, "pokeballinfo") == nil则返回getItemAttribute(pokeball, "pokeballinfo") == nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM