Thread Tools Display Modes
03-23-10, 04:15 AM   #1
cormanthor
A Warpwood Thunder Caller
 
cormanthor's Avatar
AddOn Compiler - Click to view compilations
Join Date: Nov 2008
Posts: 97
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?
__________________
Some days it's just not worth chewing through the restraints...
  Reply With Quote
03-23-10, 04:30 AM   #2
Mokhtar
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: May 2007
Posts: 22
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
  Reply With Quote
03-23-10, 10:52 AM   #3
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
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
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.

Last edited by nightcracker : 03-23-10 at 03:22 PM.
  Reply With Quote
03-23-10, 01:28 PM   #4
cormanthor
A Warpwood Thunder Caller
 
cormanthor's Avatar
AddOn Compiler - Click to view compilations
Join Date: Nov 2008
Posts: 97
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!
__________________
Some days it's just not worth chewing through the restraints...
  Reply With Quote
03-24-10, 08:55 AM   #5
kneeki
A Flamescale Wyrmkin
 
kneeki's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 134
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.
  Reply With Quote
03-29-10, 09:33 AM   #6
Mokhtar
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: May 2007
Posts: 22
Originally Posted by cormanthor View Post
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...
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » if-then-else switch

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off