尝试索引脚本中的nil值

时间:2009-08-12 19:03:05

标签: lua

我正在尝试编写一个lua脚本来帮助修改游戏,并且它会在我的一个助手库的某个特定行上打破。

odfWriter.lua:

require 'loopsetup'
require 'ioWriter'
local open = {}
odfWriter = class{
    writer = false
}
odfWriter[open] = false

function odfWriter:open(name)
    if not self[open] then
        self.writer = ioWriter()
        self.writer:open(name)
        self[open] = true
    else
        error("tried to open an already open writer")
    end
end

function odfWriter:write(args)
    self.writer:write(args.Key .. " = ") --<-- error is here, when trying to access args
    if args.Type == "seqstrings" then
        for k,v in pairs(args.Value) do
            self.writer:write("\"" .. v .. "\" ")
        end
    elseif args.Type == "string" then
        self.writer:write("\"" .. args.Value .. "\"")
    elseif args.Type == "seqnumbers" then
        for k,v in pairs(args.Value) do
            self.writer:write(tostring(v) .. " ")
        end
    elseif args.Type == "number" then
        self.writer:write(tostring(args.Value))
    elseif args.Type == "boolean" then
        if args.Value == true then
            self.writer:write("1")
        elseif args.Value == false then
            self.writer:write("0")
        end
    end
    self.writer:write("\n")
end
function odfWriter:close()
    if self[open] then
        self.writer:close()
        self.writer = false
        self[open] = false
    else
        error("tried to close an already closed writer")
    end
end

loopSetup.lua

-----------------------------------------------------------------------
--  file        : loopsetup.lua
--  description : provides global access to all of the (known) members
--                of the loop.simple code (for easier access)
-----------------------------------------------------------------------
require 'loop.simple'
class = loop.simple.class
classof = loop.simple.classof
initclass = loop.simple.initclass
instanceof = loop.simple.instanceof
isclass = loop.simple.isclass
memberof = loop.simple.memberof
members = loop.simple.members
new = loop.simple.new
rawnew = loop.simple.rawnew
subclassof = loop.simple.subclassof
superclass = loop.simple.superclass

ioWriter.lua:

local loaded = require('loopsetup')
assert(loaded, 'loopsetup not loaded')
local open = {}
ioWriter = class{
    stream = false
}
ioWriter[open] = false
function ioWriter:open(name)
    if not self[open] then
        self.stream = io.open(name, "w")
        self[open] = true
    else
        error("attempted to open an already open writer")
    end
end
function ioWriter:write(str)
    self.stream:write(str)
end
function ioWriter:writeLine(str)
    self.stream:write(str .. '\n')
end
function ioWriter:close(self)
    if self[open] then
        self.stream:flush()
        self.stream:close()
        self.stream = false
        self[open] = false
    else
        error("attempted to close an already closed writer")
    end
end

测试代码:

require 'loopsetup'
require 'odfWriter'
local odf = odfWriter()
odf:open('test.odf')
local line1Data = {
    Type = "seqstrings",
    Key = "engineTargetHardpoints",
    Value = {"hp01", "hp02", "hp03"}
}
odf:write(line1data)
odf:close()

为什么我在将有效表传递给odfwriter.write时会出现此错误?

2 个答案:

答案 0 :(得分:5)

至少在你的测试代码中,你有一个拼写错误:

line1data ~= line1Data

在close方法中,你在ioWriter.lua中也有一个拼写错误:

function ioWriter:close(self)

应该是

function ioWriter:close()

答案 1 :(得分:1)

您尚未明确检查实施odf:open()的所有内容是否成功。我担心的是,odf:open()中的整个流控制看起来似乎都假设一切都成功了。是否可能没有,因此在指示的行中,错误是由于尝试索引self.writer的{​​{1}}而引起的?

如果nil没有成功执行构造函数nil,则false可能是odfWriter:open()而不是self.writer = ioWriter()。我不是循环的常规用户,所以我可能会咆哮错误的树,但是......

如果发生了这种情况,很容易得到一条消息,该消息对哪个索引有问题感到困惑。

或许在几个选择点放弃对assert()的调用会很有效。