Thread Tools Display Modes
02-21-13, 11:45 PM   #1
Clamsoda
A Frostmaul Preserver
Join Date: Nov 2011
Posts: 269
Issue with select() and IsInInstance()

Is there a reason why:

Lua Code:
  1. local inInstance, instanceType = IsInInstance()

returns BOOLEAN, TYPE

and

Lua Code:
  1. select(1, IsInInstance())

returns BOOLEAN TYPE together? Select(2, *) returns JUST the type, so why can't I return just the boolean?
  Reply With Quote
02-21-13, 11:47 PM   #2
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Because select returns all values after the one specified, as well.

If you just want the first value, just do "local inInstance = IsInInstance()".

There is a way to force it to only return one value, and that's by wrapping it in parentheses, but there's no reason to do that here.
  Reply With Quote
02-21-13, 11:49 PM   #3
Clamsoda
A Frostmaul Preserver
Join Date: Nov 2011
Posts: 269
Nevermind, thank you.

Zzzzz -.-
  Reply With Quote
04-12-13, 10:27 AM   #4
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by semlar View Post
There is a way to force it to only return one value, and that's by wrapping it in parentheses, but there's no reason to do that here.
I would be instrested with this force only the selected value. Is this some kinda new bug, i think it only returned the actually selected parameter.
  Reply With Quote
04-12-13, 01:13 PM   #5
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
Normal lua behavior, use (IsInInstance()) to only get the first.
  Reply With Quote
04-12-13, 01:17 PM   #6
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by p3lim View Post
Normal lua behavior, use (IsInInstance()) to only get the first.
Well obv. i want to use it on a different stuff like "print(select(2, GetWorldStateUIInfo(3)))" prints every parameter from the second one.
  Reply With Quote
04-12-13, 01:33 PM   #7
Clamsoda
A Frostmaul Preserver
Join Date: Nov 2011
Posts: 269
You need to wrap your function in parenthesis like so:

print((select(2, GetWorldStateUIInfo(3))))

If that doesn't work, try:

local _, state = GetWorldStateUIInfo(3)

Edit: Fixed a mistake.

Last edited by Clamsoda : 04-12-13 at 02:05 PM.
  Reply With Quote
04-12-13, 01:37 PM   #8
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Clamsoda View Post
You need to wrap your function in parenthesis like so:

print(select(2, (GetWorldStateUIInfo(3))))

If that doesn't work, try:

local _, state = GetWorldStateUIInfo(3)
Oh i see, thanks. Yeah i dont really wanna go back for the _, method.
  Reply With Quote
04-12-13, 01:46 PM   #9
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by Resike View Post
I would be instrested with this force only the selected value. Is this some kinda new bug, i think it only returned the actually selected parameter.
It's not a bug, it's even documented as doing such. See Select() under Official Lua 5.1 Manual §5.1.



Originally Posted by Resike View Post
Well obv. i want to use it on a different stuff like "print(select(2, GetWorldStateUIInfo(3)))" prints every parameter from the second one.
As mentioned earlier, encase the Select() call in parenthesis to only use the first return.
(The post by Clamsoda put them around the wrong function.)
Code:
print((select(2, GetWorldStateUIInfo(3))))
__________________
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 : 04-12-13 at 01:48 PM.
  Reply With Quote
04-12-13, 02:01 PM   #10
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by SDPhantom View Post
It's not a bug, it's even documented as doing such. See Select() under Official Lua 5.1 Manual §5.1.




As mentioned earlier, encase the Select() call in parenthesis to only use the first return.
(The post by Clamsoda put them around the wrong function.)
Code:
print((select(2, GetWorldStateUIInfo(3))))
And i need to double wrap is its already wrapped like that?

"tostring(select(2, GetWorldStateUIInfo(3)))"
  Reply With Quote
04-12-13, 02:26 PM   #11
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by Resike View Post
Oh i see, thanks. Yeah i dont really wanna go back for the _, method.
If you're doing something computationally expensive (such as an often-used loop or polling the combat log), you'll want to avoid using select() and use the placeholder "_" variable name.
__________________
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
04-12-13, 02:28 PM   #12
Clamsoda
A Frostmaul Preserver
Join Date: Nov 2011
Posts: 269
Yes. You need to wrap select() in its own parenthisis, THEN wrap that in tostring() -- or whatever function you are calling -- if you want to JUST return the 2nd argument of GetWorldStateUIInfo().

Code:
tostring( ( select(2, GetWorldStateUIInfo(3)) ) )

There really is no downside to using the "local _, state = GetWorldStateUIInfo(3)" approach, especially when you are only one argument deep into the function. It is faster, and you can just make "_" local in your file if your are worried about global leaks (though that was fixed).

Last edited by Clamsoda : 04-12-13 at 02:34 PM.
  Reply With Quote
04-12-13, 03:53 PM   #13
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Torhal View Post
If you're doing something computationally expensive (such as an often-used loop or polling the combat log), you'll want to avoid using select() and use the placeholder "_" variable name.
I don't really see why, since its still gonna overwrite _ variable all the time the event happens, and since you don't use it its just a waste of memory and processor time. And select only gets the parameters you'll need and use, but ofc calling the select itself is also uses resources.
  Reply With Quote
04-12-13, 03:56 PM   #14
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
Originally Posted by Resike View Post
I don't really see why, since its still gonna overwrite _ variable all the time the event happens, and since you don't use it its just a waste of memory and processor time. And select only gets the parameters you'll need and use, but ofc calling the select itself is also uses resources.
Calling Select uses a lot more resources than redefining a variable. Only really important if being called a lot though.
  Reply With Quote
04-12-13, 03:58 PM   #15
Clamsoda
A Frostmaul Preserver
Join Date: Nov 2011
Posts: 269
Calling the select() function is extremely slow. Besides, you are just recycling "_" over and over. It is one variable, there isn't much overhead associated with that.
  Reply With Quote
04-12-13, 10:24 PM   #16
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Select or not to select

So i shouldn't use select on "COMBAT_LOG_EVENT_UNFILTERED"?
  Reply With Quote
04-12-13, 10:36 PM   #17
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
oooh no.
__________________
"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
04-12-13, 10:57 PM   #18
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
On a related note, I saw this in an AddOn I downloaded a while back:
Code:
local var1, var2 = select(1, ...)
Would this not be the same as:
Code:
local var1, var2 = ...
  Reply With Quote
04-13-13, 12:06 AM   #19
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Yes. No offense to whomever's addon that was, but doing that is just dumb.
__________________
"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
04-13-13, 02:17 AM   #20
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by Resike View Post
So i shouldn't use select on "COMBAT_LOG_EVENT_UNFILTERED"?
That would be "polling the combat log" so definitely no
__________________
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

WoWInterface » Developer Discussions » Lua/XML Help » Issue with select() and IsInInstance()

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