1

我正在尝试为魔兽世界创建一个简单的插件来记录我的杀戮。除了 lua 数组的写入存在问题外,我已经走了很远。

我到目前为止的代码

local CharacterDefaults = { 
    kills = {},
    totalkills = 0
}
local killDefaults = {
    DBtimeofday = 0,
    DBplayer = 0,
    DBenemyname = 0,
    DBenemyid = 0,
    DBzone = 0,
    DBkilltype = 0
}

下一部分是在一个检查过度杀伤的事件中

if not KillCount then
    KillCount = CharacterDefaults
end         

if not KillCount.totalkills then
    KillCount.totalkills = 0
end 
KillCount.enemy[KillCount.totalkills] = destName                        
KillCount.kills[KillCount.totalkills] = killDefaults 
KillCount.kills[KillCount.totalkills].DBtimeofday = stamp
KillCount.kills[KillCount.totalkills].DBzone = zone 
KillCount.kills[KillCount.totalkills].DBkilltype = killtype 
KillCount.kills[KillCount.totalkills].DBenemyid = unitId 
KillCount.kills[KillCount.totalkills].DBenemyname = destName 
KillCount.kills[KillCount.totalkills].DBplayer = playerName 
KillCount.totalkills = KillCount.totalkills + 1 

当然还有更多代码,但这是唯一重要的代码(据我所知)。

如果我看这个,我会期望对于每个新的杀戮,都会制作一个新的数组部分并输入值。然而,对于我在魔兽世界中进行的每一次击杀,已经在其中的每一个项目都会得到最后一次击杀的结果。

lua变量保存文件:

KillCount = {
    ["kills"] = {
        {
            ["DBplayer"] = "MyName",
            ["DBzone"] = "Blackrock Depths",
            ["DBkilltype"] = 0,
            ["DBenemyname"] = "Grim Patron",
            ["DBenemyid"] = 9545,
            ["DBtimeofday"] = "11-09-22 10:45:23",
        }, -- [1]
        {
            ["DBplayer"] = "MyName",
            ["DBzone"] = "Blackrock Depths",
            ["DBkilltype"] = 0,
            ["DBenemyname"] = "Grim Patron",
            ["DBenemyid"] = 9545,
            ["DBtimeofday"] = "11-09-22 10:45:23",
        }, -- [2]
        [0] = {
            ["DBplayer"] = "MyName",
            ["DBzone"] = "Blackrock Depths",
            ["DBkilltype"] = 0,
            ["DBenemyname"] = "Grim Patron",
            ["DBenemyid"] = 9545,
            ["DBtimeofday"] = "11-09-22 10:45:23",
        },
    },
    ["totalkills"] = 3,
}

如您所见, [0] 是唯一正确写入的。难道我做错了什么?

4

1 回答 1

3

问题在这里:

KillCount.kills[KillCount.totalkills] = killDefaults

每次杀死时,您都将 KillCount.kills[KillCount.totalkills] 指向 killDefaults,然后修改 killDefaults。问题是,您每次都使用相同的 killDefaults。因此,当您稍后更新 killDefaults 的值时,它会影响您已经创建的对 killDefaults 的每个引用。

尝试类似:

function GetDefaultKills()
    return {
        DBtimeofday = 0,
        DBplayer = 0,
        DBenemyname = 0,
        DBenemyid = 0,
        DBzone = 0,
        DBkilltype = 0
    };
end

KillCount.kills[KillCount.totalkills] = GetDefaultKills()
于 2011-09-22T08:58:36.183 回答