07-27-15, 06:41 PM | #1 |
Optimizing/checking bad coding 2 small addons
Hello, I'd like some help optimizing or checking bad coding in 2 small addons I wrote earlier today.
The first one announces cooldowns/spells used, but I don't like how I'm currently checking spells, feels really clunky so I wonder if there's a better way to do it: Code:
-------------------------------------- --cooldownAnnouncer -------------------------------------- ---------------CONFIG--------------- -- Channel local channel = "SAY"; -- Spells to check local cooldowns = { ["MAGE"] = {61316}, --Dalaran Brilliance as an example }; ---------------CONFIG--------------- -- Player local _, playerClass = UnitClass("player"); -- Create frame local cooldownAnnouncer = CreateFrame("Frame") -- Events to listen cooldownAnnouncer:RegisterEvent("UNIT_SPELLCAST_SENT") cooldownAnnouncer:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED") -- Do stuff cooldownAnnouncer:SetScript("OnEvent", function(self, event, unit, spell) if unit == "player" then for i=1,#cooldowns[playerClass] do if spell == GetSpellInfo(cooldowns[playerClass][i]) then if event == "UNIT_SPELLCAST_SUCCEEDED" then SendChatMessage(spell, channel); end end end end end) The second one sets raid icons to the player and party (for arena) based on if a spell cast is successful (ex. a buff for when rogues vanish, displacer beast bug, etc). I reused code I found in this thread. Code:
-------------------------------------- --raidIcons -------------------------------------- -- Spells to check local spells = { ["DEATHKNIGHT"] = GetSpellInfo(57330), -- Horn of Winter ["DRUID"] = GetSpellInfo(1126), -- Mark of the Wild ["HUNTER"] = GetSpellInfo(13165), -- Aspect of the Hawk ["MAGE"] = GetSpellInfo(61316), -- Dalaran Brilliance ["MONK"] = GetSpellInfo(115921), -- Legacy of the White Tiger ["PALADIN"] = GetSpellInfo(20217), -- Blessing of Kings ["PRIEST"] = GetSpellInfo(21562), -- Power Word: Fortitude ["ROGUE"] = GetSpellInfo(1784), -- Stealth ["SHAMAN"] = GetSpellInfo(52127), -- Water Shield ["WARLOCK"] = GetSpellInfo(109773), -- Dark Intent ["WARRIOR"] = GetSpellInfo(6673), -- Battle Shout } -- 1 = Yellow 4-point Star 3 = Purple Diamond 5 = White Crescent Moon 7 = Red "X" Cross -- 2 = Orange circle 4 = Green Triangle 6 = Blue Square 8 = White Skull -- Icons to set local icons = { ["DEATHKNIGHT"] = {DEATHKNIGHT=8,DRUID=2,HUNTER=4,PALADIN=3,MAGE=5,MONK=4,PRIEST=5,ROGUE=1,SHAMAN=6,WARLOCK=3,WARRIOR=7}, ["DRUID"] = {DEATHKNIGHT=8,DRUID=2,HUNTER=4,PALADIN=3,MAGE=5,MONK=4,PRIEST=5,ROGUE=1,SHAMAN=6,WARLOCK=3,WARRIOR=7}, ["HUNTER"] = {DEATHKNIGHT=8,DRUID=2,HUNTER=4,PALADIN=3,MAGE=5,MONK=2,PRIEST=5,ROGUE=1,SHAMAN=6,WARLOCK=3,WARRIOR=7}, ["MAGE"] = {DEATHKNIGHT=8,DRUID=2,HUNTER=4,PALADIN=3,MAGE=5,MONK=4,PRIEST=6,ROGUE=1,SHAMAN=6,WARLOCK=3,WARRIOR=7}, ["MONK"] = {DEATHKNIGHT=8,DRUID=2,HUNTER=4,PALADIN=3,MAGE=5,MONK=4,PRIEST=5,ROGUE=1,SHAMAN=6,WARLOCK=3,WARRIOR=7}, ["PALADIN"] = {DEATHKNIGHT=8,DRUID=2,HUNTER=4,PALADIN=6,MAGE=5,MONK=4,PRIEST=5,ROGUE=1,SHAMAN=6,WARLOCK=3,WARRIOR=7}, ["PRIEST"] = {DEATHKNIGHT=8,DRUID=2,HUNTER=4,PALADIN=3,MAGE=6,MONK=4,PRIEST=5,ROGUE=1,SHAMAN=6,WARLOCK=3,WARRIOR=7}, ["ROGUE"] = {DEATHKNIGHT=8,DRUID=2,HUNTER=4,PALADIN=3,MAGE=5,MONK=4,PRIEST=5,ROGUE=1,SHAMAN=6,WARLOCK=3,WARRIOR=7}, ["SHAMAN"] = {DEATHKNIGHT=8,DRUID=2,HUNTER=4,PALADIN=3,MAGE=5,MONK=4,PRIEST=5,ROGUE=1,SHAMAN=6,WARLOCK=3,WARRIOR=7}, ["WARLOCK"] = {DEATHKNIGHT=8,DRUID=2,HUNTER=4,PALADIN=6,MAGE=5,MONK=4,PRIEST=5,ROGUE=1,SHAMAN=6,WARLOCK=3,WARRIOR=7}, ["WARRIOR"] = {DEATHKNIGHT=8,DRUID=2,HUNTER=4,PALADIN=3,MAGE=5,MONK=4,PRIEST=5,ROGUE=1,SHAMAN=6,WARLOCK=3,WARRIOR=7}, } -- Create frame local raidIcons = CreateFrame("Frame") -- Events to listen raidIcons:RegisterEvent("UNIT_SPELLCAST_SENT") raidIcons:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED") -- Do stuff raidIcons:SetScript("OnEvent", function(self, event, unit, spell) if not UnitIsGroupLeader("player") and not UnitIsGroupAssistant("player") then return end if GetNumGroupMembers()>5 then return end local _, playerClass = UnitClass("player") if unit == "player" and spell == spells[playerClass] then if event == "UNIT_SPELLCAST_SUCCEEDED" and GetRaidTargetIndex("player")==nil then members = GetNumGroupMembers() icon = icons[playerClass] SetRaidTarget('player',icon[playerClass]) for i=1,(members-1) do SetRaidTarget('party'..i,icon[select(2,UnitClass('party'..i))]) end end end end) |
|
07-28-15, 01:36 PM | #2 |
Hi,
For the first one, you can start by using hash tables instead of arrays. Use the value as the key, and true as the value. (or any other non false value) This will let you do a simple table key lookup to determine whether a given value exists, rather than looping over the whole table. Code:
local cooldowns = { ["MAGE"] = { [61316] = true, --Dalaran Brilliance as an example }, }; if cooldowns[61316] then print("this spell exists") end Code:
cooldownAnnouncer:RegisterUnitEvent("UNIT_SPELLCAST_SENT", "player") cooldownAnnouncer:RegisterUnitEvent("UNIT_SPELLCAST_SUCCEEDED", "player") Code:
-------------------------------------- --cooldownAnnouncer -------------------------------------- ---------------CONFIG--------------- -- Channel local channel = "SAY"; -- Spells to check local cooldowns = { ["MAGE"] = { [61316] = true, --Dalaran Brilliance as an example }, }; ---------------CONFIG--------------- -- Player local _, playerClass = UnitClass("player"); -- Create frame local cooldownAnnouncer = CreateFrame("Frame") -- Events to listen cooldownAnnouncer:RegisterUnitEvent("UNIT_SPELLCAST_SENT", "player") cooldownAnnouncer:RegisterUnitEvent("UNIT_SPELLCAST_SUCCEEDED", "player") -- Do stuff cooldownAnnouncer:SetScript("OnEvent", function(self, event, unit, spell, _, _, spellID) if cooldowns[playerClass][spellID] then if event == "UNIT_SPELLCAST_SUCCEEDED" then SendChatMessage(spell, channel); end end end)
__________________
Grab your sword and fight the Horde! |
|
07-30-15, 08:15 PM | #3 |
ty
thanks a lot, didn't know you could only check events for 1 unit, I'll keep that in mind
|
|
WoWInterface » Developer Discussions » Lua/XML Help » Optimizing/checking bad coding 2 small addons |
«
Previous Thread
|
Next Thread
»
|
Display Modes |
Linear Mode |
Switch to Hybrid Mode |
Switch to Threaded Mode |
|
|