Reply
 
Thread Tools Display Modes
Old 02-21-13, 10:45 PM   #1
Clamsoda
A Cobalt Mageweaver
Join Date: Nov 2011
Posts: 233
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?
Clamsoda is offline   Reply With Quote
Old 02-21-13, 10:47 PM   #2
semlar
An Onyxian Warder
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 378
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.
semlar is offline   Reply With Quote
Old 02-21-13, 10:49 PM   #3
Clamsoda
A Cobalt Mageweaver
Join Date: Nov 2011
Posts: 233
Nevermind, thank you.

Zzzzz -.-
Clamsoda is offline   Reply With Quote
Old 04-12-13, 10:27 AM   #4
Resike
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 465
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.
Resike is offline   Reply With Quote
Old 04-12-13, 01:13 PM   #5
p3lim
Mmmrrrggglll
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,143
Normal lua behavior, use (IsInInstance()) to only get the first.
p3lim is offline   Reply With Quote
Old 04-12-13, 01:17 PM   #6
Resike
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 465
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.
Resike is offline   Reply With Quote
Old 04-12-13, 01:33 PM   #7
Clamsoda
A Cobalt Mageweaver
Join Date: Nov 2011
Posts: 233
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.
Clamsoda is offline   Reply With Quote
Old 04-12-13, 01:37 PM   #8
Resike
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 465
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.
Resike is offline   Reply With Quote
Old 04-12-13, 01:46 PM   #9
SDPhantom
A Molten Giant
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 933
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))))
__________________
"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.
SDPhantom is offline   Reply With Quote
Old 04-12-13, 02:01 PM   #10
Resike
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 465
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)))"
Resike is offline   Reply With Quote
Old 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,016
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 Revelation, Spamalyzer, TravelAgent, Volumizer, and many other AddOns.
Torhal is offline   Reply With Quote
Old 04-12-13, 02:28 PM   #12
Clamsoda
A Cobalt Mageweaver
Join Date: Nov 2011
Posts: 233
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.
Clamsoda is offline   Reply With Quote
Old 04-12-13, 03:53 PM   #13
Resike
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 465
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.
Resike is offline   Reply With Quote
Old 04-12-13, 03:56 PM   #14
Nibelheim
local roygbi-
 
Nibelheim's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,490
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.
__________________
Nibelheim is offline   Reply With Quote
Old 04-12-13, 03:58 PM   #15
Clamsoda
A Cobalt Mageweaver
Join Date: Nov 2011
Posts: 233
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.
Clamsoda is offline   Reply With Quote
Old 04-12-13, 10:24 PM   #16
Resike
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 465
Select or not to select

So i shouldn't use select on "COMBAT_LOG_EVENT_UNFILTERED"?
Resike is offline   Reply With Quote
Old 04-12-13, 10:36 PM   #17
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 9,477
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

Seerah is offline   Reply With Quote
Old 04-12-13, 10:57 PM   #18
Nibelheim
local roygbi-
 
Nibelheim's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,490
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 = ...
__________________
Nibelheim is offline   Reply With Quote
Old 04-13-13, 12:06 AM   #19
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 9,477
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

Seerah is offline   Reply With Quote
Old 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,016
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 Revelation, Spamalyzer, TravelAgent, Volumizer, and many other AddOns.
Torhal is offline   Reply With Quote
Reply

Go BackWoWInterface » 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