Getting multiple values from a function without creating a variables in LUA

前端 未结 2 1515
孤街浪徒
孤街浪徒 2021-01-15 13:04

Is there any way to get several values from a function without creating variables for it?

local major, minor, revision, codename = love.getVersion() -- Get c         


        
2条回答
  •  醉话见心
    2021-01-15 13:35

    Here is the function signature: [https://love2d.org/wiki/love.getVersion] which just returns multiple value, if understand to achieve like what you are asking for, you can have wrapper around getVersion to have lua table returned either like below or in array format

    local function getVersion()
     local meta_data = {minor_version = "0.1", major_version = "1"}
     return meta_data
    end
    
    local res = getVersion()
    print ("minor_version: ", res['minor_version'])
    print ("major_version: ", res['major_version'])
    

提交回复
热议问题