Question

I'm trying to create a simple addon for world of warcraft which records my kills. I've already got'n quite far except there is a problem with the writing of a lua array.

The code I have so far

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

The next piece is inside a event which checks for overkill

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 

Ofcourse there's more code but this is the only important code (as far as I know).

If I look at this I would expect that for every new kill a new array part is made and the values are entered. However, for each kill I make in world of warcraft, every single item already in it will get the results of the last kill.

The lua variables saved file:

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,
}

as you can see the [0] is the only one to be properly writen. Am I doing something wrong?

Was it helpful?

Solution

The problem is here:

KillCount.kills[KillCount.totalkills] = killDefaults

Everytime you kill, you're pointing KillCount.kills[KillCount.totalkills] to killDefaults then modifying killDefaults. The problem is, you are using the same killDefaults every time. So when you udpate the values of killDefaults later, it affects every reference to killDefaults that you have already created.

Try something like:

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

KillCount.kills[KillCount.totalkills] = GetDefaultKills()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top