1. I am trying fill two arrays with the difference in current Mana between updates. Before the iteration, the difference should be 0. After the first iteration, then sum = A - B. Array1 fills 10 increments, does its thing, then resets both itself and the increments. Array2 keeps going, always adding the sum.

    The variable incombat is flagged true, and variables A, B, and sum are reset in PLAYER REGEN DISABLED and false in PLAYER REGEN ENABLED. I would just check for those events in OnUpdate, but I need to do some other things in their functions as well.

     local counter = 0
     local increment = 0
     A = GetPower("player", 0)
     sum = 0
     self.ManaDisplay:Hide()
     self.ManaDisplay:SetScript("OnUpdate", function(self, elapsed)
        counter = counter + elapsed
        if counter > 0.2  then      
            increment = increment + 1
            if increment == 0 then
                B = GetPower("player", 0)
            else
                A = GetPower("player", 0)
            end
            sum = A - B
            tinsert(allManaValues, sum) -- deal with you after fight ends
            if increment == 10 or not in_combat then
                tinsert(manaValues, sum)
                local arraySum = 0
                for i = 1, increment in ipairs(manaValues) do
                    arraySum = arraySum + manaValues[i]
                end
                local segment = math.floor(arraySum / increment)
                self.indicator:SetPoint("CENTER", self.segments[segment], "RIGHT")
                self.indicator:SetScript("OnMouseOver", function(self)
                    GameTooltip:AddLine("%d", sum)
                    GameTooltip:Show()
                end)
                if segment <= stopRed then
                    self.message:SetText(L["Losing Mana!"])
                    self.message:SetTextColor(1, 0.5, 0)
                elseif segment >= startGreen then
                    self.message:SetText(L["Gaining Mana!"])
                    self.message:SetTextColor(1, 1, 0)
                else
                    self.message:SetText("")
                end
            elseif increment > 10 then
                wipe(manaValues)
                increment = 0
            end     
            counter = 0
        end
     end)
    

    Here's the logic bomb:

    • Getting 10 values (or less, if exit early) and reset the count
    • Making sure the first pass doesn't give some astronomical value for sum
    • With my current code, sum will always be 0, rather than the difference between A and B

    EDIT: updated code block.

    I'm not even really sure if you're asking a question. I will say a few things tho:

    You should not be re-anchoring frames or setting scripts in an OnUpdate, even if you're only doing it every 2 seconds or so. Anchor the frame once and set the script on it. No need to put them here.

    I'm also not sure what you mean by 'the logic bomb'. If you could clarify, perhaps I could help.

    To make it a bit easier, here is the paste of my code. What I am having troubles with, beside your advice not to anchor the frame during OnUpdate, is the calculation. To be further honest, I am doubting the need to have the variables A, B, and sum as local to the addon, rather than local to OnUpdate.

    Here is the question, given my code: how do I start the fight with 0 difference, then every 0.2 seconds calculate the difference in mana gain or loss, plug the difference into manaValues and allManaValues.

    Once manaValues has 10 sums, or every 2 seconds, average them, and plot the frame indicator with the average. allManaValues keeps inserting the sums until combat ends. At 2 seconds, wipe manaValues, starting fresh with the sums and 2 seconds. When combat ends, print the average of all the sums in allManaValues to chat; manaValues should reset, regardless whether it has reached 10 sums.

    Both manaValues and allManaValues need to reset when starting combat.

    Since I am only plotting the average of manaValues during combat, that is the only time OnMouseOver should be run, as outside combat, the frame indicator should sit at 0 (even if the player's mana is changing because of drinking).

    And then there is the text feedback of "Gaining Mana!" and "Full burn!", and I am not sure where to put that, if I don't plot during OnUpdate.

    Any suggestions on how to clean this up are welcome, along with the how-to with the math and where-to best practices with code snippets.

    Please, and thank you Mr. Whitehead.

    Example: after 2 seconds, the average of manaValues is -500, so move the indicator to -500. Two seconds later, the average, after reset, is +1500, and move the indicator to 1500. That's on the PvP capture frame, ManaDisplay. Using LibGraph-2.0, I would plot -500, then +1500. So yes, there are two displays. One using the capture frame, one using a real time graph.

    After 2 more seconds, the average is -1000, so move the indicator there, and the real time graph would show -500, +1500, -1000.

    allManaValues contains all three numbers, because it is not reset after 2 seconds.

    OnUpdate and OnMouseOver and the real time LibGraph display all stop once combat ends. Ideally, the real time graph would stay visible with its values until combat starts again, whereupon all three restart. The other thing (and I think I can do this with AceDB) I would like is to keep the last 10 real time graphs, so the player can go back and look them.

    Hopefully this clears up what I want to do.

    Here is the question, given my code: how do I start the fight with 0 difference, then every 0.2 seconds calculate the difference in mana gain or loss, plug the difference into manaValues and allManaValues.

    When asking a question like this, its best to ask it generally, rather than giving me code. My mantra is "Tell me what you want to do, not how you want to do it" just because its the way I'm able to best provide help.

    What I would do is probably something like this:

    1. Have a var 'previousValue' that stores the previous value we're tracking, in this case mana.
    2. When combat starts (or whenever you want to 'clear' the previous value) set it to nil.
    3. In the logic of your code, get the current value using UnitPower.
    4. If previousValue is nil, then set it to the current value
    5. Write the rest of your code, which stores the new value into your table and displays things.
    6. Set previousValue to currentValue at the end of the function, so its stored for the next update.

    This avoids having special cases all over the place because there's only one conditional (which we can use because you WANT that first difference to be 0). Since you don't STORE this first 'dummy' previous value, nothing gets weird.

    Once manaValues has 10 sums, or every 2 seconds, average them, and plot the frame indicator with the average. allManaValues keeps inserting the sums until combat ends. At 2 seconds, wipe manaValues, starting fresh with the sums and 2 seconds. When combat ends, print the average of all the sums in allManaValues to chat; manaValues should reset, regardless whether it has reached 10 sums.

    Seems like this logic is all straightforward.

    And then there is the text feedback of "Gaining Mana!" and "Full burn!", and I am not sure where to put that, if I don't plot during OnUpdate.

    You can do the SetText.. but is there a need to re-anchor the font strings every single update? Perhaps I don't understand precisely what you're trying to do there..

    Thank you for the advice; it pointed me in the right direction. The addon is called Average Combat Mana, currently in alpha on wowace. This is completely dry coded so I expect bugs.

    ACM is geared primarily toward healers, but any mana using class can use it. Every 2 seconds it compares your current mana with the last time checked, then stores the difference. After 5 iterations, it averages the sum of the values and moves the indicator on the frame, along with text output saying your gain or loss.

    Once combat is finished, it averages the sum of all gains and losses and prints that to chat.

    There is some to do stuff in terms of shiny features, but that was the main focus.

    Thank you, Mr Whitehead!

    I've got to the stage of fixing bugs, and passed the stage where the addon won't load. However, there are some bugs I don't understand why they are being caused. I am not using any XML, and the only example I can compare is Halion Helper, and I can't spot what I've done wrong, unless it has something to do with the frame name, which is in my OnInitialize()

    1x AverageCombatMana-@project-version@\AverageCombatMana.lua:398: <name> or '...' expected near '"OnUpdate"'

    Code paste http://pastey.net/147070

     1x Usage: NewLocale(application, locale[, isDefault[, silent]]): 'silent' must be specified for the first locale registered:
     AverageCombatMana-@project-version@\Locales\Localization.enUS.lua:6: in main chunk
    

    Here are examples of my localization.enUS.lua

     local debug = false
     --@debug@
     debug = true
     --@end-debug@
    
     local L = LibStub("AceLocale-3.0"):NewLocale("AverageCombatMana", "enUS", true, debug)
    
     --@localization(locale="enUS", format="lua_additive_table", same-key-is-true=true, handle-unlocalized="english")@
    

    and localization.deDE.lua

     local L = LibStub("AceLocale-3.0"):NewLocale("AverageCombatMana", "deDE")
     if not L then return end
    
     --@localization(locale="deDE", format="lua_additive_table", same-key-is-true=true, handle-unlocalized="english")@
    

    And finally the error with my options file. Probably easily fixed if the main file was working.

     1x AverageCombatMana-@project-version@\AverageCombatManaOptions.lua:6: Usage: GetAddon(name): 'name' - Cannot find an AceAddon 'AverageCombatMana'.
     AceAddon-3.0-10 (Ace3):176: in function `GetAddon'
     AverageCombatMana-@project-version@\AverageCombatManaOptions.lua:6: in main chunk
    

    Options paste http://pastey.net/147071 Do you have any suggestions? I could try creating the frame in the main chunk, and giving it attributes in OnInitialize() but before I try throwing things at the wall to find out what sticks, perhaps expert eyes will see something.

    I've got to the stage of fixing bugs, and passed the stage where the addon won't load. However, there are some bugs I don't understand why they are being caused. I am not using any XML, and the only example I can compare is Halion Helper, and I can't spot what I've done wrong, unless it has something to do with the frame name, which is in my OnInitialize()

    1x AverageCombatMana-@project-version@\AverageCombatMana.lua:398: <name> or '...' expected near '"OnUpdate"'

    This is a syntax error. It tells you precisely what is wrong..

    function AverageCombatMana.ManaDisplay:SetScript("OnUpdate", function(self, elapsed)

    That's line 398, it doesn't make sense. function AverageCombatMana.ManaDisplay:SetScript doesn't do anything sensical, and its not valid Lua syntax.

    Code paste http://pastey.net/147070

     1x Usage: NewLocale(application, locale[, isDefault[, silent]]): 'silent' must be specified for the first locale registered:
     AverageCombatMana-@project-version@\Locales\Localization.enUS.lua:6: in main chunk
    

    That's a library error, can't help you with it.. but it seems like you need to make sure the 'silent' parameter is true for the first locale you register. If you're using a library, you should read the documentation for it.

    Here are examples of my localization.enUS.lua

     local debug = false
     --@debug@
     debug = true
     --@end-debug@
     
     local L = LibStub("AceLocale-3.0"):NewLocale("AverageCombatMana", "enUS", true, debug)
     
     --@localization(locale="enUS", format="lua_additive_table", same-key-is-true=true, handle-unlocalized="english")@
    

    and localization.deDE.lua

     local L = LibStub("AceLocale-3.0"):NewLocale("AverageCombatMana", "deDE")
     if not L then return end
     
     --@localization(locale="deDE", format="lua_additive_table", same-key-is-true=true, handle-unlocalized="english")@
    

    And finally the error with my options file. Probably easily fixed if the main file was working.

     1x AverageCombatMana-@project-version@\AverageCombatManaOptions.lua:6: Usage: GetAddon(name): 'name' - Cannot find an AceAddon 'AverageCombatMana'.
     AceAddon-3.0-10 (Ace3):176: in function `GetAddon'
     AverageCombatMana-@project-version@\AverageCombatManaOptions.lua:6: in main chunk
    

    Options paste http://pastey.net/147071 Do you have any suggestions? I could try creating the frame in the main chunk, and giving it attributes in OnInitialize() but before I try throwing things at the wall to find out what sticks, perhaps expert eyes will see something.