1. #1

    Question Hourly Alarm Clock Lua Question

    For the last couple days I've been dabbling in Lua coding and was trying to create a simple addon in which a sound was played every hour at local time. Here is my code at this time:

    local zA = date("%M")
    local zB = date("%S")
    if (zA == "00" and zB == "00") then
    PlaySoundFile("Sound\\Spells\\Flare.wav");
    end

    I've looked through a couple addons for coding help (uClock, Broker_uClock), read through WoWWiki's "API date" and "Lua variable scoping", and searched around GameTime.lua on wowprogramming.com but no luck yet. I feel like I'm missing something that is right in front of my face and was hoping all of you could give me in the right direction. Thanks in advance!

  2. #2
    If you want repeated checking, you'll probably want OnUpdate. OnUpdate is a frame script that fires every time the frame is re-drawn (e.g. a lot). You usually want to throttle this, as doing operations around 60 times a second is usually redundant.

    In this example, I've throttled the function to check once every two seconds.
    Code:
    -- Make a local variable that tells us whether the sound was played previously during this (minute == 0) occurrence
    local firedLast = false
    CreateFrame("Frame"):SetScript("OnUpdate",function(self,elapsed)
        -- increment a member on the frame, initializing it to zero if it has no value
        self.elapsed = (self.elapsed or 0)+elapsed
        -- if 2 seconds passed since the last time, then check current time
        if self.elapsed >= 2 then
            -- make sure you call tonumber() if you compare to a number
            local minutes = tonumber(date("%M"))
            if minutes == 0 then
                if not firedLast then
                    -- we didn't play it during this minute #0 yet, so do it now
                    PlaySoundFile("Sound\\Spells\\Flare.wav")
                    -- make sure it doesn't play until after minute has been ~= 0
                    firedLast = true
                end
            else
                -- reset the var so that the sound will be played again the next time minute == 0
                firedLast = false
            end
            -- reset the internal time counter to zero so it starts counting the 2 second throttle again
            self.elapsed = 0
        end
    end)
    UI & AddOns expert | Interface & Macros moderator - My work

  3. #3
    Treeston,

    Thanks for the code, works like a charm! Got another question for you, if you don't mind. I've accomplished a couple simple things (like moving and locking frames, scrolling the minimap with the mousewheel, hiding parts of the UI) but I wanted to dive deeper in to the coding of Lua (possibly learn how to make my own addons / oUF frames). Where you be a good place to look up reference code (like the "tonumber" you added above) or some basic to advanced guides / coding examples? Thanks in advance!

  4. #4
    i go here for my API functions:

    http://www.wowwiki.com/World_of_Warcraft_API

    a nice list of everything you can do


    AddOn Author of: SpellNotReadyYet, Rune of Powa

  5. #5
    The wowprogramming docs are usually the most up-to-date (because most used). They also have the FrameXML browser and other useful stuff.

    There's also the Lua manual, though it might be a bit technical if you're just starting out.

    Really, the best way of finding out how to make stuff work is by looking at other addons with similar functionality. You can also look at the FrameXML, but by doing so you might pick up some nasty habits, so always take it with a grain of salt.

    Lastly, most authors will be happy to help you with questions on IRC. The best channels for this would be #wowace#wowuidev,[email protected]. You could of course also try [email protected]. I can usually be found there, too.
    UI & AddOns expert | Interface & Macros moderator - My work

Posting Permissions

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