Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Access one variable directly when more than one are returned — Gideros Forum

Access one variable directly when more than one are returned

totebototebo Member
edited May 2015 in General questions
When a function returns more than one variable:
function getVars()
   return var_a, var_b
end
Is it possible to get only var_b directly when calling the function? Something like:
print( getVars()[2] )

To avoid:
local var_1, var_2 = getVars()
print( var_2 )
Nic
My Gideros games: www.totebo.com

Comments

  • totebototebo Member
    edited May 2015
    Rewrote this question since it's more of a straight-up Lua question rather than Box2D specifically.
    My Gideros games: www.totebo.com
  • totebototebo Member
    Thanks @keszegh.

    To me it seems like the cleanest solution is to add a method that returns just one specific value;
    function getVarB()
       return var_b
    end
    My use case is getting the Y position of a Body in Box2D directly. I'm now extending the Body class with Body:getY().

    Happy days. :)



    My Gideros games: www.totebo.com
  • tkhnomantkhnoman Member
    edited May 2015
    function getVars()
       return {var_a, var_b}
    end
     
    print( getVars()[2] )
     
    local var_1, var_2 = unpack( getVars() )
  • hgy29hgy29 Maintainer
    What about something like that:
    print(table.pack(getVars())[2])
    Not that this is probably not efficient at all, but seems pretty much what you were looking for...
  • totebototebo Member
    Hey @hgy29, that would do it! You're right, doesn't feel like the "right" way of doing it. :)

    @tkhnoman, that also works, but not without changing the Box2D interface unfortunately.
    My Gideros games: www.totebo.com
Sign In or Register to comment.