Thread Tools Display Modes
10-26-09, 06:03 PM   #1
AJSTITAN14
A Murloc Raider
Join Date: Oct 2009
Posts: 8
Function returns from GetSpellInfo, GetItemInfo UnitAura etc...

These are functions that return quite a few variables. Do I need to create variables for each value the function can return? If not how do I specify which piece of data I want from the function? Can I do it by referring to the specific returns I want to look at?

For instance:

icon, caster = GetSpellInfo(6774);
  Reply With Quote
10-26-09, 08:47 PM   #2
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
All of the return values are returned, and the naming of the variables which collect them has no influence upon which are retrieved. If you call a function which has four return values and are only interested in the second and fourth, you would do this:

Code:
local _, thing_name, _, thing_desc = MakeBelieveFunction()
From this, thing_name would be the second return value, and thing_desc would be the fourth. Using the underscore is simply a common usage for a "throwaway" variable. It could just have easily been named "throwaway".
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
10-26-09, 11:25 PM   #3
lilsparky
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2007
Posts: 117
i'd just add that ignoring return values doesn't hurt you in any way. that is, if a function has 5 returns and you only need the 1st and 3rd, you could do:

local valueA, _, valueB = functionCall()
  Reply With Quote
10-27-09, 12:36 AM   #4
Shirik
Blasphemer!
Premium Member
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2007
Posts: 818
Originally Posted by lilsparky View Post
i'd just add that ignoring return values doesn't hurt you in any way. that is, if a function has 5 returns and you only need the 1st and 3rd, you could do:

local valueA, _, valueB = functionCall()
Actually it does hurt you slightly. The opcodes generated in this case requires a little additional work -- that is, the work of popping 2 values off the stack to ensure 3 returns. Of course, this is a minuscule difference and is probably not even testable.
__________________
たしかにひとつのじだいがおわるのお
ぼくはこのめでみたよ
だけどつぎがじぶんおばんだってことわ
しりたくなかったんだ
It's my turn next.

Shakespeare liked regexes too!
/(bb|[^b]{2})/
  Reply With Quote
10-27-09, 12:49 AM   #5
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
If you want just one value you can use this:
lua Code:
  1. select(3, GetSpellInfo(6774))

If you want for example the fifth returned value from GetSpellInfo change 3 with 5.
  Reply With Quote
10-27-09, 01:21 AM   #6
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
You never want to use select() if you will be retrieving the single value multiple times - it incurs unnecessary overhead due to the function call, plus select() itself isn't very efficient.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
10-27-09, 06:59 AM   #7
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
Select loops through the given parameter with an iterator until it has arrived at the nth parameter given to select.
  Reply With Quote
10-27-09, 09:23 AM   #8
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
select() also doesn't just return that one value. It returns that one plus all after.

Code:
local var3, var4, var5 = select(3, functionwith5values())
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Function returns from GetSpellInfo, GetItemInfo UnitAura etc...


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