【问题标题】:Point table to another table in Lua将表指向 Lua 中的另一个表
【发布时间】:2015-01-21 00:31:45
【问题描述】:

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

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

【问题讨论】:

  • 为什么不直接b = a?
  • 因为如果 a 发生变化, b 不会更新。
  • @Feche1320 是的,它会的。代码b=a 导致两个变量引用内存中完全相同的表。对一个的所有更改都会影响“另一个”,因为它们是相同的。我认为您对保存表与引用表的变量感到困惑。也许您的意思是,如果您这样做a={t=1} b=a a={t=2},那么b 不会“更新”以使用与a 相同的表被重新指向参考?
  • 您的编辑是朝着正确方向迈出的重要一步,但这还不够。请创建一个独立的演示来展示您的问题,并使用可以自行运行的代码。

标签: lua lua-table meta-method


【解决方案1】:

你想要__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

同样,您不是将newdata“复制”到maintable,而是在此处更改变量以引用同一个表。

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

但是,如果c 发生变化,这样做不会导致b 更新。

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。

【讨论】:

  • 您好,感谢您的帮助 :) 我遇到了一些棘手的问题,在询问之前我看到了那篇文章并做了你所做的,我发现如果我这样做 _mapcache = { 123, 123213} _test = {} setmetatable(_test, { __index = _mapcache }) print(_test[1]) 确实有效,但如果我这样做: function callThisFunction() setmetatable(_test, { __index = _mapcache }) end _mapcache = { 123, 123213} _test = {} callThisFunction() print(_test[1]) 由于某种原因不起作用.. 我必须在函数中使用 setmetatable,这就是为什么:P
  • 实际上,我收回了我之前所说的话..我遇到了问题..我有表 _mapcache 并且它被另一个脚本的触发器更新,所以当我更新 _mapcache 我做 _mapcache = data 那里是问题所在,如果我直接更改 _mapcache(不是通过指出特定索引,例如 _mapcache.name = "Josh")元表不会得到更新
  • @Feche1320 从 cmets 中的代码很难理解您的问题。请使用更新的详细信息编辑您的问题,准确显示您的问题(以使用真实代码实际重现问题的最小方式),或使用该完整代码在网站上发布新问题。请在上面查看我对您的问题的评论。
  • 我已根据您的新问题描述为我的答案添加了更新。
  • 已解决,谢谢大家的帮助,对XY问题表示抱歉:p
猜你喜欢
  • 1970-01-01
  • 2014-06-03
  • 1970-01-01
  • 2017-09-20
  • 2023-02-17
  • 2016-03-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
相关资源
最近更新 更多