Author Topic: API optional parameters: error using nil  (Read 36 times)

llamazing

  • Full Member
  • ***
  • Posts: 139
    • View Profile
API optional parameters: error using nil
« on: November 11, 2018, 03:08:49 am »
I tried making a new surface and got a strange error:

bad argument #1 to create (number, string or no value expected, got nil)

The only way I've found to get around it is the following, but it's a bit awkward:
Code: Lua
  1. local surface = width and sol.surface.create(width, height) or sol.surface.create()

It's a bit strange that there'd be a distinction between a value of 'nil' and 'no value' when it comes to specifying optional values. Is there a better way to get around it while working purely in lua? i.e. is there a way to assign 'no value' to a lua variable?

For context, the value of width is being specified by a data file, and I'm wanting to use default values if the width is omitted in the data file.