To listen to events, you always need a frame - it doesn't need to be shown, it just needs to be there. Use CreateFrame for this purpose.
Code:
local f = CreateFrame("Frame")
2. Next, you have to register for the applicable events (using :RegisterEvent). In this case, we want to listen for COMBAT_LOG_EVENT_UNFILTERED
Code:
f:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
3. Afterwards, you have to create the OnEvent handler script on your frame. We'll use :SetScript to do that.
Code:
f:SetScript("OnEvent", function(self, event, ...)
The ... parameter in the function definition above is special in that it takes all function arguments not handled otherwise into a list object. To take arguments from this list, we use select.
4. We want to filter only spells cast by the player. We achieve this by matching arg4 (srcGUID) against the player's GUID. To prevent calling UnitGUID too often, we will cache this result in our frame table.
Code:
self.pguid = self.pguid or UnitGUID("player")
if (self.pguid == (select(4,...))) then
5. We need to check for successful spell casts - for instant casts, this means that arg2 of COMBAT_LOG_EVENT_UNFILTERED will be "SPELL_CAST_SUCCESS", for cast time spells it will be "SPELL_CAST_START".
Code:
local subType = (select(2,...))
if (subType == "SPELL_CAST_START") or (subType == "SPELL_CAST_SUCCESS") then
6. We want to specifically check for certain spells. We'll fetch the spell ID (arg12) into a local, then check it against the spell IDs and check it against the IDs for the spells we want to announce.
For simplicity's sake, I'm going to output the spell name using print.
Code:
local spellID = (select(12,...))
if spellID == 403 then
print("Lightning Bolt!")
elseif spellID == 331 then
print("Healing Wave!")
elseif spellID == 73920 then
print("Healing Rain!")
end
7. Close all the end's and brackets.