WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   if-then-else switch (https://www.wowinterface.com/forums/showthread.php?t=31387)

cormanthor 03-23-10 04:15 AM

if-then-else switch
 
I am starting to develop my own addon. At the moment, I am working through code to determine a player's role in a group based on spec/stance/etc.
One "if" statement I am using feels like it could be much better:
Code:

local _, class = UnitClass("player")
if class == "HUNTER" or class == "MAGE" or class == "ROGUE" or class == "WARLOCK" then
-- do pure dps code here
else
-- finish function
end

Is there a better way to make a switch?

Mokhtar 03-23-10 04:30 AM

Well not really, you could always do something like this but it is not necessarily faster :

Code:

local _, class = UnitClass("player")
local allowed = {
        ["HUNTER"]=true,
        ["MAGE"]=true,
        ["ROGUE"]=true,
        ...
}
if allowed[class] then
-- do pure dps code here
else
-- finish function
end


nightcracker 03-23-10 10:52 AM

There is no built in way for this, but you can make them yourself like this:

Code:

local function multeq(check, ...)
    for i=1, select(#, ...) do
        if check==select(i, ...) then return true end
    end
    return false
end


cormanthor 03-23-10 01:28 PM

Thanks for the replies. I think I'm going to have to keep the code I have, mostly for clarity.

The purpose of the code being to determine someone's spec (easy part) and role ("Damage", "Healer", or "Tank") makes for an apparently complex function. I wish at the least that they would include Righteous Fury in with a pally aura so that I can consolidate the tank checks to a class & stance check. /sigh

Off to make this function useful. Thanks again!

kneeki 03-24-10 08:55 AM

Most all tanking specs have key talents that all tanks will take, so scanning that one talent will determine what role that player is fulfilling. I would really recommend you use nightcracker's suggestion though. It may seem complex now, but it's a really good habit to be in. :)

Mokhtar 03-29-10 09:33 AM

Quote:

Originally Posted by cormanthor (Post 182508)
The purpose of the code being to determine someone's spec (easy part) and role ("Damage", "Healer", or "Tank") makes for an apparently complex function. I wish at the least that they would include Righteous Fury in with a pally aura so that I can consolidate the tank checks to a class & stance check. /sigh

Have a look at http://www.wowace.com/addons/libgrouptalents-1-0/
It is concentrated awesomeness with a spread of bacon...


All times are GMT -6. The time now is 06:43 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI