1
votes

How to pass table in lua function which is optional.

For example

function test(options)
   local a = options.a
end

this function should work as both

test(options)

and

test()
1

1 Answers

5
votes
function test(options)

  options = options or {}
  local a = options.a or 0 -- or whatever it defaults to

end

You simply or the optional values with their default value. If the value has not been provided and hence is nil it will resolve to ored value.

This is a shorter version of

function test(options)
  if not options then
    options = {}
  end
  local a = 0
  if options.a then
    a = options.a    
  end
end