我正在尝试为魔兽世界创建一个简单的插件,记录我的杀戮。我已经说得够多了,除了一个写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
当然还有更多的代码,但这是唯一重要的代码(据我所知)。
如果我看这个,我希望对于每个新的kill,都创建一个新的数组部分并输入值。然而,对于我在魔兽世界中的每一个击杀,它里面的每一个物品都会得到最后一个击杀的结果。
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]是唯一一个被正确写的。我做错了什么吗?
问题就在这里:
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()