View Single Post
03/30/18, 09:51 AM   #1
Letho
AddOn Author - Click to view addons
Join Date: Apr 2016
Posts: 238
Updating values using RegisterForUpdate()

Hey,

I have to update duration values (represented by a label) of ~10 objects. Each object contains a control (Top level window) and it's data:

Code:
object.control = #userData
object.data = {
    ['durationInfo'] = {
        ['startTime'] = 0100.00000
        ['endTime'] = 120.12345,
    },
    ['someOtherData'] = {
        -- ...
    },
    ['evenMoreData'] = {
        -- ...
    }
}
All objects show the display duration, taken from self.data.durationInfo, as a label that gets updated ten times per second (= needed for a display that has a precision of one digit after the dot). Let's say our GetGameTimeSeconds() is 114.2, the displayed remaining duration would be 5.92345 seconds, rounded to and displayed as 5.9s.


Now the question: There is two ways of achieving it:

1) Registering one updater that iterates through all objects and sets their displayed durations

Code:
Addon:RegisterForUpdate("GenericUpdater", 100, OnUpdateHandler)

function Addon.OnUpdateHandler()
    local curTime = GetGameTimeSeconds()
    for objName, objData in pairs(Addon.objects) do
        local durationDisplay = objData.durationInfo.endTime - curTime
        Addon.objects[objName]:SetDuration(durationDisplay)
    end

    -- some other stuff that needs to be checked onRegisteredUpdate, too, there are ~4-5 other tables that are iterated here
end
2) Registering an updater for every single object that updates it
Code:
-- genericObj is the class that all objects inherit their methods from
function genericObj:New(controlName)
    -- setting up the metatable, controls, etc.
    -- ...
    -- ...
    -- ...

    genericObj:RegisterForUpdate(controlName, 100, genericObj.OnUpdate)
end

function genericObj.OnUpdate()
    local durationDisplay = self.data['endTime'] - curTime -- (curTime being defined somewhere and stored 
    'globally' in the addon class)
end

-- create an instance of the class genericObj
local testObject = genericObj:New("SuperDuperTestObject")

In short words: Should I register an updater for every object, that only handles itself, or should I register a 'monster updater' for the whole addon that updates every object that is part of the addon by iterating through it using a loop?

Last edited by Letho : 03/30/18 at 10:00 AM.
  Reply With Quote