It’s Wednesday and time for a July 4th FAQ session. Here are five frequently asked questions (FAQ).
1. What is the difference between ‘require(“file”)’ and ‘require “file” ‘?
You may have see the “require” API written two different ways.
Normally functions require arguments to be passed to function as in the first example. But there is an exception offered in Lua: If the function has one single argument and that argument is either a literal string or table constructor, then the parentheses are optional. This works for print statements or any other function that accepts one argument if either a table or string.
2. We get a lot of questions about Corona complaining that a variable, index or function is “nil” and saying there must be a bug in Corona because I know I set the variable to a value.
“nil” errors are the most common problems when you program in Lua (Corona’s programming language). Unlike other languages, Lua doesn’t require you to declare a variable before you use it. If you don’t assign a value to a variable, it assumes the value nil.
The general cause of nil errors is variables or functions “out of scope.” If you define your variables and functions with the “local” keyword to conserve memory, you will run into this error from time to time. The scope of local variables is limited to the Lua file where it’s defined (e.g. main.lua). If you try to access a local variable in a different Lua file, it will be nil. Also, you can only reference a local variable after it’s defined. If you try to access the variable before it’s define in the Lua file, you will get a nil error.
I generally define local variables at the top of my Lua file so they can be accessed later. Just be sure to not use the “local” statement in front of the variable elsewhere in your Lua file or you will be defining a totally different variable.
The timer.performWithDelay calls the addNumbers function in an execution code “chunk” after addNumbers function has been defined. It still needs the local addNumbers statement to access the pointer to the function.
One tip is to remove “local” from the variable or function to see if it’s a scoping issue. It won’t help resolve the problem where you try to call a function before it’s defined. Another tip to help understand and resolve nil errors is the use of print statements. You can add print statements in your code to determine if a variable or function is nil (and out of scope). Use commas to print out variables and functions. Printing variables with commas will display nil values as well as function and table addresses.
3. Where do I find documentation on Facebook interface?
Corona’s Facebook interface uses the Facebook Graph API. You can find the interface information on the Corona Facebook documentation here and the Facebook parameters on the
Facebook API page. Also check out Jonathan Beebe’s great Facebook tutorials: Implementing Facebook Single Sign-on and Uploading Photos to Facebook. Remember to check out the Facebook sample app included in the Corona SDK.
4. I’m trying to load an image by adding the subdirectory name to the baseDirectory but it doesn’t work. What am I doing wrong?
The baseDirectory argument used in many of the Corona APIs is there to specify the base directory for the assets. This must be one of predefined constants: system.ResourceDirectory, system.TemporaryDirectory, system.DocumentsDirectory. If none is specified, system.ResourceDirectory is used.
If you are trying to access an asset in a subdirectory, you must append the subdirectory name to the file name and not to the baseDirectory. Here are a few examples:
Note that you use slash “/” as a separator for the subfolder name instead of dots “.” used in require. The dot command is only used in require. Slashes are used everywhere else.
5. This is not really a question but a feature of Lua that makes it unique. The ability to return multiple values from a function.
Lua is one of the few languages that allows you to return multiple values from a function. This can come in handy when you need to return multiple values like an error status and error message. You will find a number of Lua functions take advantage of this feature.
You can use this feature in your own functions too.
That’s it for today’s questions. I hope you enjoyed it and even learned a few things.
Have a great 4th of July.