点表到Lua中的另一个表

时间:2014-11-23 04:00:53

标签: lua lua-table meta-method

有没有办法将表指向另一个表?例如:

local a = {}
local b = {}
a.name = "Josh"
print(a.name) -- Prints 'Josh'
print(b.name) -- Prints 'Josh' aswell
a.name = "I don't have a name"
print(a.name) -- Print 'I don't have a name'
print(b.name) -- Prints 'I don't have a name' aswell

我希望你明白我的意思..谢谢

编辑:

好的,这就是这个想法:

我正在制作像这样的恐龙功能

local table = { 1, 2, "hey" }

function drawimage(name, posx, posy referencetable)
    _tabledata[name] = { posx = posx, posy = posy, reference = {}}
    setmetatable(_tabledata[name].reference, { __index = referencetable })
end

drawimage("Header", 0, 50, table)

所有好的,好的,价值观都有效,我们都很高兴...当参考表以这种方式改变它的价值时会出现问题

local data = { 123123, 545454, "heyou" } -- Data is sent from another script via a trigger
table = data

由于我没有通过索引更新它(即:table [1] = 9999)引用变量与真实引用变量“未同步”,我希望您理解:)

EDIT2:

好的,这是我主要问题的一个自我工作的例子

local maintable = { "Stack", "Overflow" }
local maintablecopy = {}

maintablecopy = maintable

print("maintable[1] = " ..maintable[1]) -- Prints Stack
print("maintable[2] = " ..maintable[2]) -- Prints Overflow
print("")
print("maintablecopy[1] = " ..maintablecopy[1]) -- Prints Stack
print("maintablecopy[2] = " ..maintablecopy[2]) -- Prints Overflow
print("")

print("Changing values..")
local newdata = { "Hello", "World" }
maintable = newdata

print("")
print("maintable[1] = " ..maintable[1]) -- Prints Hello
print("maintable[2] = " ..maintable[2]) -- Prints World
print("")
print("maintablecopy[1] = " ..maintablecopy[1]) -- Prints Stack    -- PROBLEM
print("maintablecopy[2] = " ..maintablecopy[2]) -- Prints Overflow -- PROBLEM

print("Using setmetatable..")

maintable = { "Stack", "Overflow" }
maintablecopy = {}
setmetatable(maintablecopy, { __index = maintable })

print("maintable[1] = " ..maintable[1]) -- Prints Stack
print("maintable[2] = " ..maintable[2]) -- Prints Overflow
print("")
print("maintablecopy[1] = " ..maintablecopy[1]) -- Prints Stack
print("maintablecopy[2] = " ..maintablecopy[2]) -- Prints Overflow
print("")

print("Changing values..")
local newdata = { "Hello", "World" }
maintable = newdata

print("")
print("maintable[1] = " ..maintable[1]) -- Prints Hello
print("maintable[2] = " ..maintable[2]) -- Prints World
print("")
print("maintablecopy[1] = " ..maintablecopy[1]) -- Prints Stack    -- PROBLEM
print("maintablecopy[2] = " ..maintablecopy[2]) -- Prints Overflow -- PROBLEM

为什么在变量更新时我无法直接将其指向表?因为我有20个表要更新,这样做会更容易

local _dynamics = {}

local tbl1 = { "Hey", 8787 } 
local tbl2 = { 123, "There" }
local tbl3 = { "You", 1111 }  

function dynamicFunction(name, posx, posy, textsize, reference)
    _dynamics[name] = { posx = posx, posy = posy, textsize = textsize, reference = reference }
end

dynamicFunction("first", 0, 0, 5, tbl1)
dynamicFunction("second", 0, 0, 5, tbl2)
dynamicFunction("third", 0, 0, 5, tbl3)

for key in pairs(_dynamics) do
    local inf = _dynamics[key]
    for i = 1, #inf.reference do
        print(inf.reference[i])
        if i == #inf.reference then
            print("")
        end
    end
end

print("")
print("")

tbl1 = { "aaaaa", "bbbbbbbbbb" }
tbl2 = { "ccccccccccc", "ttttttttttt" }
tbl3 = { "rrrrrrr", "yyyyyyyyyyy" }

for key in pairs(_dynamics) do
    local inf = _dynamics[key]
    for i = 1, #inf.reference do
        print(inf.reference[i])
        if i == #inf.reference then
            print("")
        end
    end
end

print("Values should get updated on the reference variable, but it doesn't.. this would save me     to do a check for every single variable")

您可以在http://www.compileonline.com/execute_lua_online.php上运行它,看看自己的意思。

对不起,如果这是一团糟,但我的英语不是最好的:D

1 个答案:

答案 0 :(得分:2)

你想要__index metamethod

local a = { name="Josh" }
local b = {}

print(a.name) --> Josh
print(b.name) --> nil

setmetatable(b,{__index=a})
print(b.name) --> Josh

a.name = "Gilligan"
print(a.name) --> Gilligan
print(b.name) --> Gilligan

-- but note! the shadow
b.name = "overridden"
print(a.name) --> Gilligan
print(b.name) --> overridden

b.name = nil
print(a.name) --> Gilligan
print(b.name) --> Gilligan

有关详细信息,请提供我的这篇文章:
http://phrogz.net/lua/LearningLua_ValuesAndMetatables.html


对Edit2的回复

让我总结一些代码的问题:

local maintablecopy = {}
maintablecopy = maintable

使用上面的代码,您可以创建一个表,设置maintablecopy以引用该表,然后在将maintablecopy设置为引用其他表时完全放弃它。这表明缺乏对变量如何运作的理解。

local newdata = { "Hello", "World" }
maintable = newdata

同样,您没有“复制”newdatamaintable,您正在更改变量以引用此处的相同表格。

maintable = { "Stack", "Overflow" }
maintablecopy = {}
setmetatable(maintablecopy, { __index = maintable })

-- …

local newdata = { "Hello", "World" }
maintable = newdata

同样,同样的问题。以下是一些更改代码的方法:

替换表的内容

而不是maintable = newdata你可以这样做:

function copytable(from,to_table)
  -- erase all old keys
  for k,_ in pairs(to_table) do to_table[k] = nil end

  -- copy the new ones over
  for k,v in pairs(from) do to_table[k] = v end
end

local a = { name="Foo" }
local b = {}
copytable(a,b)
print(a.name == b.name) --> true

local c = { name="NEW" }
copytable(c,b)
print(c.name == b.name) --> true

但是,如果b发生更改,执行此操作不会导致c更新。

c.name = "EVEN NEWER"
print(c.name == b.name) --> false

更新__index

local a = { name="Foo" }
local b = setmetatable({},{__index=a})
print(a.name == b.name) --> true

-- cause b to follow c now instead of a
local c = { name="NEW" }
getmetatable(b).__index = c
print(c.name == b.name) --> true

c.name = "EVEN NEWER"
print(c.name == b.name) --> true

通常,您需要退后一步并描述您尝试解决的原始问题,而不是此XY problem