Thread Tools Display Modes
09-18-08, 10:30 PM   #1
Ne0nguy
A Fallenroot Satyr
 
Ne0nguy's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 22
Need help simplifying this

I am trying to create a function that returns True if the player can invite a player to a group or raid but false any other time.

I was able to come up with something but there has to be a way to simplify it.. can anyone give me suggestions?

Code:
function CanInvite()
	if(GetNumRaidMembers() > 0 and IsRaidLeader()) then
		return True;
	elseif(GetNumPartyMembers() > 0 and IsPartyLeader()) then
		return True;
	elseif(GetNumPartyMembers() == 0 and GetNumRaidMembers() == 0) then
		return True;
	else
		return False;
	end
end
  Reply With Quote
09-19-08, 12:15 AM   #2
Tekkub
A Molten Giant
 
Tekkub's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 960
Code:
local function CanInvite()
	return GetNumRaidMembers() == 0 and (GetNumPartyMembers() == 0 or IsPartyLeader()) or IsRaidLeader()
end
No real point in wasting the memory for a closure, just stick the logic into your if call directly.
  Reply With Quote
09-19-08, 12:19 AM   #3
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Try this... It's the only way I can think of to shorten it.

Code:
function CanInvite()
	return (GetNumRaidMembers() > 1 and IsRaidLeader()) or (GetNumPartyMembers() > 0 and IsPartyLeader()) or (GetNumPartyMembers() <= 0 and GetNumRaidMembers() <= 1);
end
What this does is turn all of the conditions into a single boolean comparison statement. If any of the conditions are true, it'll return so, else it'll be false.



Note: I corrected a couple things, mainly GetNumRaidMembers() does include the player as well, so you need to check if this is greater than 1, not 0.

Edit: Also to note the last condition checking if not in a party or raid has been corrected as well.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 09-19-08 at 12:21 AM.
  Reply With Quote
09-19-08, 08:37 AM   #4
Ne0nguy
A Fallenroot Satyr
 
Ne0nguy's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 22
Thanks for the help guys.
Tekkub's example did exactly what I needed it to do.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Need help simplifying this


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