gpt4 book ai didi

lua - 尝试在脚本中索引一个 nil 值

转载 作者:行者123 更新时间:2023-12-04 15:05:30 25 4
gpt4 key购买 nike

我正在尝试编写一个 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 时,为什么会出现此错误?

最佳答案

至少在您的测试代码中,您有一个错字:

line1data ~= line1Data

你在 ioWriter.lua 的 close 方法中也有一个错字:
function ioWriter:close(self)

应该
function ioWriter:close()

关于lua - 尝试在脚本中索引一个 nil 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1268085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com