问题描述
我有一个函数可以动态改变3D数组或表格的大小,但是它会一直断言它返回值为零,因为它超出了范围。这里是它的代码:
function resize()
temp = {}
for h = 1,height为
table.insert(temp,{})
for y = 1,length do
table.insert(temp [h],{})
for x = 1,宽度为
num = 16
if #blocks if #blocks [h] if #blocks [h] [ y] num = blocks [h] [y] [x]
结束
结束
结束
table.insert(temp [h] [y],num)
end
end
end
blocks = temp
end
我知道它的评论不是很好,但是它的想法是它创建了一个新的表格,其中包含更改后的维度,然后将块的数据叠加到新表格上,最后用新的临时表覆盖块。
长度的宽度和高度可以增加或减少一个,但一次只能改变一个。
我不确定是否足够清楚地解释了它,如果不让我知道,我会尝试更详细地解释。
谢谢大家,
James
我不确定这是否是最好的方法,但它确实有效。
pre $函数resize()
temp = {} - 临时表
- 插入所有高度级别
for h = 1,height do table.insert(temp,{})end
- 插入所有长度
for h = 1,高度为l = 1时的
,长度为table.insert(temp [h],{})结束
结束
- 插入所有宽度和默认值当h = 1时它们为0
,l = 1时高度为
,w = 1时长度为
,宽度为table.insert(temp [h] [l],0)结束
结束
结束
- 如果ca nvas大小增加
if #blocks if #blocks [1]< = length然后
if #blocks [1] [1]< = width then
for h = 1,#blocks do
for l = 1,#blocks [1] do
for w = 1,#blocks [1] [1] do
- 填充块中的数据
temp [h] [l] [w] = blocks [h] [l] [w]
end
end
end
end
end
end
- 如果#blocks> =高度,则画布大小减少
;然后
if #blocks [1]> = length然后
if #blocks [1] [1]> =宽度然后
for h = 1,#temp do
for l = 1,#temp [1] do
for w = 1,#temp [1] [1] do
- 从块中填入数据但不填写最后的值
temp [h] [l] [w] = blocks [h ] [l] [w]
结束
结束
end
end
end
end
- 用新尺寸覆盖块
blocks = temp
I have a function that is meant to dynamically change the size of a 3D Array or table but it keeps breaking saying that it return value nil because it is out of bounds. Here is the code for it:
function resize()
temp = { }
for h=1, height do
table.insert( temp , { } )
for y=1, length do
table.insert ( temp[h], { } )
for x=1, width do
num = 16
if #blocks <= height then
if #blocks[h] <= length then
if #blocks[h][y] <= width then
num = blocks[h][y][x]
end
end
end
table.insert( temp[h][y] , num )
end
end
end
blocks = temp
end
I know it's not very well commented but the idea is that it creates a new table with the changed dimensions, and then superimpose the data of blocks over the new table and finally overwriting blocks with the new temp table.
The length width and height are changed by one either incremented or decremented but only one at a time.
I'm not sure if I explained it clearly enough, if not let me know and I'll try explain in more detail.
Thanks all,James
Well I'm not sure if this is the best way to do it, but it does work.
function resize()
temp = { } -- temp table
-- inserting all the height levels
for h=1, height do table.insert( temp , { } ) end
-- inserting all the lengths
for h=1, height do
for l=1, length do table.insert( temp[h], { } ) end
end
-- inserting all the width and defaulting them to 0
for h=1, height do
for l=1, length do
for w=1, width do table.insert( temp[h][l] , 0 ) end
end
end
-- if the canvas size is increasing
if #blocks <= height then
if #blocks[1] <= length then
if #blocks[1][1] <= width then
for h=1, #blocks do
for l=1, #blocks[1] do
for w=1, #blocks[1][1] do
-- fill in data from blocks
temp[h][l][w] = blocks[h][l][w]
end
end
end
end
end
end
--if the canvas size is decreasing
if #blocks >= height then
if #blocks[1] >= length then
if #blocks[1][1] >= width then
for h=1, #temp do
for l=1, #temp[1] do
for w=1, #temp[1][1] do
-- fill in data from blocks but not the last value
temp[h][l][w] = blocks[h][l][w]
end
end
end
end
end
end
-- overwrite blocks with the new dimensions
blocks = temp
这篇关于动态表或数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!