1. #1

    Savedvars and tables

    So I'm modifying QDKP2 for my guilds needs, I've mentioned that in several posts.

    There's a saved var, QDKP2_Data that contains a metric you-know-what ton of table data. There's a function that takes QDKP2_Data[PlayerGuild] and assigns it to GuildData. It then takes GuildData and does all sorts of stuff like this:

    Code:
    GuildData = QDKP2_Data[PlayerGuild]
    .
    .
    .
    GuildData.raid        = GuildData.raid or {}
    GuildData.raidOffline = GuildData.raidOffline or {}
    GuildData.standby     = GuildData.standby or {}
    GuildData.raidRemoved = GuildData.raidRemoved or {}
    GuildData.TimerBase   = GuildData.TimerBase or {}
    .
    .
    .
    QDKP2raid = GuildData.raid
    QDKP2raidOffline = GuildData.raidOffline
    QDKP2standby = GuildData.standby
    QDKP2raidRemoved = GuildData.raidRemoved
    QDKP2timerBase = GuildData.TimerBase
    Now, since QDKP2_Data is a table, GuildData is mearly a pointer to that table, and subsequently the QDKP2 variables are pointers to a pointer. (I didn't think Lua did it like that, but after some reading found I was wrong. And here I thought Lua didn't have pointers like C)

    I added in some tables following the same format and they persist between game sessions and /reload.

    My problem comes in when I try to add a non-table variable:

    Code:
    GuildData.GuildHours = GuildData.GuildHours or 0
    .
    .
    QDKP2GuildHours = GuildData.GuildHours
    The GuildHour data always gets reset to 0, and I'm not sure why. The 0 would be under, effectively, QDKP2_Data[PlayerGuild]["GuildHours"] which is a table, right? Which QDKP2GuildHours should just point to? Or am I thinking this through wrong and just need to be a better debugger? (I believe I'm not resetting QDKP2GuildHours to 0 in my code though)
    Moo.

  2. #2
    Deleted
    No, you cannot really apply to C (and derivates) concept of pointers here. Well you can, but not quite.

    Tables, functions and userdata are pointers. This is most noticable in tables:
    Code:
    local function b(t) t[1] = "new string" end local a = {"old string"} b(a) print(a[1])
    > new string
    As you can see, "table variables" are always pointers (allocated on the stack) to a lua table (allocated in the heap). This is also the reason why, for example, the memory used by a table is not freed when they go out of scope, but only when the garbage collector collects them.

    This contrasts to all other variables, which are always allocated on the stack.
    Code:
    local function b(v] v = "new string" end local a = "old string" b(a) print(a)
    > old string
    In your example, GuildData is a table, while GuildData.GuildHours is a numeric value in that table.

    When you execute "QDKP2GuildHours = GuildData.GuildHours", you simply copy the (numeric) value of GuildData.GuildHours to QDKP2GuildHours. If you now change the value of QDKP2GuildHours, the value of GuildData.GuildHours stays unchanged (as both are numeric values and thus not pointers).

  3. #3
    So to get GuildHours to persist through the way that the tables do, I have to make it a table as well.

    Code:
    GuildData.GuildHours = GuildData.GuildHours or {0}
    QDKP2GuildHours = GuildData.GuildHours

    If I understand your explanation, if I did the above except QDKP2GuildHours = GuildData.GuildHours[1], that would just copy the value over. (I think I might have tried that with no success...) So I have to do the definition above, but use QDKP2GuildHours[1] in my code later on and any changes to it would persist. Makes sense from working with my other tables. Seems sloppy, but I'm trying to stay within the same nomenclature that the original author is using, he doesn't reference the QDKP2_Data table directly anywhere and assigns his pointers. I guess it makes sense, who'd want to type out QDKP2_Data[Guildname]["GuildHours"] and various other subtables?

    Time for some search and replace! (unless there's a better way to do it, which I'm sure there is, since I'm not worthy )
    Moo.

  4. #4
    Deleted
    You can always do "GuildData.GuildHours = <new value>", I suppose.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •