如何解决表既为零又不为零
出于某种原因,当我只访问我的主表时,这很好,但是当我尝试使用变量访问它时,不知何故nil
?
这是给出错误的函数:
function parseMtl(mtlFilePath,mtlTbl) -- Parses the mtl file at mtlFIlePath
local step1 = {} -- Table to store the split lines
local mtlID = 0 -- The ID of the material
local mtlList = {} -- The list of materials
local faceColors = {} -- The returned colors of the polygons
local fC -- A test variable
contents,size = love.filesystem.read(mtlFilePath,all) -- Read the .mtl file at mtlFilePath
step1 = split(contents,"\n") -- Split the contents into step1 by the newline character
for i=1,#step1 do
if (starts(step1[i],"newmtl")) -- Create a new material
then
mtlID = mtlID + 1
mtlName = split(step1[i],"%s")[2]
table.insert(mtlList,{mtlName,mtlID})
end
if (starts(step1[i],"Kd")) -- If it's a color value,put value into the list of materials
then
R = split(step1[i],"%s")[2] * 255
G = split(step1[i],"%s")[3] * 255
B = split(step1[i],"%s")[4] * 255
table.insert(mtlList[mtlID],{R,G,B})
end
for i=1,#mtlTbl do -- Convert the mtlTbl values into 'Vertex ID,Color' format
fC = mtlList[mtlTbl[i][2]]
table.insert(faceColors,{i,fC})
end
end
return faceColors
end
做奇怪事情的部分是 table.insert(faceColors,fC})
,当我将 nil
放入表格时,它以某种方式返回 fC
,当我直接打印 fC
值时,它不是nil
。我不知道它为什么要这样做..
解决方法
您是否尝试使用 table.insert
的返回值?
table.insert
是无效的,所以它总是返回 nil。
您如何访问颜色数据?
您设置数据的方式,您可以通过以下方式访问它:
local faceColors = parseMtl(...)
for i = 1,#faceColors do
print(faceColors[i][2])
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。