Differences Between Basic AS3 and Lua (Corona SDK) Part 1

Seeing how the market has shifted to mobile gaming and how Flash has a long way to go before it is a good option for iOS development I have been looking into alternatives to make mobile apps. A friend of mine recommended Corona SDK so I’ve put some time in with Corona and I’m very happy I did.

Let me give you an idea of my skillset. I’m a self taught programmer who mainly uses movie clips and timelines to make my games in Flash. I am not one to use Flex or Flash Builder to set up my projects, I like to see things on the timeline. I learned to program in AS1, switched to AS2, and held out using AS2 for a long time because AS3 was confusing to me, finally I learned AS3 and my skills have slowly improved over the years. I don’t know any other programming languages other than Actionscript, PHP and HTML, basically stuff I need to know for web design.

I’m about a month or so into using Corona and I’ve been able to pick up on it pretty fast. There are some major differences between Lua and AS3 so I’m compiling into a list for other Flash devs who follow in my footsteps. I was scratching my head a few times and there wasn’t really anything out there that I could find that put the differences in plain English. So I’m going to take a stab at it.

Here are some key areas that you might find confusing, I’m going to try to explain them from a AS2-AS3 developer perspective.

File Structure:

With Corona SDK you will always start with a “main.lua” file. This is the starting point for all your coding. You can break up your code into several files to try to keep things neat and tidy by using “require(‘filename’)” this is covered fairly clearly in the documentation of Lua.

You can use the structure defined here to build multiple scenes into your game: http://techority.com/2010/11/19/how-to-use-scenesscreens-in-corona/

However if you to use this be sure to create your global variables in “main.lua” if you want to access them everywhere else. Otherwise your code might error out looking for a variable.

If Statement Structure:

If you are trying to learn Lua you more than likely already noticed this.  If statements don’t need parentheses and they don’t use brackets to enclose the statement.

so this…

if ( !myVar) {
trace(‘Hello, World!’);
}

becomes this…

if not myVar then
print(‘Hello, World!’)
end

Logical Operators:

If statements wouldn’t be very useful without the ability to check multiple things at once.  Remember that Lua uses words rather than symbols for most of these.  So “&&” becomes “and” , “||” becomes “or”, and “!” becomes “not”.    There are several other differences on this so if you want to read about them go here.  http://lua-users.org/wiki/ExpressionsTutorial

Loop structure

Not So Random Numbers:

Random numbers in Flash always seem at least fairly random. However on several devices random numbers are always the same sequence based off a default seed number. You need to change the seed for your random numbers to make sure the numbers are more random. So using a number that will always be different for a seed, like the current time and date will make your random numbers works better.

math.randomseed( os.time() )

Read more here… http://lua-users.org/wiki/MathLibraryTutorial

Missing functions:

This is irksome, things you take for granted in AS3 are missing.  There are quite a few that are either missing or are called by another name.  Here is a quick list of the ones that I’ve come across that I really miss.

Array Funtions:

Arrays in Lua are called tables, and several of the functions that you are used to as a flash developer are not available in Lua.

First off to make a table you simply do this:

myTable = { }

or

myTable = {1,2,3,4 }

or

myTable = {name = value, name = value}

Your table is ready to use. But you don’t have the same tools at your disposal as in Actionscript. You have nothing that works like “pop()” or “splice()”. Here are a few functions you need to be familiar with to work with tables.  Notice that you do not call the name of the table then insert to insert into a table.  You tell the function what table is effected by passing the table name as an parameter.

table.insert( myTable, #)

table.remove( myTable, #)

Here is a link to a pretty good writeup on tables: http://lua-users.org/wiki/TablesTutorial

Comparing tables seems to not work the same way as in actionscript.  I might be doing it wrong but comparing

Continue:

Need to skip to the next itteration of the loop that you are currently running? Lua does not offer this functionality.

Switch statements:

You can’t use switch statements in Lua, you have to either use a massive if else statement, or a table. Here is a link to a better explanation:
http://lua-users.org/wiki/SwitchStatement

There are quite a few other things that I want to point out but this post is getting rather long so I’m going to make it a multiple part post.  Look for more information in the near future in part 2.