Language:
Lua     Change language:
Pastebin: 79341
Author: Antiarc
Subject: Untitled
Created: 2007-12-21 14:19:22
Download and save
Toggle line numbers
1local myAddonObject = {}                                -- A blank table. Tables can contain functions, so Lua tables can be pseudo-objects. 
2                                                        -- Local variables only exist in the scope of the block they are defined in. Since this isn't defined in an explicit block, 
3                                                        -- its scope is this file. Code from outside this file won't be able to see the "myAddonObject" variable. 
4 
5local frame = CreateFrame("Frame", nil, UIParent)       -- Create an anonymous (no-name) frame parented to UIParent. 
6frame:RegisterEvent("UNIT_SPELLCAST_SUCCESSFUL")        -- Register UNIT_SPELLCAST_SUCCESSFUL to this frame 
7frame:RegisterEvent("VARIABLES_LOADED")                 -- Register VARIABLES_LOADED to this frame 
8 
9-- Define the OnEvent handler. In this case, when the event is fired, there are several magic variables set - event (which is the name of the event 
10-- and arg/n/, which are the arguments that the event passes along. 
11-- This handler is a function that tries to call a method with the event name on our addon object, and passes the first 7 args to it. 
12-- It will fail if we registered an event to the frame, but didn't define a handler method for it, so be aware of that. 
13 
14-- This is a generalized version of the following code: 
15--[[ 
16local function eventHandler() 
17    if event == "UNIT_SPELLCAST_SUCCESSFUL" then 
18        myAddonObject:UNIT_SPELLCAST_SUCCESSFUL(arg1, arg2, arg3, arg4, arg5, arg6, arg7) 
19    elseif event == "VARIABLES_LOADED" then 
20        myAddonObject:VARIABLES_LOADED(arg1, arg2, arg3, arg4, arg5, arg6, arg7) 
21    end 
22end 
23frame:SetScript("OnEvent", eventHandler) 
24]]-- 
25 
26frame:SetScript("OnEvent", function() 
27    myAddonObject[event](myAddonObject, arg1, arg2, arg3, arg4, arg5, arg6, arg7) 
28end
29 
30-- This defines a function on the myAddonObject table. It is functionally equivalent to: 
31-- myAddonObject.UNIT_SPELLCAST_SUCCESSFUL = function(self, arg1, arg2, arg3) ... end 
32-- Calling a function on a table with the colon syntax passes the table as an implicit first argument. Defining a function with the colon syntax defines an implicit first argument, which 
33-- is expected to be the calling table. 
34function myAddonObject:UNIT_SPELLCAST_SUCCESSFUL(arg1, arg2, arg3) 
35    -- As per http://www.wowwiki.com/Events/U, arg1 is the unit ID of the unit casting the spell. Our addon will watch for events from the player, so we'll check for that. 
36    if arg1 == "player" then 
37        tutorialAddonSpellCastCount = tutorialAddonSpellCastCount + 1 
38 
39        -- arg2 is the spell name, arg3 is the rank 
40        -- Strings can operate as objects which may have functions called on them. In this case, this is is equivalent to string.format("%s", whatever) 
41        -- If we cast Frostbolt Rank 1, then this should print out "You cast Frostbolt Rank 1. You have cast 1 spells so far." 
42        msg = ("You cast %s %s. You have cast %s spells so far."):format(arg1, arg2, tutorialAddonSpellCastCount) 
43 
44        -- Print our message out to the default chat frame (which should be ChatFrame1) 
45        DEFAULT_CHAT_FRAME:AddMessage(msg) 
46    end 
47end 
48 
49function myAddonObject:VARIABLES_LOADED() 
50    -- This initializes tutorialAddonSpellCastCount to 0 if it's nil. Otherwise, it just sets it to itself. 
51    -- This is equivalent to "tutorialAddonSpellCastCount ||= 0" or "if tutorialAddonSpellCastCount == nil then tutorialAddonSpellCastCount = 0 end" 
52 
53    -- We're going to assume that we've set tutorialAddonSpellCastCount as a saved variable, so that its value will be persisted between sessions. 
54    tutorialAddonSpellCastCount = tutorialAddonSpellCastCount or 0 
55end 
56 
Download and save
Toggle line numbers
Thread:
[79341] Untitled by Antiarc at 2007-12-21 14:19:22
Tip: Click the line numbers to toggle highliting on that line.

Paste followup:

Language:
Author:
Subject:


    Tabstop:     bigger biggest
Note: You can prefix a line with "@@@" to highlight it.