WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Issue with select() and IsInInstance() (https://www.wowinterface.com/forums/showthread.php?t=45869)

Clamsoda 02-21-13 11:45 PM

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?

semlar 02-21-13 11:47 PM

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.

Clamsoda 02-21-13 11:49 PM

Nevermind, thank you.

Zzzzz -.-

Resike 04-12-13 10:27 AM

Quote:

Originally Posted by semlar (Post 273321)
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.

p3lim 04-12-13 01:13 PM

Normal lua behavior, use (IsInInstance()) to only get the first.

Resike 04-12-13 01:17 PM

Quote:

Originally Posted by p3lim (Post 276334)
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.

Clamsoda 04-12-13 01:33 PM

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.

Resike 04-12-13 01:37 PM

Quote:

Originally Posted by Clamsoda (Post 276338)
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.

SDPhantom 04-12-13 01:46 PM

Quote:

Originally Posted by Resike (Post 276320)
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.



Quote:

Originally Posted by Resike (Post 276335)
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))))

Resike 04-12-13 02:01 PM

Quote:

Originally Posted by SDPhantom (Post 276340)
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)))"

Torhal 04-12-13 02:26 PM

Quote:

Originally Posted by Resike (Post 276339)
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.

Clamsoda 04-12-13 02:28 PM

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).

Resike 04-12-13 03:53 PM

Quote:

Originally Posted by Torhal (Post 276343)
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.

Nibelheim 04-12-13 03:56 PM

Quote:

Originally Posted by Resike (Post 276352)
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.

Clamsoda 04-12-13 03:58 PM

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.

Resike 04-12-13 10:24 PM

Select or not to select
 
So i shouldn't use select on "COMBAT_LOG_EVENT_UNFILTERED"?

Seerah 04-12-13 10:36 PM

oooh :( no.

Nibelheim 04-12-13 10:57 PM

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 = ...

Seerah 04-13-13 12:06 AM

Yes. No offense to whomever's addon that was, but doing that is just dumb. ;)

Torhal 04-13-13 02:17 AM

Quote:

Originally Posted by Resike (Post 276379)
So i shouldn't use select on "COMBAT_LOG_EVENT_UNFILTERED"?

That would be "polling the combat log" so definitely no :)


All times are GMT -6. The time now is 01:53 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI