2012-12-04 41 views
2

我需要幫助我的腳本。 我嘗試了幾乎所有的東西,但我無法弄清楚問題所在。 我想要look.lua檢查是否str = str.."\nIt's "..getPokemonAge(thing.uid).." old." 返回nil,然後忽略它並繼續與腳本。Lua錯誤:字符串預計,得到零

這是我得到的控制檯錯誤:

[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 
+3

聽起來像'getItemAttribute(pokeball,「pokeballinfo」)'返回'nil'。 –

+0

但如何返回零隻是忽略和繼續,如果不retirning零然後vieuw –

+0

但如何返回零隻是忽略並繼續所以當不零然後 返回「」.. getPokemonYears(pokeball)..「年, 「..getPokemonMonths(pokeball)..」months「 當它返回零然後不做 str = str ..」\ n它是「..getPokemonAge(thing.uid)..」old。「 –

回答

1

我想我終於明白了你的問題,所以我會重新說明我是如何理解它的,你可以告訴你這是否是你想要的。

據我所知,您知道您的功能getPokemonAge有時會導致錯誤。其他人指出,這個錯誤是從getItemAttribute(pokeball, "pokeballinfo")返回nil

現在我想你想讓程序在產生文本時返回文本,但忽略可能發生的任何錯誤並在出錯時返回nil

這可以用pcall完成(look here)。

在我的部分重寫你的getPokemonAge函數我呼籲getPokemonAgeInternal(這是你的原始函數)與pcall。然後我只是檢查結果,並返回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函數。

如果你的錯誤總是來自getItemAttribute(pokeball, "pokeballinfo")nil你不應該使用pcall,而只是檢查該條件並返回nil,如果getItemAttribute(pokeball, "pokeballinfo") == nil

相關問題