我正在尝试为魔兽世界创建一个简单的插件来记录我的杀戮。 我已经相当远了,除了写一个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]是唯一一个正确写入的。我做错了吗?
答案 0 :(得分: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()