尝试索引字段? (零值)
我正在使用 Lua/love2d 编写一个小型 RPG 游戏引擎,我需要将文件解析为二维数组,但它不起作用,并且我收到错误...
main.lua :
local fmap = love.filesystem.read("map.txt")
map = {}
for c in fmap:gmatch(".") do
if c == "\n" then
y = 0
x = x + 1
else
map[x][y] = c -- this won't work
y = y + 1
end
end
map.txt :
6777633333
6558633333
6555614133
7757711112
2111111112
2111111112
2222222222
I am writing a small RPG game engine with Lua/love2d, and I need to parse a file to a 2d array, but it don't work, and I am getting errors...
main.lua :
local fmap = love.filesystem.read("map.txt")
map = {}
for c in fmap:gmatch(".") do
if c == "\n" then
y = 0
x = x + 1
else
map[x][y] = c -- this won't work
y = y + 1
end
end
map.txt :
6777633333
6558633333
6555614133
7757711112
2111111112
2111111112
2222222222
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能像这样使用多维数组。请参阅矩阵和多维数组
您可以像这样转换代码:
You can't use multi-dimensional array like this. See Matrices and Multi-Dimensional Arrays
You can transform your code like this :
我知道这个问题已经得到解答,但您可能会找到我的(正在进行中)tile 教程< /a> 有用。 字符串部分正好解决您遇到的问题。
I know that this has already been answered, but you'd probably find my (in progress) tile tutorial useful. The strings section deals with exactly the issue you are having.