user2136963
user2136963

Reputation: 2606

How to parse function arguments to a variable in Lua (Corona SDK)?

For example, I have a function:

myFunction = function(a1,a2,a3)
end;

And I want to save all the arguments given myFunction inside it by a code which will be correct after changing number of myFunction arguments and their names. It seems to me that it can be done by a for cycle, but I don't know how to call arguments and #arguments in it.

Upvotes: 2

Views: 453

Answers (1)

Egor Skriptunoff
Egor Skriptunoff

Reputation: 23737

local saved_arguments

myFunction = function(...)
  -- save the arguments
  saved_arguments = {...}
  local a1, a2, a3 = ...
  -- main code of function
end;

-- Use saved arguments
local last_a1, last_a2, last_a3 = unpack(saved_arguments)
-- do something with last_a1, last_a2, last_a3
-- or use it directly: saved_arguments[1], saved_arguments[2], saved_arguments[3]

Upvotes: 2

Related Questions