Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 3d表格缺少对象持久性(键值与表格数据不一致)_Lua - Fatal编程技术网

Lua 3d表格缺少对象持久性(键值与表格数据不一致)

Lua 3d表格缺少对象持久性(键值与表格数据不一致),lua,Lua,所以我对Lua有点奇怪的问题,我使用以下函数来设置、读取和写入一个名为Cube的全局3D“数组”。然而,似乎每次我读或写这个“数组”时,数据只存储在函数使用的实例上,尽管多维数据集是一个全局变量,但不能说我以前遇到过这个问题,非常奇怪 -- stole this one from: https://stackoverflow.com/questions/27976526/using-a-coordinate-pair-as-a-key-in-a-lua-table -- basically th

所以我对Lua有点奇怪的问题,我使用以下函数来设置、读取和写入一个名为Cube的全局3D“数组”。然而,似乎每次我读或写这个“数组”时,数据只存储在函数使用的实例上,尽管多维数据集是一个全局变量,但不能说我以前遇到过这个问题,非常奇怪

-- stole this one from: https://stackoverflow.com/questions/27976526/using-a-coordinate-pair-as-a-key-in-a-lua-table
-- basically the intended use is to store a 2d table of block information for the level the turtle is on

function setUpLevel()
    local test = {_props = {}}
    local mt = {}


    local function coord2index(x, z)
        return ((x-1) * xMax) + z
    end

    mt.__index = function(s, k)
        if s._props[coord2index(k[1], k[2])] ~= nil then
            return s._props[coord2index(k[1], k[2])]
        end
    end

    mt.__newindex = function(s, k, v)
    s._props[coord2index(k[1], k[2])] = v 
    end
    mt.__call = function (t, k)
        if type(k) == "table" then print "Table" end
    end

    setmetatable(test, mt)
    return test
    --test[{1,2}] = 5
end

function setupCube()
    local cube = {}
    cube[relY]=setUpLevel()
    return cube
end

Cube = setupCube()

function readCubeData(x,y,z)
    if (Cube[y]==nil) then
        return nil
    end
    -- debug
    return Cube[y][{x,z}]
end

function storeCubeData(x,y,z,data)
    if (readCubeData(x,y,z)==nil) then
        Cube[y]=setUpLevel()
    end



    Cube[y][{x,z}]=data
    data= readCubeData(x,y,z)
    print (x,",",y,",",z,":",readCubeData(x,y,z))
    sleep(.5)
end
输出示例:

storeCubeData()
中的print语句将提供正确的输出(顺序无关紧要,只是对应于x、y、z值的数据)

但是,
start()

将产生

不幸的是,这是不正确的,x,y,z值没有指向正确的值,而且表格也不应该在相同的x,y,z位置重复。我真的一辈子都搞不明白这一点

错误在于
如果(readCubeData(x,y,z)=nil),那么
存储cubedata(x,y,z,data)
。每次尝试在“级别”中存储新信息时,都会覆盖整个“级别”。如果(多维数据集[y]==nil),则应为
,然后为
。整个
if
语句可以进一步简化为
Cube[y]=Cube[y]或setUpLevel()


此外,
如果s._props[coord2index(k[1],k[2])]~=nil,则不需要
;并且可以将函数定义为本地函数;而散列函数
coord2index
将导致冲突(例如
coord2index(1,5)=coord2index(2,-5)
,如果
xMax==10
),那么请按照@Egor Skriptunoff所说的替换它。

尝试
局部函数coord2index(x,z)返回x.;“.z结束
function start()
    detectAndStore()
    print("=============")
    sleep(1)
    data= readCubeData(0,relY,1)
    print (0,",",relY,",",1,":",readCubeData(0,relY,1))
    sleep(.5)
    data= readCubeData(-1,relY,0)
    print (-1,",",relY,",",0,":",readCubeData(-1,relY,0))
    sleep(.5)
    data= readCubeData(0,y,-1)
    print (0,",",relY,",",-1,":",readCubeData(0,relY,-1))
    sleep(.5)
    data= readCubeData(1,y,0)
    print (1,",",relY,",",0,":",readCubeData(1,relY,0))
    sleep(.5)
end
start()