Lua 错误:字符串预期,但却得到了 nil。
2012-12-4 21:13:33
收藏:0
阅读:292
评论:1
我需要帮助我的脚本。
我尝试了几乎所有方法,但我无法弄清楚问题出在哪里。
我想让 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."
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
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
我想我最终理解了你的问题,所以我会重述我理解的方式,你可以告诉我这是否是你想要的。
据我了解,你知道你的函数
getPokemonAge有时会导致错误。几个人指出,这个错误是由getItemAttribute(pokeball,“pokeballinfo”)返回nil引起的。现在我认为你希望程序在产生文本时返回文本,但忽略任何可能发生的错误,并在出错的情况下返回
nil。这可以使用pcall(请看这里)完成。
在我部分重写的
getPokemonAge函数中,我用pcall调用getPokemonAgeInternal(这是您的原始函数)。然后我只需检查结果并在错误时返回nil。function getPokemonAgeInternal(pokeball) return“”..getPokemonYears(pokeball)..”年,”..getPokemonMonths(pokeball)..”月” 结束 function getPokenmonAge(pokeball) success,value = pcall(getPokemonAgeInternal,pokeball) 如果(成功) 然后 返回值 其他 返回nil 结束 结束如果你想保护你的
getPokemonYears函数免受错误的影响,你可以将类似的代码应用于那里。如果你的错误总是来自于
getItemAttribute(pokeball,“pokeballinfo”)== nil,那么你不应该使用pcall,而是检查那个条件,如果getItemAttribute(pokeball,“pokeballinfo”)== nil,则返回nil。