Working with Events
From Riftui Wiki
For an explanation of what an event is, see Event
Events in Rift are tables (see Table).
table.insert(Event.Addon.SavedVariables.Load.Begin, {Function, Identifier, Text Label})
Parameters:
- Function -- the name of the local function you want to run when the event fires. Function names can use capital or small letters and the underscore _ character.
- Identifier (String) -- the Identifier in the addon's RiftAddon.toc
- Text Label (String) -- an addon-specific text label. It doesn't have any particular meaning to Rift, but it will be used in error reports and performance reports, so please choose something that's meaningful to you (and, ideally, to your users as well)
Some Events may have additional, or even different parameters.
Example, from ZorbasBuffBars
local function refresh() -- Get the list of buffs from the player. Note that "player" can be replaced by "player.target" to turn this into a target's-buff-bar display. local bufflist = Inspect.Buff.List("player") if not bufflist then return end -- If we don't get anything, then we don't currently have information about the player. This may happen when the player is logging in or teleporting long distances. local buffdetails = Inspect.Buff.Detail("player", bufflist) -- Retrieve the details for all of our buffs. If there were only certain buffs we cared about, we could filter them out before asking for detail. StartBuffBars() -- Reset the buff bars and start entering buffs. -- We want to order buffs by their time remaining, splitting apart buffs and debuffs. We do the ordering here, in standard Lua code. local bbars = {} for id, buff in pairs(buffdetails) do buff.id = id -- Make a copy of the ID, because we'll need it table.insert(bbars, buff) end table.sort(bbars, function (a, b) if a.buff ~= b.buff then return not b.buff end if a.duration and b.duration then return a.remaining > b.remaining end if not a.duration and not b.duration then return false end return not a.duration end) -- Now that we have the ordering, we just add the bars one at a time. Done! for k, v in ipairs(bbars) do AddBuffBar(v) end end -- Here's our event. As mentioned, this triggers every single frame. See TrionDevelopmentTools for more detail on events. table.insert(Event.System.Update.Begin, {refresh, "ZorbasBuffBars", "refresh"})
You might notice a few things. The function refresh() is a local function, meaning it only exists and works in ZorbasBuffBars, and it is created before the Event that uses it, in this case Event.System.Update.Begin
table.insert is used to add the parameters to the Event. refresh is not a string, so it not in quotes, but you will also notice does not have () in the event. "ZorbasBuffBars" is a string, and is the Identifier in its toc file. "refresh" is a string, but in this case, it is not a very descriptive text label. It is fine for a simple addon, but with something much more complex, you would want to name it accordingly, like "buff_refresh_function" or the like.