Table 由 lua 中的最后一个重写

Table rewrite by last in lua

我有一个无法解决的问题。

如果我执行此函数 2 或 3 次:

Core:CreateButton({ Name = "Spell__Slot", Slot = 1, OffSetY = 0, OffSetX = 0, }) Core:CreateButton({ Name = "Item__Slot", Slot = 2, OffSetY = 50, OffSetX = 0, }) Core:CreateButton({ Name = "Mount__Slot", Slot = 3, OffSetY = 0, OffSetX = 50, }) Core:CreateButton({ Name = "Pet__Slot", Slot = 4, OffSetY = 50, OffSetX = 50, })

全部 table "SavedData" 被最后改写:

function Core:CreateButton(Saved)
-- Usage.
if type(Saved) ~= 'table' and type(Saved.Name) ~= 'string' and type(Saved.Slot) ~= 'number' then
    print("Core:CreateButton({Saved}): Button must have a valid Name[string] and Slot[number].")
    return
end
-- Create Table.
if not self.Region then self.Region = {} end
if not self.Region[Saved.Slot] then
    -- Unique Table.
    self.Region[Saved.Slot] = {}
    self.Region[Saved.Slot].Data = {}
    self.Region[Saved.Slot].Region = {}
    self.Region[Saved.Slot].SavedData = DefaultValue
    -- Table Merge.
    for key, value in pairs(Saved) do
        self.Region[Saved.Slot].SavedData[key] = value
    end
end end

替换

   self.Region[Saved.Slot].SavedData = DefaultValue
   -- Table Merge.
   for key, value in pairs(Saved) do
       self.Region[Saved.Slot].SavedData[key] = value
   end

   self.Region[Saved.Slot].SavedData = {}
   -- Table Merge.
   for key, value in pairs(DefaultValue) do
       self.Region[Saved.Slot].SavedData[key] = value
   end
   -- Table Merge.
   for key, value in pairs(Saved) do
       self.Region[Saved.Slot].SavedData[key] = value
   end