Category Archives: Code

ProTip for Corona: Finding Typos

One of the most common mistakes in programming is typos.   Your brain wants to type one thing and your fingers another.

In Corona (and Lua in general), variables aren’t declared explicitly before you use them.  When  Lua sees a variable name that you haven’t declared as local, it starts looking up the chain of scopes until it reaches the Global namespace (_G).  If it gets there, it creates a key in the _G table with your variable name.

This little trick (shown to me by my friend Chris Blackwell) makes Corona tell you whenever this happens.  Sometimes it’s fine, but most of the time you’ll find a typo (as I did in the Particle Candy library).   I even found that loading the built-in JSON library causes it to check for a global called “Chipmunk.”  Granted, Chipmunk isn’t a typo, but it is accessing a variable that doesn’t exist.  This can happen if you’re check to see if something has been loaded or not.  In those cases this warning can be ignored.

So, put this in your app’s main.lua, run your app in the simulator and watch the console output.  You might just find a problem you didn’t know you had.

Note: You only want this in debug builds.  You could wrap it with a check for running on the simulator, but the best bet is to just remove it before building your final version.

local globalsmeta = {
__index = function(table, key)
print("!!! access to nonexistent global variable "..tostring(key).."\n"..debug.traceback())
end
}
setmetatable(_G, globalsmeta)

This works by adding a metatable to the Global namespace to call this function whenever you access an unknown key in its table.

 

CoronaSDK: Building and installing on a Kindle Fire

Amazon's Kindle Fire

  1. Build the app using  File > Build > Amazon/Kindle…
  2. On the Fire, go to Settings and tap More
  3. Tap Device
  4. Turn ON “Allow Installation of Applications — from unknown sources”
  5. Plug in a Micro-B usb cable into the Fire and the other into your computer (in this case a MacBook Air)
  6. The Kindle should be showing up on your desktop
  7. Copy the .apk file generated by Corona onto the Kindle (I’m not sure that it makes a difference where — I used the “downloads” folder)
  8. Eject the Kindle from the Mac
  9. On the Kindle you’ll need a File management application installed (I used “ES File Explorer” because it was free and had the most reviews)
  10. Use the File manager to find your app and tap it to install it
  11. Profit!

CoronaSDK: How to know if a file exists?

Update: Dejay was correct, you have to try and open the file to know for sure if it’s there or not.

Want to know if a file exists before you use it?

Lately, I’ve been wanting something a little more dynamic in my projects for opening various Scenes (think Director, but not).   I needed a way of looking to see if a file existed or not before I tried to load it up.  Here’s the result:

function fileExists(fileName, base)
  assert(fileName, "fileName is missing")
  local base = base or system.ResourceDirectory
  local filePath = system.pathForFile( fileName, base )
  local exists = false
 
  if (filePath) then -- file may exist. won't know until you open it
    local fileHandle = io.open( filePath, "r" )
    if (fileHandle) then -- nil if no file found
      exists = true
      io.close(fileHandle)
    end
  end
 
  return(exists)
end

Typical usage:

if fileExists("myGame.lua") then
  -- do something wonderful
end

If the file does not exist, the CoronaSDK library function system.pathForFile() returns nil.  It also returns a Warning in the console, which can be safely ignored.

By default it checks your app’s asset directory (system.ResourceDirectory), however, if you’d like to check if a file you created exists you can pass in an alternative base path like so:

if fileExists("some_file.txt", system.DocumentsDirectory) then
  -- do something wonderful
end

The three valid options for base path are:

  1. system.ResourceDirectory
  2. system.DocumentsDirectory
  3. system.TemporaryDirectory