• Get second value from function
    4 replies, posted
Hi, I have made a function which returns two values. How can I get the second value from the function and only the second. In this example below I only want the b value, is this possible or do I have to get both and only use the one i want? function example() local a = 1 local b = 2 return a, b
local myA, myB = example()
It's also a common lua idiom to assign throw-away values to a variable named `_` local _, myB = example() If you need to get the second and only the second, you can create a throw-away table and index it funcThatUsesB(({example()})[2]) It's not nearly as pretty as just making a variable though.
depending on how much data you return from a function, at some point you might want to consider returning it as a table instead of multiple returns. I'd say once you hit more than 3 returns it's probably a good idea to use a table instead if you want to return anything else.
There's also the select function which allows you to select single values from a vararg return. local h = select(2, panel:GetSize())
Sorry, you need to Log In to post a reply to this thread.