<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Ludicrous Software]]></title>
  <link href="http://www.ludicroussoftware.com/atom.xml" rel="self"/>
  <link href="http://www.ludicroussoftware.com/"/>
  <updated>2012-05-17T09:13:19-05:00</updated>
  <id>http://www.ludicroussoftware.com/</id>
  <author>
    <name><![CDATA[Darren Osadchuk]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Simple app to ipa script]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2012/05/17/simple-app-to-ipa-script/"/>
    <updated>2012-05-17T08:54:00-05:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2012/05/17/simple-app-to-ipa-script</id>
    <content type="html"><![CDATA[<p>I have a client who asks for .ipa files of test builds, but the Corona SDK (among others) creates .app files. The process of creating an .ipa from an .app file is pretty simple, but I wanted to automate it a little more. The result is this simple little bash script.</p>

<!-- more -->


<p>As you may know, the process of creating an .ipa file from an .app file looks like this:</p>

<ol>
<li>Create a directory called &#8220;Payload&#8221; (case sensitive).</li>
<li>Copy/move your .app file into that directory.</li>
<li>Zip the directory.</li>
<li>Rename it to &#8220;whatever.ipa&#8221;.</li>
</ol>


<p>This is all pretty simple, but if you do this via Finder it can get pretty tedious if you have to do it very often - for example, one current client requires separate localized builds of the app I&#8217;m working on. So for them, I need to repeat those steps for the English and the French versions (thankfully in this context, Canada only has two official languages). That&#8217;s a lot of clicking around with the mouse.</p>

<p>To automate the process, I whipped up this simple little bash script:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c">#!/bin/bash</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="o">[</span> -d <span class="s2">&quot;Payload&quot;</span> <span class="o">]</span>; <span class="k">then</span>
</span><span class='line'><span class="k">  </span>rm -fr Payload/*
</span><span class='line'><span class="k">else</span>
</span><span class='line'><span class="k">  </span>mkdir Payload
</span><span class='line'><span class="k">fi</span>
</span><span class='line'><span class="c"># if you&#39;d rather copy the .app file, then replace the next line with:</span>
</span><span class='line'><span class="c"># cp -r $1 Payload</span>
</span><span class='line'>mv <span class="nv">$1</span> Payload
</span><span class='line'>zip -r <span class="nv">$2</span>.ipa Payload
</span><span class='line'>rm -fr Payload
</span></code></pre></td></tr></table></div></figure>


<p>To set this up on your system:</p>

<ol>
<li><p>Copy and paste the script above into an empty text file. Save it as apptoipa.sh (or another name of your choosing).</p></li>
<li><p>Open up Terminal.app and navigate to the directory where you saved the script.</p></li>
<li><p>Make the script executable: <code>chmod +x apptoipa.sh</code> should do it (if you used TextMate for step 1, the Shell Script bundle has a command to make the script executable).</p></li>
<li><p>You&#8217;re all set</p></li>
</ol>


<p>The script requires two arguments: the name of the .app file, and the name of the resulting .ipa file. For the latter, don&#8217;t include the .ipa extension, so usage would look like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>Behemoth:Desktop darren<span class="nv">$ </span>/path/to/script/apptoipa.sh MyFabulousApp.app App<span class="se">\_</span>1<span class="se">\_</span>0<span class="se">\_</span>45
</span></code></pre></td></tr></table></div></figure>


<p>A few things to bear in mind:</p>

<ul>
<li>This script will delete any existing Payload directory.</li>
<li>It will also delete the Payload directory after the .ipa file has been created.</li>
<li>It moves the .app file into Payload instead of copying it, so your .app file WILL BE DELETED. If you want to preserve the .app file, see the comments in the script. I haven&#8217;t tested it, but it ought to work.</li>
</ul>


<p>This script is very straightforward and doesn&#8217;t do any checking for required arguments or anything like that, so exercise caution, use at your own risk, and do not use while driving.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Remove Unused Libraries From Corona Apps]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2012/05/08/remove-unused-libraries-from-corona-apps/"/>
    <updated>2012-05-08T09:43:00-05:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2012/05/08/remove-unused-libraries-from-corona-apps</id>
    <content type="html"><![CDATA[<p>Ansca has incorporated a lot of libraries into the Corona SDK that make it very easy to add social, analytics, and other features to your app. The downside is that all these great features are still bundled into your app during the build process, even if you don&#8217;t use them. Ideally, Corona would exclude these unused libraries, but currently it does not. However, if you&#8217;re building for Android, it&#8217;s possible to remove them from the apk file.</p>

<!-- more -->


<p>Doing so will trim approximately 2.5 MB from the file, which is a pretty substantial savings.</p>

<p>(<strong>Update</strong>: it turns out that the bulk of the savings comes from simply rebuilding the apk file using the tools in the SDK. This was pointed out by a <a href="http://developer.anscamobile.com/forum/2012/05/08/removing-unused-libraries-android-builds#comment-105576">commenter in a thread on Ansca&#8217;s forums</a>. So I tried it with Poker Solitaire. Simple repackaging the apk decreased the total size by about 2.1 MB, so it turns out that the libraries actually take up a pretty small proportion of the apk. So if you&#8217;re at all leery about deleting libraries from your app, you can simple rebuild the app and still reap a substantial reduction in file size!)</p>

<p>It can also help you reassure clients who may wonder why all this extraneous stuff is being included in their app (I have had to answer this question for enterprise clients who are using Corona-built apps internally). To recoup this space, there are a few prerequisites:</p>

<ol>
<li>You need to have the <a href="http://developer.android.com/sdk/index.html">Android SDK</a> installed;</li>
<li>You need to have the <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java JDK</a> installed;</li>
<li>You need to be at least somewhat comfortable with the command line.</li>
</ol>


<p>I&#8217;ve tested this in OS X using builds of both my own apps and client apps. I see no reason why it wouldn&#8217;t work in Windows, but I haven&#8217;t tested it. The resulting apps have installed and worked fine for me in all cases, but please bear in mind that you&#8217;re doing this at your own risk and I&#8217;m not responsible for anything at all that you care to do with the following information. So let&#8217;s get started:</p>

<h2>First Things First</h2>

<p>You need an apk generated by Corona, so build your app as you normally would.</p>

<p>Next, open up Terminal.app and navigate to the directory containing your apk file. The apk is essentially a zip archive, so you could simply change the extension and unzip it. If you do this and examine the results, you&#8217;ll notice that the manifest file is in a binary format - this is done as part of the build process. Since we need to edit that file, we need it in plain text. Instead of simply unzipping the file, we&#8217;re going to use <code>apktool</code>, which is part of the Android SDK, to &#8216;decode&#8217; the apk file. Enter this at the command line - I&#8217;m using my Poker Solitaire game in this example, so obviously replace that with the name of your apk file as you follow along - and you should see the resulting output:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ apktool d PokerSolitaire.apk
</span><span class='line'>I: Baksmaling...
</span><span class='line'>I: Loading resource table...
</span><span class='line'>W: Skipping "android" package group
</span><span class='line'>I: Loaded.
</span><span class='line'>I: Loading resource table from file: /Users/darren/apktool/framework/1.apk
</span><span class='line'>I: Loaded.
</span><span class='line'>I: Decoding file-resources...
</span><span class='line'>I: Decoding values*/* XMLs...
</span><span class='line'>I: Done.
</span><span class='line'>I: Copying assets and libs...</span></code></pre></td></tr></table></div></figure>


<p>If apktool isn&#8217;t in your path, you&#8217;ll need to enter the full path, which will look something like this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ \path\to\AndroidSDK\platform-tools\apktool</span></code></pre></td></tr></table></div></figure>


<p>This will create a directory called &#8216;PokerSolitaire&#8217;, which will contain the following files and directories:</p>

<p><img src="http://www.ludicroussoftware.com/images/2012/05/08/AndroidAppStructure.png" title="Android App Structure" ></p>

<p>The first thing to do is open up the <code>AndroidManifest.xml</code> file in your favourite text editor and remove some entries. Basically, you need to remove anything that looks like it&#8217;s related to Papaya Mobile, Open Feint, or Super Rewards. These will be entries where the <code>android:name</code> attribute starts with <code>com.papaya</code>, <code>com.openfeint</code>, or <code>com.adknowledge</code>. Here are a few by way of example - this is not an exhaustive list:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>&lt;activity android:theme="@style/OFNestedWindow" android:label="NativeBrowser" android:name="com.openfeint.internal.ui.NativeBrowser" android:configChanges="keyboardHidden|orientation" /&gt;
</span><span class='line'>&lt;provider android:name="com.papaya.social.PPYSocialContentProvider" android:authorities="com.ludicroussoftware.test.PokerSolitaire.ppy_cache" /&gt;
</span><span class='line'>&lt;activity android:name="com.adknowledge.superrewards.ui.activities.SRPaymentMethodsActivity" android:screenOrientation="portrait" /&gt;</span></code></pre></td></tr></table></div></figure>


<p>You can also delete any entries that start with <code>com.zong</code>. I haven&#8217;t actually investigated what these do - they seem to be related to Super Rewards - but I was able to safely delete them. Once you&#8217;ve got them all, save the file.</p>

<p>Next, we&#8217;ll delete the actual libraries. Expand the <code>smali\com</code> directory, and you should see something like the screenshot above. Delete directories for any libraries that you&#8217;re not using. Poker Solitaire uses none of these, so I was able to safely delete the <code>adknowledge</code>, <code>facebook</code>, <code>flurry</code>, <code>inmobi</code>, <code>inneractive</code>, <code>openfeint</code>, <code>papaya</code>, and <code>zong</code> directories. I also deleted the <code>zongfuscated</code> directory with no ill effects. Obviously, if you are using any of these services, do not delete them.</p>

<h2>Rebuild the apk</h2>

<p>Back in the Terminal, here&#8217;s what you need to do:</p>

<p>1. Build the apk file using apktool:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>Behemoth:Desktop darren$ apktool b PokerSolitaire PokerSolUnsigned.apk
</span><span class='line'>I: Checking whether sources has changed...
</span><span class='line'>I: Smaling...
</span><span class='line'>W: Unknown file type, ignoring: PokerSolitaire/smali/.DS_Store
</span><span class='line'>I: Checking whether resources has changed...
</span><span class='line'>I: Building resources...
</span><span class='line'>I: Copying libs...
</span><span class='line'>I: Building apk file...</span></code></pre></td></tr></table></div></figure>


<p>2. Sign the apk using the <code>jarsigner</code> tool that&#8217;s part of the Java JDK. You&#8217;ll also need your keystore and key alias that you used to sign the app, as well as the password. The key alias is the last argument passed to <code>jarsigner</code>:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>Behemoth:Desktop darren$ jarsigner -verbose -keystore ~/Dropbox/LudicrousRelease.keystore ~/Desktop/PokerSolUnsigned.apk ludicrousRelease
</span><span class='line'>Enter Passphrase for keystore: </span></code></pre></td></tr></table></div></figure>


<p>After you enter your passphrase, you&#8217;ll see a whole bunch of &#8216;signing:&#8217; messages zoom by; once it&#8217;s all done, you have a signed apk file.</p>

<p>3. The last step is to <code>zipalign</code> the apk file:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>Behemoth:Desktop darren$ zipalign -v 4 PokerSolUnsigned.apk PokerSol.apk</span></code></pre></td></tr></table></div></figure>


<p>And you&#8217;re all done. You can test your app as you normally would, upload the Android Market, etc.</p>

<h2>But Wait, There&#8217;s More!</h2>

<p>If you poke around in the directory created by <code>apktool</code>, you&#8217;ll notice a bunch of other files related to Papaya and Open Feint, mainly. There are all kinds of image files in the <code>res</code> directory, as well as a bunch of entries in the various xml files in <code>res/values</code>. I tried going for a wholesale removal of all of these things, which resulted in the build process breaking. I haven&#8217;t yet figured out why that&#8217;s happening. The completist in me would like to figure it out, but these images add up to a small percentage of the total space taken up by all of these libraries, so it&#8217;s not a huge deal if they&#8217;re left in the app. It&#8217;s bothersome, but at a certain point the law of diminishing returns kicks in and I just need to Let It Go.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Corona Bunnymark Performance]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2012/03/09/corona-bunnymark-performance/"/>
    <updated>2012-03-09T09:57:00-06:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2012/03/09/corona-bunnymark-performance</id>
    <content type="html"><![CDATA[<p>Last week I ported Iain Lobb&#8217;s Bunnymark performance test to Corona, as I was curious to see how it would perform. I posted the results on Twitter, but thought that I&#8217;d follow up with a post showing the Bunnymark code, and also talk about how the results have changed after testing again using the graphics optimizations recently announced by Ansca.</p>

<!-- more -->


<p>First off, I&#8217;ve posted the basic Bunnymark code to GitHub. Included in it is a simple FPS counter that I put together. If you look at the code for the counter, you&#8217;ll see that you can specify a &#8216;target&#8217; frame rate, and the counter will dispatch an event when the frame rate drops below that target. This makes it a bit easier to stop any particular code from running if the FPS drops too low. Also, to account for any temporary dips in the frame rate (e.g. by creating a couple hundred display objects in a single frame) it tracks the frame rate as an average of the rate over the last 60 frames.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[PlayBook Sales/Downloads]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2012/02/21/playbook-stats/"/>
    <updated>2012-02-21T14:12:00-06:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2012/02/21/playbook-stats</id>
    <content type="html"><![CDATA[<p>I have two PlayBooks apps available in BlackBerry&#8217;s App World: <a href="http://appworld.blackberry.com/webstore/content/23622/?lang=en">Poker Solitaire</a> (available for $0.99) and <a href="http://appworld.blackberry.com/webstore/content/51599/?lang=en">Marblous</a> (a free app). I recently noticed a bit of a spike in sales of the former and downloads of the latter, and while I was looking at the numbers, I realized that I&#8217;ve never done a sales/stats post. So here&#8217;s a sales/stats post. The numbers for both apps are overall pretty low (I&#8217;m not retiring off this income), but I think what&#8217;s interesting is the trend that they both indicate, which is that the market for PlayBook apps seems to be growing.</p>

<!-- more -->


<p>The bulk of my income is in client work, so my main goal in developing/distributing these apps wasn&#8217;t necessarily the income they would generate. I don&#8217;t do any marketing, unless you want to count as marketing a couple of mentions on Twitter that I&#8217;d released the games (but you probably shouldn&#8217;t). So these numbers can perhaps be seen as what happens if you create a game and then essentially neglect it (but you probably shouldn&#8217;t).</p>

<p>I ported Poker Solitaire to the PlayBook because it was a good exercise in familiarizing myself with AIR development for the device (and because I like the game). Marblous is a very straightforward collapse clone. I&#8217;d originally created it because I wanted to figure out the logic for finding matching neighbours - this was back in about 2006 when I was doing Flash Lite 1.1 work. (Side note: the algorithm had to execute over the course of multiple frames because Flash Lite was pretty slow on a Nokia 6620 - I also had to walk uphill in the snow to and from the mobile phone store; you kids these days have it easy). Then early last year, during the brief period of time when I was under contract to write a book on AIR for Android game development, I dusted off the code and ported it to AS3 with the intent of using it in the book. Ultimately, I never did finish the book, but I had the completed game code sitting around, and figured that since at least something should come out of all that work, I released the game for free for the PlayBook.</p>

<p>So all that said, here&#8217;s the sales chart for Poker Solitaire, broken down by week (Excel is the one that figured there are 53 weeks in a year, for what that&#8217;s worth). The game was available at the launch of the PlayBook - note the anemic launch sales. I&#8217;d have expected at least a moderate bump at launch, given the small size of the app market for the PlayBook at launch:</p>

<p><img src="http://www.ludicroussoftware.com/images/2012/02/21/PokerSolSales.png" title="&#34;Poker Solitaire Sales&#34;" alt="&#34;Poker Solitaire Sales&#34;"></p>

<p>The sales are pretty inconsistent, but overall there&#8217;s an uptick starting in week 53 of 2011, which would of course be right after Christmas. As far as total numbers are concerned, there have been 138 sales altogether. 43 of those sales have happened since week 53 of 2011. Of the 138 sales, 105 were from either the US (42) or Canada (63). So sales to Canadian PlayBook owners accounts for almost half of the total sales.</p>

<p>My guess is that the price drops on the PlayBook meant that there were a lot of them showing up under Christmas trees, which would explain the relatively recent increase in sales. This is even more pronounced in the chart of Marblous downloads:</p>

<p><img src="http://www.ludicroussoftware.com/images/2012/02/21/MarblousDownloads.png" title="&#34;Marblous Downloads&#34;" alt="&#34;Marblous Downloads&#34;"></p>

<p>First off, the scale of the chart is obviously much higher, which is to be expected from a free app. But the trend is most consistent and more apparent with the free app, too: after its initial release in July, it settled down into a pattern of about 30 to 50 downloads per week. However, the same spike in week 53 of 2011 occurs here too, only it&#8217;s much more pronounced. Now, the game is being downloaded about 200 times per week, give or take (since week 8 isn&#8217;t yet complete, it&#8217;s a bit lower than the others). Although the chart doesn&#8217;t show total numbers, it turns out that Marblous has already been downloaded almost as many times in 2012 as it was in all of 2011, more if you include week 53 to capture all post-Christmas sales.</p>

<p>Also, the PlayBook is a predominantly North American and overwhelmingly Canadian device, if the downloads broken down by country are any indication (sorry for the small print):</p>

<p><img src="http://www.ludicroussoftware.com/images/2012/02/21/MarblousCountry.png" title="&#34;Marblous Downloads by Country&#34;" alt="&#34;Marblous Downloads by Country&#34;"></p>

<p>In terms of actual numbers, Marblous has been downloaded 3422 times since it was released. 2271 of those downloads are from the US and Canada, and 1691 (49.4%) are from Canada alone.</p>

<p>From my perspective, I think this points to the PlayBook as an attractive development target - I suspect that this is true of it were a developer&#8217;s primary target for development, but I&#8217;m pretty confident that it&#8217;s worth the time and effort required to port to the PlayBook an application that was originally developed for a different device/platform. It also means that I&#8217;d give serious thought to using Adobe AIR, since it would mean less work in porting to the PlayBook (as compared to something like Corona, which would require a rewrite to a different language).</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Scope in Corona]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2012/01/16/scope-in-corona/"/>
    <updated>2012-01-16T17:23:00-06:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2012/01/16/scope-in-corona</id>
    <content type="html"><![CDATA[<p>Scope in Lua can be a bit of a challenge if you&#8217;re used to how scope works in other languages, such as AS3. Rather than try to write the exhaustive, authoritative description of scope in Lua, I figured I&#8217;d do a quick overview to explain a few things that may not be obvious when you&#8217;re starting out, and also describe how I typically handle potential scoping issues.</p>

<!-- more -->


<h2>Local v. Global</h2>

<p>First off, the difference between local variables and global variables. The basic difference is pretty simple: anything that is not explicitly declared as a local variable is automatically made global. You don&#8217;t need to explicitly make a variable global by using <code>_G</code>, nor do you have to declare globals in a specific location. So, for example:</p>

<p>MyModule.lua:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">MyModule</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'>
</span><span class='line'><span class="n">foo</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="s">foobar&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="k">return</span> <span class="n">MyModule</span>
</span></code></pre></td></tr></table></div></figure>


<p>main.lua:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">MyModule</span> <span class="o">=</span> <span class="nb">require</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">MyModule&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c1">-- outputs &#39;foobar&#39; to the console</span>
</span><span class='line'><span class="nb">print</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you&#8217;re coming to Lua from another language, you have a few options for how to handle things:</p>

<ol>
<li><p>Avoid using global variables at all. Probably a comfortable approach if you&#8217;re coming from a language like AS3, where globals are allowed but not really encouraged as a best practice.</p></li>
<li><p>Use globals, but restrict their definition to main.lua. Optionally also use <code>_G</code> when declaring and accessing global variables (e.g. <code>_G.foo = "foo"</code>) to make it clear that this variable is global on purpose.</p></li>
<li><p>Put all your globals into a single module whose sole purpose is to store global values.</p></li>
</ol>


<p>Which one do you choose? That&#8217;s entirely up to you. I usually go with the third approach, since it forces me to do a little extra work to create a global variable, which helps make sure that I actually think through whether some needs to be global before I make it so. But I&#8217;ve also gone with option two on occasion, and it works just fine.</p>

<h2>Local Scope</h2>

<p>Local variables are local to the enclosing chunk. Chunks are basically one of four things:</p>

<ol>
<li><p>The enclosing module (i.e. the <code>.lua</code> file);</p></li>
<li><p>The enclosing function</p></li>
<li><p>The enclosing control structure</p></li>
<li><p><code>do/end</code> chunks</p></li>
</ol>


<p>So, consider the following simple module:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">Module</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">local</span> <span class="n">counter</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'>
</span><span class='line'><span class="k">function</span> <span class="nc">Module</span><span class="p">.</span><span class="nf">new</span><span class="p">()</span>
</span><span class='line'>  
</span><span class='line'>  <span class="kd">local</span> <span class="n">g</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newImage</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">myImage.png&quot;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'>  
</span><span class='line'>  <span class="kd">local</span> <span class="n">xPos</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'>  
</span><span class='line'>  <span class="kd">local</span> <span class="k">function</span> <span class="nf">onTouch</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
</span><span class='line'>      <span class="k">if</span> <span class="n">event</span><span class="p">.</span><span class="n">phase</span> <span class="o">==</span> <span class="s2">&quot;</span><span class="s">ended&quot;</span> <span class="k">then</span>
</span><span class='line'>          <span class="kd">local</span> <span class="n">xDelta</span> <span class="o">=</span> <span class="nb">math.random</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
</span><span class='line'>          <span class="n">xPos</span> <span class="o">=</span> <span class="n">xPos</span> <span class="o">+</span> <span class="n">xDelta</span>
</span><span class='line'>          <span class="n">g</span><span class="p">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">xPos</span>
</span><span class='line'>          <span class="n">counter</span> <span class="o">=</span> <span class="n">counter</span> <span class="o">+</span> <span class="mi">1</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>      <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">xDelta&quot;</span><span class="p">,</span> <span class="n">xDelta</span><span class="p">)</span>
</span><span class='line'>      <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">touches&quot;</span><span class="p">,</span> <span class="n">counter</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  
</span><span class='line'>  <span class="n">g</span><span class="p">:</span><span class="n">addEventListener</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">touch&quot;</span><span class="p">,</span> <span class="n">onTouch</span><span class="p">)</span>
</span><span class='line'>  
</span><span class='line'>  <span class="k">return</span> <span class="n">g</span>
</span><span class='line'>  
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">xPos&quot;</span><span class="p">,</span> <span class="n">xPos</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">return</span> <span class="n">Module</span>
</span></code></pre></td></tr></table></div></figure>


<p>When called, <code>Module.new()</code> will return a new display object. When touched, the object&#8217;s <code>x</code> position will be incremented by a random amount from 1 to 5. The amount is stored in a local variable, <code>xDelta</code>, and the actual position is stored in a different local variable, <code>xPos</code>. The callback also tracks the number of times the object has been touched.</p>

<p>So, there are three different local variables:</p>

<ul>
<li><p><code>counter</code>: declared at the module level, this variable is accessible throughout the entire module. Since it&#8217;s declared at the module level, all instances created with <code>Module.new()</code> will refer to the same <code>counter</code> variable. This is basically how you would implement static methods or properties in Lua</p></li>
<li><p><code>xPos</code>: declared inside the <code>new()</code> function, this is accessible only within <code>new()</code>. When the statement <code>print("xPos", xPos)</code> is executed, it will return a value of <code>nil</code>, because <code>xPos</code> isn&#8217;t accessible outside of its scope.</p></li>
<li><p><code>xDelta</code>: declared inside the <code>if</code> control block, this value is accessible only within the <code>if</code> block. As a result, the statement <code>print("xDelta", xDelta)</code> will return a value of <code>nil</code>. However, the value of <code>counter</code> will be properly printed, because <code>counter</code> is accessible throughout the module.</p></li>
</ul>


<p>(In both cases, the value returned is <code>nil</code> because Lua is looking for a global variable with that name since it hasn&#8217;t found any locals. Undeclared globals have a value of <code>nil</code>.)</p>

<p>Other control blocks for the purpose of local variables:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="k">while</span> <span class="n">foo</span> <span class="o">&lt;</span> <span class="n">bar</span> <span class="k">do</span>
</span><span class='line'>  <span class="kd">local</span> <span class="n">q</span> <span class="o">=</span> <span class="mi">5</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">repeat</span>
</span><span class='line'>  <span class="kd">local</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">10</span>
</span><span class='line'><span class="k">until</span> <span class="n">foo</span> <span class="o">&lt;</span> <span class="n">bar</span>
</span><span class='line'>
</span><span class='line'><span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">10</span> <span class="k">do</span>
</span><span class='line'>  <span class="kd">local</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">15</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">do</span>
</span><span class='line'>  <span class="kd">local</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">5</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The trickiest part in these examples is that in a <code>for</code> statement, the iterator variable is automatically declared as a local variable. This means that the value of <code>i</code> in the example above is unavailable outside of the loop. So, if you want to iterate over something, potentially breaking out of the loop when some condition is reached, and you need to remember the point at which you broke out of the loop, you need to declare a local variable outside of the <code>for</code> loop, and set its value equal to the iterator before your <code>break</code>.</p>

<h2>Scope in Functions</h2>

<p>Another aspect of scope in Lua is when dealing with functions. Unlike other languages, with many Lua functions you need to explicitly declare the scope of a function call. You can see this throughout the <code>table</code> and <code>string</code> packages:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="nb">table.insert</span><span class="p">(</span><span class="n">myTable</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">foo&quot;</span><span class="p">)</span>
</span><span class='line'><span class="nb">table.remove</span><span class="p">(</span><span class="n">myTable</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
</span><span class='line'><span class="kd">local</span> <span class="n">length</span> <span class="o">=</span> <span class="nb">string.len</span><span class="p">(</span><span class="n">myString</span><span class="p">)</span>
</span><span class='line'><span class="n">myString</span> <span class="o">=</span> <span class="nb">string.lower</span><span class="p">(</span><span class="n">myString</span><span class="p">)</span>
</span><span class='line'><span class="c1">-- etc.</span>
</span></code></pre></td></tr></table></div></figure>


<p>In effect, the first argument being passed to these functions is scope of the function call. It&#8217;s telling <code>table</code> to insert or remove values from <code>myTable</code>, or to do stuff with <code>myString</code>.</p>

<p>This is done so frequently that in many cases Lua provides &#8220;syntactic sugar&#8221; to make it easier to make the same function calls. It doesn&#8217;t apply to the <code>table</code> package, but for the string methods above they could be rewritten this way:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">length</span> <span class="o">=</span> <span class="n">myString</span><span class="p">:</span><span class="n">len</span><span class="p">()</span>
</span><span class='line'><span class="n">myString</span> <span class="o">=</span> <span class="n">myString</span><span class="p">:</span><span class="n">lower</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<p>In effect, using a colon to call these methods lets you emulate the sort of object-oriented approach that you get in other languages. With the colon, Lua knows to pass the object as the first parameter of the function.</p>

<p>You can recreate this same approach in your own code in one of two ways: either declare your function using a colon, or make <code>self</code> the first parameter of the function:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">g</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newRect</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">100</span> <span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">g&quot;</span><span class="p">,</span> <span class="n">g</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">function</span> <span class="nf">g</span><span class="p">:</span><span class="n">test</span><span class="p">()</span>
</span><span class='line'>  <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">g:test&quot;</span><span class="p">,</span> <span class="n">self</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">function</span> <span class="nc">g</span><span class="p">.</span><span class="nf">test2</span><span class="p">(</span><span class="n">self</span><span class="p">)</span>
</span><span class='line'>  <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">g.test2&quot;</span><span class="p">,</span> <span class="n">self</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">g</span><span class="p">:</span><span class="n">test</span><span class="p">()</span>
</span><span class='line'><span class="n">g</span><span class="p">:</span><span class="n">test2</span><span class="p">()</span>
</span><span class='line'><span class="n">g</span><span class="p">.</span><span class="n">test2</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<p>The output from this will be:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="n">g</span>       <span class="n">table</span><span class="p">:</span> <span class="mh">0x5bdaf0</span>
</span><span class='line'><span class="n">g</span><span class="p">:</span><span class="n">test</span>  <span class="n">table</span><span class="p">:</span> <span class="mh">0x5bdaf0</span>
</span><span class='line'><span class="n">g</span><span class="p">.</span><span class="n">test2</span> <span class="n">table</span><span class="p">:</span> <span class="mh">0x5bdaf0</span>
</span><span class='line'><span class="n">g</span><span class="p">.</span><span class="n">test2</span> <span class="kc">nil</span>
</span></code></pre></td></tr></table></div></figure>


<p>Since the value of <code>self</code> from both functions matches the value of <code>g</code> from the original print statement, you can see that whether done implicitly or explicitly, calling a method using the colon will correctly set the proper scope. However, in the last case, where the function was called using dot notation, no reference to self was passed to the function, so <code>self</code> is <code>nil</code>.</p>

<p>One implication of this approach is that in Lua the scope of a method of an object can be set to any other object. Like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">g</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newRect</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">100</span> <span class="p">)</span>
</span><span class='line'><span class="kd">local</span> <span class="n">h</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newRect</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">200</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">100</span> <span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">g&quot;</span><span class="p">,</span> <span class="n">g</span><span class="p">)</span>
</span><span class='line'><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">h&quot;</span><span class="p">,</span> <span class="n">h</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">function</span> <span class="nf">g</span><span class="p">:</span><span class="n">test</span><span class="p">()</span>
</span><span class='line'>  <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">g.test&quot;</span><span class="p">,</span> <span class="n">self</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">g</span><span class="p">:</span><span class="n">test</span><span class="p">()</span>
</span><span class='line'><span class="n">g</span><span class="p">.</span><span class="n">test</span><span class="p">(</span><span class="n">h</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This gives us the following output:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="n">g</span>       <span class="n">table</span><span class="p">:</span> <span class="mh">0x202d490</span>
</span><span class='line'><span class="n">h</span>       <span class="n">table</span><span class="p">:</span> <span class="mh">0x202d5e0</span>
</span><span class='line'><span class="n">g</span><span class="p">.</span><span class="n">test</span>  <span class="n">table</span><span class="p">:</span> <span class="mh">0x202d490</span>
</span><span class='line'><span class="n">g</span><span class="p">.</span><span class="n">test</span>  <span class="n">table</span><span class="p">:</span> <span class="mh">0x202d5e0</span>
</span></code></pre></td></tr></table></div></figure>


<p>So even though <code>test2</code> is a method of the display object <code>g</code>, it can act on <code>h</code> if we pass it to that function. Not that you&#8217;d want to do that.</p>

<p>Specifically in the Corona APIs, you see the colon notation fairly frequently:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">g</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newImage</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">image.png&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="n">g</span><span class="p">:</span><span class="n">removeSelf</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="c1">-- is the same as:</span>
</span><span class='line'><span class="n">g</span><span class="p">.</span><span class="n">removeSelf</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c1">-- so yes, you could also do:</span>
</span><span class='line'><span class="n">g</span><span class="p">.</span><span class="n">removeSelf</span><span class="p">(</span><span class="n">h</span><span class="p">)</span>
</span><span class='line'><span class="c1">-- but again, why would you want to?</span>
</span></code></pre></td></tr></table></div></figure>


<h2>All That Said&#8230;</h2>

<p>I never (okay, very rarely) actually use <code>self</code> in Corona apps. About the only time I&#8217;ll use it is if I&#8217;m creating a bunch of &#8216;anonymous&#8217; objects - i.e. objects for which I&#8217;m not explicitly storing a reference - and need to create some kind of event handler for them. In that case, I&#8217;ll typically use a table listener rather than a function listener:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="k">function</span> <span class="nf">onTouch</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">event</span><span class="p">)</span>
</span><span class='line'>  <span class="k">if</span> <span class="n">event</span><span class="p">.</span><span class="n">phase</span> <span class="o">==</span> <span class="s2">&quot;</span><span class="s">ended&quot;</span> <span class="k">then</span>
</span><span class='line'>      <span class="nb">print</span><span class="p">(</span><span class="n">self</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">6</span> <span class="k">do</span>
</span><span class='line'>  <span class="kd">local</span> <span class="n">rect</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newRect</span><span class="p">(</span> <span class="n">i</span> <span class="o">*</span> <span class="mi">15</span><span class="p">,</span> <span class="n">i</span> <span class="o">*</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">10</span> <span class="p">)</span>
</span><span class='line'>  <span class="n">rect</span><span class="p">.</span><span class="n">touch</span> <span class="o">=</span> <span class="n">onTouch</span>
</span><span class='line'>  <span class="n">rect</span><span class="p">:</span><span class="n">addEventListener</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">touch&quot;</span><span class="p">,</span> <span class="n">rect</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>(Even though in this case you could also use <code>event.target</code> to figure out what object was touched.)</p>

<p>However, the far more common approach I&#8217;ll take is not not use self at all and simply rely on the closure to take care of scope for me. So for example, here&#8217;s a condensed version of the module we looked at earlier:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">Module</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'>
</span><span class='line'><span class="k">function</span> <span class="nc">Module</span><span class="p">.</span><span class="nf">new</span><span class="p">()</span>
</span><span class='line'>  
</span><span class='line'>  <span class="kd">local</span> <span class="n">g</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newImage</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">myImage.png&quot;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'>  
</span><span class='line'>  <span class="kd">local</span> <span class="n">xPos</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'>  
</span><span class='line'>  <span class="kd">local</span> <span class="k">function</span> <span class="nf">onTouch</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
</span><span class='line'>      <span class="k">if</span> <span class="n">event</span><span class="p">.</span><span class="n">phase</span> <span class="o">==</span> <span class="s2">&quot;</span><span class="s">ended&quot;</span> <span class="k">then</span>
</span><span class='line'>          <span class="kd">local</span> <span class="n">xDelta</span> <span class="o">=</span> <span class="nb">math.random</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
</span><span class='line'>          <span class="n">xPos</span> <span class="o">=</span> <span class="n">xPos</span> <span class="o">+</span> <span class="n">xDelta</span>
</span><span class='line'>          <span class="n">g</span><span class="p">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">xPos</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  
</span><span class='line'>  <span class="n">g</span><span class="p">:</span><span class="n">addEventListener</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">touch&quot;</span><span class="p">,</span> <span class="n">onTouch</span><span class="p">)</span>
</span><span class='line'>  
</span><span class='line'>  <span class="k">return</span> <span class="n">g</span>
</span><span class='line'>  
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">return</span> <span class="n">Module</span>
</span></code></pre></td></tr></table></div></figure>


<p>Notice that inside the <code>onTouch</code> event handler, I&#8217;m referencing the display object <code>g</code> directly, rather than using <code>self</code>. Because Lua supports closures, even though <code>g</code> is a local variable (and so the reference to it should be removed when the function has finished execution), it remains in scope as long as it&#8217;s required for some other chunk of code to run.</p>

<p>(That may not be the most elegant way to describe closures, but should get the point across.)</p>

<p>The added benefit of this approach is that it doesn&#8217;t really matter whether you use colon or dot notation to access your object&#8217;s methods. I remember reading one comment where the developer said they prefer to use colon notation to reinforce the fact that they&#8217;re calling methods of an instance of a class, rather than a class/static method. My current inclination is to use the AS3-style naming conventions to distinguish between classes and instances of a class (so, <code>Class.new()</code> v. <code>instance.doStuff()</code>). But as with some many things in Lua: whatever floats your boat.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[hasEventListener() in Corona]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/12/23/haseventlistener-in-corona/"/>
    <updated>2011-12-23T10:26:00-06:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/12/23/haseventlistener-in-corona</id>
    <content type="html"><![CDATA[<p>One of the nice things about Corona if you&#8217;re a Flash developer is the similarity between Corona&#8217;s API and some Flash APIs. For example, the event listener, although different in some respects, are also quite similar, which makes learning Corona a lot easier. But sometimes, those differences mean you&#8217;re missing out on things that you &#8220;take for granted&#8221; in Flash. For example, Corona has no native <code>hasEventListener</code> function. But I needed one, so I wrote one.</p>

<!-- more -->


<p>The <code>hasEventListener</code> function I wrote relies on the fact that there are some &#8220;hidden&#8221; properties in Corona that aren&#8217;t mentioned in the reference documents, but are completely accessible using plain old Lua. Specifically, objects that can listen for events will have a table storing the events being listened for and the functions to call when the events occur - you can get more background about this in an earlier post about the <a href="http://www.ludicroussoftware.com/blog/2011/08/24/remove-all-listeners/">fastest way to remove all event listeners</a>.</p>

<p>You can <a href="https://github.com/osadchuk/Corona-Utilities">grab the code from the github repository I set up</a> to hold all of the utility functions I use for Corona development. (I&#8217;ll be adding to it over time.)</p>

<p>Usage is pretty straightforward:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'>  <span class="c1">-- import the module</span>
</span><span class='line'>  <span class="c1">-- local Utilities = require(&quot;com.ludicroussoftware.Utilities&quot;)</span>
</span><span class='line'>  
</span><span class='line'>  <span class="c1">-- check for a function listener</span>
</span><span class='line'>  <span class="n">hasListener</span> <span class="o">=</span> <span class="n">Utilities</span><span class="p">.</span><span class="n">hasEventListener</span><span class="p">(</span><span class="n">object</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">touch&quot;</span><span class="p">,</span> <span class="n">onTouch</span><span class="p">)</span>
</span><span class='line'>  
</span><span class='line'>  <span class="c1">-- check for a table listener</span>
</span><span class='line'>  <span class="n">hasListener</span> <span class="o">=</span> <span class="n">Utilities</span><span class="p">.</span><span class="n">hasEventListener</span><span class="p">(</span><span class="n">object</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">touch&quot;</span><span class="p">,</span> <span class="n">object</span><span class="p">)</span>
</span><span class='line'>  
</span><span class='line'>  <span class="c1">-- check for any listener whatsoever</span>
</span><span class='line'>  <span class="n">hasListener</span> <span class="o">=</span> <span class="n">Utilities</span><span class="p">.</span> <span class="n">hasEventListener</span><span class="p">(</span><span class="n">object</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">touch&quot;</span><span class="p">)</span>
</span><span class='line'>  
</span></code></pre></td></tr></table></div></figure>


<p>A somewhat more detailed example is included in the repository.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Speed Up Device Builds in Image-heavy Apps]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/12/20/speed-up-device-builds-in-image-heavy-apps/"/>
    <updated>2011-12-20T08:19:00-06:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/12/20/speed-up-device-builds-in-image-heavy-apps</id>
    <content type="html"><![CDATA[<p>I&#8217;m currently working on an image-heavy Corona iPad application for a client. And when I say &#8220;image-heavy&#8221;, I mean <em>extremely</em> image-heavy. The app is about 150MB in size, and about 99.8% of that is PNG images, and most of those images are fullscreen (i.e. 1024 x 768) images. It takes about eight(!) minutes to build the app using the latest public release of the Corona SDK, and I wanted to find a way to speed that up.</p>

<!-- more -->


<p>(The app is for internal use, so it won&#8217;t be released publicly and I can&#8217;t share specific images.)</p>

<p>From watching the console output as the app was built, it was clear that the bottleneck is the process of compressing/copying all the images into the app (Corona runs all PNG images through <code>pngcrush</code> to optimize them; this is also what Xcode does). Since copying a file isn&#8217;t particularly time-consuming, I assumed that the compression was the problem.</p>

<p>My initial thought was that it would be great if there were a way for Corona to cache any image optimizations that it does, so that it doesn&#8217;t have to do them again if the image changes. This would be similar to how Flash Pro will now cache the compressed version it generates from a WAV file during the build process.</p>

<p>I mentioned this to Carlos, and he suggested that I manually optimize the images ahead of time, and that he thought this would cause that step to be skipped during the build process. The optimization is performed by a program called <code>iphoneos-optimize</code> in the iOS SDK, so you&#8217;ll need to have the SDK installed. <code>iphoneos-optimize</code> is basically just a wrapper for <code>pngcrush</code>, passing in the optimal settings so you don&#8217;t need to do that yourself.</p>

<p>The major downside of this approach is that it renders your images unusable on the desktop - you can get some technical background on how the images are optimized in <a href="http://iphonedevelopment.blogspot.com/2008/10/iphone-optimized-pngs.html">this blog post</a>, but the short version is that an optimized PNG isn&#8217;t a &#8216;real&#8217; PNG, so it looks like a corrupt file. If you optimize the images yourself and then try to test your app in the Corona Simulator, you&#8217;ll get a lot of &#8220;incorrect header check&#8221; error messages, and your images won&#8217;t show up.</p>

<p>So, this means that you&#8217;ll need to export your project to a separate folder so that you don&#8217;t destroy your original images. Here&#8217;s the basic process:</p>

<ol>
<li>Export your project to a separate folder; if you&#8217;re using git, then <code>git checkout-index -a -f --prefix=/path/to/target/folder/</code>, when run from the root folder of your project, is an easy way to do this.</li>
<li>Open up the Terminal and change to the directory containing the copy of your project (<code>cd /path/to/target/folder/</code>)</li>
<li>Run <code>iphoneos-optimize</code> on the folder. You need to provide the target folder, so just use the dot to pass in the current folder: <code>/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/iphoneos-optimize .</code></li>
<li>Open the project in the Corona Simulator. Ignore the fact that none of your images show up.</li>
<li>Build as usual.</li>
</ol>


<p>So, what are the results for my ginormous image-heavy app?</p>

<p>The regular build process, where Corona handles the optimization, took <strong>8 minutes, 8 seconds</strong>, just now.</p>

<p>Manually running the image optimization from the command line took <strong>1 minute, 36 seconds</strong>.</p>

<p>Creating the device build with Corona using the pre-optimized images took <strong>42 seconds</strong>.</p>

<p>So, clearly, even if you factor in the time spent copying images, etc., pre-optimizing images can save a lot of time during the build process. This is especially true if you&#8217;re debugging stuff that can&#8217;t be tested in the Simulator anyway (like multitouch), so it doesn&#8217;t matter if your app doesn&#8217;t &#8216;work&#8217; in the Simulator.</p>

<p>Also, since these images aren&#8217;t going to be changing a lot, I don&#8217;t need to run the optimization every time, so things could be even faster if I were to write a shell script to move all my images out of my project folder and replace them with the optimized images. Then I could do the build and then call another script to copy all the images back again so I could have something that does work in the Simulator.</p>

<p>(It would be so nice to be able to automate the whole process from the command line! At least with Android builds you can go into the Simulator resource bundle and tweak the <code>build.xml</code> file. It&#8217;s not much, but it&#8217;s something. But that&#8217;s another blog post.)</p>

<p>So why the huge discrepancy here? I&#8217;m not sure why the &#8216;default&#8217; build takes so long, but my best guess is that <code>iphoneos-optimize</code> can work a little more quickly because it&#8217;s doing things in batch mode - it iterates through the entire project just optimizes the images. Based on the output you get when Corona creates a device build, it looks like the build process iterates through every individual file in the project and decides what to do with it. So, images are getting optimized one at a time and then copied into the app. I&#8217;m not sure if that&#8217;s actually the problem, but that seems to be what&#8217;s happening.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Fixing the Corona Debugger in Snow Leopard]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/12/12/fixing-the-corona-debugger-in-snow-leopard/"/>
    <updated>2011-12-12T11:40:00-06:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/12/12/fixing-the-corona-debugger-in-snow-leopard</id>
    <content type="html"><![CDATA[<p>Last week somebody <a href="http://www.ludicroussoftware.com/blog/2011/08/22/using-the-corona-debugger/#comment-374923559">commented</a> on my post about <a href="http://www.ludicroussoftware.com/blog/2011/08/22/using-the-corona-debugger/">using the Corona debugger</a> that they&#8217;re getting an error with one of the daily builds. I tested this out against the latest public build and I experienced the same thing. Here&#8217;s what I&#8217;m seeing:</p>

<pre><code>dyld: Library not loaded: /usr/lib/libedit.3.dylib
  Referenced from: /Applications/CoronaSDK/debugger
    Reason: image not found
Trace/BPT trap
</code></pre>

<!-- more -->


<p>So the problem is that libedit.3.dylib isn&#8217;t found on my machine. Based on a bit of Googling, it looks like the problem is that I&#8217;m running Snow Leopard, which has libedit.2.dylib. However, my guess is that Ansca is building against Lion, which ships with libedit.3.dylib.</p>

<p>Luckily, there&#8217;s an easy fix to the problem. Open up the Terminal, and issue the following commands:</p>

<pre><code>cd /usr/lib
sudo ln -s libedit.2.dylib libedit.3.dylib
</code></pre>

<p>All you&#8217;re doing is navigating to the directory where the debugger is looking for libedit.3.dylib, and creating a symbolic link to libedit.2.dylib named libedit.3.dylib. So when the debugger looks for the latter, it&#8217;ll get pointed to the former. After you enter the second command, you&#8217;ll have to enter your password.</p>

<p>This seems to be working fine for me so far, but hopefully it&#8217;s something for which Ansca can issue a proper fix in the future.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Build Missile Command With the Corona SDK]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/11/21/build-missile-command-with-the-corona-sdk/"/>
    <updated>2011-11-21T22:13:00-06:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/11/21/build-missile-command-with-the-corona-sdk</id>
    <content type="html"><![CDATA[<p>Last week I had the pleasure and the honour of being able to deliver both an <a href="http://www.fitc.ca/events/presentations/presentation.cfm?event=118&amp;presentation_id=1699">introduction to Corona presentation</a> and a <a href="http://www.fitc.ca/events/presentations/presentation.cfm?event=118&amp;presentation_id=1717">full-day game development workshop</a> at <a href="http://www.fitc.ca/events/about/?event=118">FITC Screens</a>. The workshop attendees built (most of) a complete cross-platform Missile Command game using the Corona SDK; we ran out of time before we could implement everything you&#8217;ll find in the workbook, but we covered all of the basics. I&#8217;ve uploaded the materials I used in the workshop to <a href="https://github.com/osadchuk/Corona-SDK-Missile-Command">GitHub</a>, so feel free to check it out.</p>

<!-- more -->


<p>The workbook will walk you through all the steps involved in building the game, right from configuring the project through to adding sound effects and creating custom components to control the volume. Along the way, you&#8217;ll build a game that, among other things:</p>

<ul>
<li>Uses Corona&#8217;s cross-platform capabilities to build a game that can be deployed to both iOS and Android devices from the same code base;</li>
<li>Uses the built-in physics engine to handle collision detection;</li>
<li>Uses sprite sheets for in-game animations, and learn how to incorporate sprite sheets sized for different devices from within the same code base (i.e. manually doing for sprites what <code>display.newImageRect()</code> does for images);</li>
</ul>


<p>The only things currently not covered in the tutorial, which I may add down the road if I have some spare time (ha!) is:</p>

<ul>
<li>Creating a menu system with a main menu, help screen, and so on. Once Corona publicly releases their new <a href="http://blog.anscamobile.com/2011/11/introducing-the-storyboard-api/">Storyboard API</a>, I may add this;</li>
<li>Creating a device build and deploying to your device for testing. In the meantime, there are other tutorials online that cover this, including a pretty thorough explanation of the steps involved on <a href="http://developer.anscamobile.com/resources/docs">Ansca&#8217;s website</a> - look under the &#8220;Basics&#8221; heading.</li>
</ul>


<p>Please be aware that a few points that probably should be fleshed out in the text are glossed over. This was intentional, since there were some things I felt would be better explained during the course of the workshop itself, rather than putting it all into the workbook. If there is sufficient interest, I may expand on these explanations (again, given this mythical spare time I seem to think exists).</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Performance of Local v. Table Functions]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/11/01/local-v--table-functions/"/>
    <updated>2011-11-01T14:12:00-05:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/11/01/local-v&#8211;table-functions</id>
    <content type="html"><![CDATA[<p>Short version: If you&#8217;re creating a Lua module with publicly accessible functions, just add the functions as properties of the table you&#8217;re returning, rather than as local functions with a table reference. Now for the long(er) version&#8230;</p>

<p><strong>Update:</strong> added one more example for further context. It&#8217;s at the bottom of the post.</p>

<!--more-->


<p>While working on an app that uses a particularly popular Corona library I encountered a forward declaration bug in the library. If you&#8217;ve been using Corona/Lua for any length of time, you&#8217;ve probably encountered this kind of bug before. It occurs when you try to reference a local variable that you haven&#8217;t yet declared. It looks something like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="n">myFunction</span> <span class="n">printA</span><span class="p">()</span>
</span><span class='line'>  <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="kd">local</span> <span class="n">a</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="s">5&quot;</span>
</span><span class='line'><span class="n">printA</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<p>The problem is that the <code>printA()</code> function is referencing the local variable <code>a</code>, but <code>a</code> wasn&#8217;t declared before <code>printA()</code>. So, not having found a local variable <code>a</code>, it looks for a global variable with the same name. If that exists, then it prints out the value of that variable (which probably isn&#8217;t your intent), or you get a <code>nil</code> value error. The easy way to avoid this kind of error is to declare all your variables at the top of your chunk of code, before they&#8217;re referenced.</p>

<p>The actual forward reference bug in the library was a little more involved, because the local functions were also assigned to properties of the table that the library returns, because the functions need to be publicly accessible. It looks something like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="n">myTable</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">local</span> <span class="k">function</span> <span class="nf">functionA</span><span class="p">()</span>
</span><span class='line'>  <span class="n">functionB</span><span class="p">()</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="n">myTable</span><span class="p">.</span><span class="n">functionA</span> <span class="o">=</span> <span class="n">functionA</span>
</span><span class='line'>
</span><span class='line'><span class="kd">local</span> <span class="k">function</span> <span class="nf">functionB</span><span class="p">()</span>
</span><span class='line'>  <span class="c1">-- whatever</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="n">myTable</span><span class="p">.</span><span class="n">functionB</span> <span class="o">=</span> <span class="n">functionB</span>
</span><span class='line'>
</span><span class='line'><span class="k">return</span> <span class="n">myTable</span>
</span></code></pre></td></tr></table></div></figure>


<p>So you can probably spot the forward declaration bug. This could have been fixed by a bunch of forward declarations. But in thinking about this, I wondered if that would even be worthwhile. All of the functions are being added as properties of the table, so why not just do this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="n">myTable</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'><span class="k">function</span> <span class="nc">myTable</span><span class="p">.</span><span class="nf">functionA</span><span class="p">()</span>
</span><span class='line'>  <span class="n">myTable</span><span class="p">.</span><span class="n">functionB</span><span class="p">()</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">function</span> <span class="nc">myTable</span><span class="p">.</span><span class="nf">functionB</span><span class="p">()</span>
</span><span class='line'>  <span class="c1">-- whatever</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">return</span> <span class="n">myTable</span>
</span></code></pre></td></tr></table></div></figure>


<p>I don&#8217;t know what the developer was really thinking about, but I suspect that the answer is &#8220;performance&#8221;. Since Lua is able to look up local variables faster than table properties, referencing the functions via local variables is presumably faster for function calls from within the module.</p>

<p>So I wonder if that is really true.</p>

<p>Or if it is, whether the speed difference makes the extra effort involved in the &#8216;local&#8217; approach to be worthwhile.</p>

<p>And it&#8217;s not. At least, not enough to matter.</p>

<p>To test this, here&#8217;s the sample app I whipped up:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">myTable</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">local</span> <span class="k">function</span> <span class="nf">stress</span><span class="p">()</span>
</span><span class='line'>  <span class="kd">local</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="mi">1</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">myTable</span><span class="p">.</span><span class="n">stress</span> <span class="o">=</span> <span class="n">stress</span>
</span><span class='line'>
</span><span class='line'><span class="c1">-- test calling the local function</span>
</span><span class='line'><span class="kd">local</span> <span class="n">startTime</span> <span class="o">=</span> <span class="nb">os.time</span><span class="p">()</span>
</span><span class='line'><span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">10000000</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">stress</span><span class="p">()</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">stress(), elapsed &quot;</span><span class="p">,</span> <span class="p">(</span><span class="nb">os.time</span><span class="p">()</span> <span class="o">-</span> <span class="n">startTime</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">-- test calling the table function</span>
</span><span class='line'><span class="n">startTime</span> <span class="o">=</span> <span class="nb">os.time</span><span class="p">()</span>
</span><span class='line'><span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">10000000</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">myTable</span><span class="p">.</span><span class="n">stress</span><span class="p">()</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">myTable.stress(), elapsed &quot;</span><span class="p">,</span> <span class="p">(</span><span class="nb">os.time</span><span class="p">()</span> <span class="o">-</span> <span class="n">startTime</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>So here&#8217;s what the code does:</p>

<ol>
<li>Create a local function;</li>
<li>Store a reference to that function in a table;</li>
<li>Time how long it takes to call that local function;</li>
<li>Time how long it takes to call the function via the table property.</li>
</ol>


<p>And the result when I built this and tested it on my iPhone 4: 7 milliseconds for the local function calls, and 9 milliseconds for the table function calls. Now, you may be thinking that that is actually pretty significant, but take a look at how many times I had to call the functions to get that difference: 10,000,000 times.</p>

<p>Ten million times. To save two milliseconds</p>

<p>Now, just to be clear, I&#8217;m a big fan of optimizing code up front in a lot of cases. Having started my development life in the world of Flash Lite, that sort of behaviour is ingrained. But even for me, it&#8217;s tough to see the benefit to the &#8220;local and table&#8221; approach. If there&#8217;s a value in that approach that I&#8217;m not quite seeing, please let me know what it is!</p>

<p><strong>Update:</strong> I just wanted to expand on my comments above. Initially, I had meant this comparison of the execution of local and table functions to be applicable within a limited context: when you&#8217;re creating a local function that you&#8217;re planning to make public via the table. To be clear, my point is that there is no reasonable benefit to be gained from making that function local in the first place. Just skip that step altogether.</p>

<p>But as I thought about it more, I started to wonder about the general wisdom of the usual approach of making local references to table functions to increase performance speed. For example, doing this sort of thing:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">asin</span> <span class="o">=</span> <span class="nb">math.asin</span>
</span><span class='line'><span class="kd">local</span> <span class="n">myVar</span> <span class="o">=</span> <span class="n">asin</span><span class="p">(</span><span class="mi">43</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is the usual recommendation, and at first I feared that what I&#8217;d written above might be construed as contradicting that general wisdom. But it turns out that I&#8217;m not sure there&#8217;s enough benefit to be gained from doing this. Here&#8217;s the sample code:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="n">startTime</span> <span class="o">=</span> <span class="nb">os.time</span><span class="p">()</span>
</span><span class='line'><span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">5000000</span> <span class="k">do</span>
</span><span class='line'>  <span class="kd">local</span> <span class="n">j</span> <span class="o">=</span> <span class="nb">math.floor</span><span class="p">(</span><span class="mf">4.35</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">math.floor, elapsed &quot;</span><span class="p">,</span> <span class="p">(</span><span class="nb">os.time</span><span class="p">()</span> <span class="o">-</span> <span class="n">startTime</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="kd">local</span> <span class="n">floor</span> <span class="o">=</span> <span class="nb">math.floor</span>
</span><span class='line'><span class="n">startTime</span> <span class="o">=</span> <span class="nb">os.time</span><span class="p">()</span>
</span><span class='line'><span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">5000000</span> <span class="k">do</span>
</span><span class='line'>  <span class="kd">local</span> <span class="n">j</span> <span class="o">=</span> <span class="n">floor</span><span class="p">(</span><span class="mf">4.35</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">floor, elapsed &quot;</span><span class="p">,</span> <span class="p">(</span><span class="nb">os.time</span><span class="p">()</span> <span class="o">-</span> <span class="n">startTime</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Turns out that over the course of 5,000,000 calls to the local function, a grand total of one millisecond is saved. Again, this is tested on an iPhone 4. I realize that a) these results may vary quite a bit on different devices, so thorough testing on all targets should be undertaken, and b) in other contexts, where you may in fact be handling that many function calls (e.g. if you&#8217;re using Lua in a non-mobile setting, perhaps), local references to table functions may be worthwhile.</p>

<p>But, for my part, I think I&#8217;ll probably skip it, at least until the point where I think it might actually help when implemented as part of a broader range of optimizations.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using `do/end` for Disposable Code]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/09/21/using-do-end-for-disposable-code/"/>
    <updated>2011-09-21T20:53:00-05:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/09/21/using-do-end-for-disposable-code</id>
    <content type="html"><![CDATA[<p>This is a quick tip about how to take advantage of scope in Lua to keep memory consumption down. Typically, any application that you write is going to have some initialization code. By this, I mean code that is going to run once and only once. It may set up some initial values, do a bunch of calculations to configure your app for the specific device it&#8217;s on, and so on.</p>

<!--more-->


<p>Even though this code may only run once, it might be very involved. Because you value clean, readable code, you might end up using a lot of local variables, even though you won&#8217;t need to reference these values again. The problem is, if you just stick this code inside your <code>main.lua</code> file (or a module), these variables will persist, taking up memory when you no longer need them. You have a couple of options to deal with this:</p>

<ul>
<li>you could <code>nil</code> out all these variables, but that involves writing more code than necessary. I&#8217;ve done this before, and I&#8217;d like to avoid the extra work if I could. Plus, I think it&#8217;s somewhat inelegant to manually <code>nil</code> out a bunch of variables to make them eligible for garbage collection.</li>
<li>you could wrap all the initialization code inside a function. This is a little more elegant, since any locals within the scope of the function will get cleaned up after the function has completed. However, the function itself is still lying around (since that&#8217;s the whole point of a function), so you haven&#8217;t cleaned up after yourself completely. It&#8217;s like when you sweep debris onto a dustpan and you get that little line of dirt along the front edge of the pan. I hate that.</li>
</ul>


<p>There&#8217;s one more option, which I think is more elegant than these two: wrap your initialization code inside a <code>do/end</code> block. Unlike code inside a function, code inside <code>do/end</code> is executed immediately. But the real beauty of <code>do/end</code> is that <em>it has its own scope</em>, just like a function, so any local variables inside the block can be garbage collected after the code block has finished execution. To compare it to a function, consider this simple code:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>local function init()
</span><span class='line'>  local a = "foo"
</span><span class='line'>  print(a)
</span><span class='line'>end
</span><span class='line'>init()
</span><span class='line'>print(a)</span></code></pre></td></tr></table></div></figure>


<p>This is pretty standard Lua function scope. The first line of output will be &#8220;foo&#8221;, because <code>init()</code> is called first and references the local variable <code>a</code> within the scope of the function, and the second line of output is <code>nil</code> because <code>a</code> is within the scope of <code>init()</code>. In comparison, here&#8217;s similar code without the function call:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="k">do</span>
</span><span class='line'>  <span class="kd">local</span> <span class="n">a</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="s">foo&quot;</span>
</span><span class='line'>  <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>In this case, the code inside <code>do/end</code> is executed immediately - no function call required and no function hanging around after it&#8217;s no longer needed. And even though the code is executed immediately, it still has its own scope. The first line of output will be &#8220;foo&#8221;, and the second line will be <code>nil</code>, just as before, because <code>a</code> exists only within the scope of the <code>do/end</code> block.</p>

<p>Now, you may think that this degree of concern over memory consumption is unnecessary, but I hate that little line of dust in front of the dustpan. Plus I like the fact that this clearly communicates that the initialization code is completely and utterly disposable</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Passing Parameters in timer.performWithDelay()]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/09/05/passing-parameters-in-a-timer/"/>
    <updated>2011-09-05T13:02:00-05:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/09/05/passing-parameters-in-a-timer</id>
    <content type="html"><![CDATA[<p>Somebody on the Corona forums asked if it were possible to pass arguments to a timer callback from within the <code>performWithDelay</code> call. After a bit of thought, here&#8217;s what I came up with.</p>

<!-- more -->


<p>What they wanted was something like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="k">function</span> <span class="nf">restartLevel</span><span class="p">(</span><span class="n">boolVal</span><span class="p">,</span> <span class="n">stringVal</span><span class="p">)</span>
</span><span class='line'>  <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">boolVal&quot;</span><span class="p">,</span> <span class="n">boolVal</span><span class="p">)</span>
</span><span class='line'>  <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">stringVal&quot;</span><span class="p">,</span> <span class="n">stringVal</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="n">timer</span><span class="p">.</span><span class="n">performWithDelay</span><span class="p">(</span> <span class="mi">1000</span><span class="p">,</span> <span class="n">restartLevel</span><span class="p">(</span> <span class="kc">true</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">resetScore&quot;</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Ignore syntactical issues for the moment as I&#8217;m copying their pseudo-code directly; also, I have no idea what their <code>restartLevel</code> function looks like, so I just made one up.</p>

<p>My first thought was that the simplest approach would be to just use a closure, so the code would look something like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="k">function</span> <span class="nf">restartLevel</span><span class="p">(</span><span class="n">boolVal</span><span class="p">,</span> <span class="n">stringVal</span><span class="p">)</span>
</span><span class='line'>  <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">boolVal&quot;</span><span class="p">,</span> <span class="n">boolVal</span><span class="p">)</span>
</span><span class='line'>  <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">stringVal&quot;</span><span class="p">,</span> <span class="n">stringVal</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="kd">local</span> <span class="k">function</span> <span class="nf">doIt</span><span class="p">()</span>
</span><span class='line'>  <span class="n">restartLevel</span><span class="p">(</span><span class="kc">true</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">resetScore&quot;</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">timer</span><span class="p">.</span><span class="n">performWithDelay</span><span class="p">(</span> <span class="mi">1000</span><span class="p">,</span> <span class="n">doIt</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This will work nicely, but if you have to do this a lot, then you&#8217;ll end up with closures all over the place. That in itself isn&#8217;t a bad thing, since Lua is so closure-friendly, but it means having to write all those closures, which is more work for you. And since there are a lot of cool things you could be doing that don&#8217;t involve writing a bunch of repetitive code, here&#8217;s a way to avoid all of that:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="n">timer</span><span class="p">.</span><span class="n">oldPerformWithDelay</span> <span class="o">=</span> <span class="n">timer</span><span class="p">.</span><span class="n">performWithDelay</span>
</span><span class='line'>
</span><span class='line'><span class="n">timer</span><span class="p">.</span><span class="n">performWithDelay</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">interval</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="o">..</span><span class="p">.)</span>
</span><span class='line'>  <span class="kd">local</span> <span class="n">iterations</span><span class="p">,</span> <span class="n">params</span>
</span><span class='line'>  <span class="kd">local</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{</span><span class="o">..</span><span class="p">.}</span>
</span><span class='line'>  <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">options</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">==</span> <span class="s2">&quot;</span><span class="s">number&quot;</span> <span class="k">then</span>
</span><span class='line'>      <span class="n">iterations</span> <span class="o">=</span> <span class="n">options</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
</span><span class='line'>      <span class="n">params</span> <span class="o">=</span> <span class="n">options</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>      <span class="n">iterations</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'>      <span class="n">params</span> <span class="o">=</span> <span class="n">options</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  <span class="kd">local</span> <span class="k">function</span> <span class="nf">doIt</span><span class="p">()</span>
</span><span class='line'>      <span class="n">func</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  <span class="n">timer</span><span class="p">.</span><span class="n">oldPerformWithDelay</span><span class="p">(</span><span class="n">interval</span><span class="p">,</span> <span class="n">doIt</span><span class="p">,</span> <span class="n">iterations</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="kd">local</span> <span class="k">function</span> <span class="nf">restartLevel</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
</span><span class='line'>  <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">boolVal&quot;</span><span class="p">,</span> <span class="n">params</span><span class="p">.</span><span class="n">boolVal</span><span class="p">)</span>
</span><span class='line'>  <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">stringVal&quot;</span><span class="p">,</span> <span class="n">params</span><span class="p">.</span><span class="n">stringVal</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">timer</span><span class="p">.</span><span class="n">performWithDelay</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="n">restartLevel</span><span class="p">,</span> <span class="p">{</span><span class="n">boolVal</span> <span class="o">=</span> <span class="kc">true</span><span class="p">,</span> <span class="n">stringVal</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="s">yo&quot;</span><span class="p">})</span>
</span><span class='line'><span class="n">timer</span><span class="p">.</span><span class="n">performWithDelay</span><span class="p">(</span><span class="mi">1500</span><span class="p">,</span> <span class="n">restartLevel</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="p">{</span><span class="n">boolVal</span> <span class="o">=</span> <span class="kc">false</span><span class="p">,</span> <span class="n">stringVal</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="s">iterations!&quot;</span><span class="p">})</span>
</span></code></pre></td></tr></table></div></figure>


<p>All I&#8217;ve done is move the closure, which is called <code>doIt</code>, into the new <code>timer.performWithDelay</code> function. <code>doIt</code> calls the function passed to performWithDelay, along with whatever parameters have been provided.</p>

<p>Because the original <code>timer.performWithDelay</code> accepts an optional third argument (the number of iterations), the new function needs to account for that. To do this, the new function checks to see if the third value passed to it is a number. If it is, then it&#8217;s assumed that this is the number of iterations, and the fourth value is the table of parameters to pass along. If the third value is not a number, then plan for a single iteration, and assign the third value passed to the new function to the parameters to be passed on. You can see the iterations in action in the second <code>performWithDelay</code> call.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Corona Physics: Forced Horizontal/Vertical Bouncing]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/09/01/corona-physics-forced-bouncing/"/>
    <updated>2011-09-01T14:54:00-05:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/09/01/corona-physics-forced-bouncing</id>
    <content type="html"><![CDATA[<p>This recent article on the Ansca web site about <a href="http://blog.anscamobile.com/2011/08/solutions-to-common-physics-challenges/">solutions to common physics challenges</a> is helpful, but as one commenter notes, the issue of objects &#8220;sticking&#8221; to walls isn&#8217;t easily solved by using the suggestions provided in the article. The commenter notes that the Corona Physics API doesn&#8217;t expose the collision properties required to properly handle these sticky (ha ha) situations. So here&#8217;s how you can hack around the problem when your walls are completely horizontal or vertical (situations where the collision is with non-horizontal/vertical objects are somewhat more complicated, and I&#8217;ll deal with those in a subsequent blog post).</p>

<!--more-->


<p>Here&#8217;s a simple <code>main.lua</code> that creates four walls and a bouncing ball:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">physics</span> <span class="o">=</span> <span class="nb">require</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">physics&quot;</span><span class="p">)</span>
</span><span class='line'><span class="n">physics</span><span class="p">.</span><span class="n">start</span><span class="p">()</span>
</span><span class='line'><span class="n">physics</span><span class="p">.</span><span class="n">setGravity</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c1">-- create wall objects</span>
</span><span class='line'><span class="kd">local</span> <span class="n">topWall</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newRect</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">display</span><span class="p">.</span><span class="n">contentWidth</span><span class="p">,</span> <span class="mi">10</span> <span class="p">)</span>
</span><span class='line'><span class="kd">local</span> <span class="n">bottomWall</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newRect</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="n">display</span><span class="p">.</span><span class="n">contentHeight</span> <span class="o">-</span> <span class="mi">10</span><span class="p">,</span> <span class="n">display</span><span class="p">.</span><span class="n">contentWidth</span><span class="p">,</span> <span class="mi">10</span> <span class="p">)</span>
</span><span class='line'><span class="kd">local</span> <span class="n">leftWall</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newRect</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="n">display</span><span class="p">.</span><span class="n">contentHeight</span> <span class="p">)</span>
</span><span class='line'><span class="kd">local</span> <span class="n">rightWall</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newRect</span><span class="p">(</span> <span class="n">display</span><span class="p">.</span><span class="n">contentWidth</span> <span class="o">-</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="n">display</span><span class="p">.</span><span class="n">contentHeight</span> <span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c1">-- make them physics bodies</span>
</span><span class='line'><span class="n">physics</span><span class="p">.</span><span class="n">addBody</span><span class="p">(</span><span class="n">topWall</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">static&quot;</span><span class="p">,</span> <span class="p">{</span><span class="n">density</span> <span class="o">=</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">friction</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">bounce</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">isSensor</span> <span class="o">=</span> <span class="kc">false</span><span class="p">})</span>
</span><span class='line'><span class="n">physics</span><span class="p">.</span><span class="n">addBody</span><span class="p">(</span><span class="n">bottomWall</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">static&quot;</span><span class="p">,</span> <span class="p">{</span><span class="n">density</span> <span class="o">=</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">friction</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">bounce</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">isSensor</span> <span class="o">=</span> <span class="kc">false</span><span class="p">})</span>
</span><span class='line'><span class="n">physics</span><span class="p">.</span><span class="n">addBody</span><span class="p">(</span><span class="n">leftWall</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">static&quot;</span><span class="p">,</span> <span class="p">{</span><span class="n">density</span> <span class="o">=</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">friction</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">bounce</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">isSensor</span> <span class="o">=</span> <span class="kc">false</span><span class="p">})</span>
</span><span class='line'><span class="n">physics</span><span class="p">.</span><span class="n">addBody</span><span class="p">(</span><span class="n">rightWall</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">static&quot;</span><span class="p">,</span> <span class="p">{</span><span class="n">density</span> <span class="o">=</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">friction</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">bounce</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">isSensor</span> <span class="o">=</span> <span class="kc">false</span><span class="p">})</span>
</span><span class='line'>
</span><span class='line'><span class="c1">-- create a ball and set it in motion</span>
</span><span class='line'><span class="n">ball</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newCircle</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">15</span> <span class="p">)</span>
</span><span class='line'><span class="n">ball</span><span class="p">.</span><span class="n">x</span><span class="p">,</span> <span class="n">ball</span><span class="p">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">contentWidth</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">display</span><span class="p">.</span><span class="n">contentHeight</span> <span class="o">-</span> <span class="mi">80</span>
</span><span class='line'><span class="n">physics</span><span class="p">.</span><span class="n">addBody</span><span class="p">(</span><span class="n">ball</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">dynamic&quot;</span><span class="p">,</span> <span class="p">{</span><span class="n">density</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">friction</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">bounce</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">isSensor</span> <span class="o">=</span> <span class="kc">false</span><span class="p">,</span> <span class="n">radius</span> <span class="o">=</span> <span class="mi">15</span><span class="p">})</span>
</span><span class='line'><span class="n">ball</span><span class="p">.</span><span class="n">isBullet</span> <span class="o">=</span> <span class="kc">true</span>
</span><span class='line'><span class="n">ball</span><span class="p">:</span><span class="n">applyForce</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="kd">local</span> <span class="k">function</span> <span class="nf">onCollision</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
</span><span class='line'>  <span class="nb">print</span><span class="p">(</span><span class="n">event</span><span class="p">.</span><span class="n">phase</span><span class="p">,</span> <span class="n">ball</span><span class="p">:</span><span class="n">getLinearVelocity</span><span class="p">())</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">ball</span><span class="p">:</span><span class="n">addEventListener</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">collision&quot;</span><span class="p">,</span> <span class="n">onCollision</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>A force is applied to the ball, and when the ball hits the bottom wall, it will &#8216;stick&#8217;, which is not the behaviour that we want. As you&#8217;ll see from the output of the <code>onCollision</code> event handler, eventually, the <code>y</code> velocity becomes 0, meaning that the object is not moving up or down at all, which creates the effect of sticking.</p>

<p>One other thing you may notice from the terminal output: the <code>x</code> and <code>y</code> velocities follow a consistent pattern from the <code>began</code> phase of the event to the <code>ended</code> phase: one of the values will flip from positive to negative depending on which way the ball was moving before and after the collision. Here&#8217;s how it breaks down:</p>

<ul>
<li> Bounce to the left: the <code>x</code> value goes from negative to positive</li>
<li> Bounce to the right: the <code>x</code> value goes from positive to negative</li>
<li> Bounce downwards: the <code>y</code> value goes from negative to positive</li>
<li> Bounce upwards: the <code>y</code> value goes from positive to negative</li>
</ul>


<p>In the example code above, the <code>y</code> velocity toggles between 12.73 and -12.73 (I&#8217;m rounding off for the sake of convenience). Then, at some point instead of remaining at 12.73, it becomes 0. The ball is moving downward (positive <code>y</code> velocity) and instead of moving upward (negative <code>y</code> velocity), it stops moving up/down at all.</p>

<p>So the immediate solution presents itself: we know what the <code>y</code> velocity should be, so we can override the calculated value of zero and set it to what it should be. Here&#8217;s the new <code>resetLinearVelocity</code> function with the modified <code>onCollision</code> function:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="k">function</span> <span class="nf">resetLinearVelocity</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
</span><span class='line'>  <span class="kd">local</span> <span class="n">thisX</span><span class="p">,</span> <span class="n">thisY</span> <span class="o">=</span> <span class="n">ball</span><span class="p">:</span><span class="n">getLinearVelocity</span><span class="p">()</span>
</span><span class='line'>  <span class="k">if</span> <span class="n">thisY</span> <span class="o">==</span> <span class="mi">0</span> <span class="k">then</span>
</span><span class='line'>      <span class="n">thisY</span> <span class="o">=</span> <span class="o">-</span><span class="n">ball</span><span class="p">.</span><span class="n">lastY</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  <span class="k">if</span> <span class="n">thisX</span> <span class="o">==</span> <span class="mi">0</span> <span class="k">then</span>
</span><span class='line'>      <span class="n">thisX</span> <span class="o">=</span> <span class="o">-</span><span class="n">ball</span><span class="p">.</span><span class="n">lastX</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  <span class="n">ball</span><span class="p">:</span><span class="n">setLinearVelocity</span><span class="p">(</span><span class="n">thisX</span><span class="p">,</span> <span class="n">thisY</span><span class="p">)</span>
</span><span class='line'>  <span class="n">ball</span><span class="p">.</span><span class="n">lastX</span><span class="p">,</span> <span class="n">ball</span><span class="p">.</span><span class="n">lastY</span> <span class="o">=</span> <span class="n">thisX</span><span class="p">,</span> <span class="n">thisY</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="kd">local</span> <span class="k">function</span> <span class="nf">onCollision</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
</span><span class='line'>  <span class="n">timer</span><span class="p">.</span><span class="n">performWithDelay</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">resetLinearVelocity</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p><code>onCollision</code> will call <code>resetLinearVelocity</code> after a delay of 0 milliseconds - all this does is force Corona to call the function on the next frame. We wait a frame so that we don&#8217;t interfere with the calculations being performed by the physics engine, and so that we don&#8217;t have to wait for another collision before resetting any values that need it (try calling the <code>resetLinearVelocity</code> function immediately to see the difference).</p>

<p>If the <code>x</code> or <code>y</code> velocity is zero, we just need to set them to the signed opposite of their last non-zero value. The <code>ball</code> object has a couple of new properties, <code>lastX</code> and <code>lastY</code> that stores the values for the <code>x</code> and <code>y</code> velocities, respectively, from the previous collision. If either of the new velocities are zero, then we manually set the linear velocity to the proper values.</p>

<p>All of this is being done by the <code>resetLinearVelocity</code> function. Here&#8217;s what it does, in order:</p>

<ol>
<li>It gets the current linear velocity values, storing them in local variables.</li>
<li>It checks to see if the current <code>y</code> velocity (stored in <code>thisY) is 0. If it is, then it's set to the negative value of</code>ball.lastY`.</li>
<li>The same check is done for the <code>x</code> velocity.</li>
<li><code>:setLinearVelocity()</code> is called. If <code>thisX</code> and/or <code>thisY</code> have changed, then this means the ball should have bounced after the last collision, but didn&#8217;t. This will force the bounce.</li>
<li>The <code>x</code> and <code>y</code> velocities are stored for the next time there&#8217;s a collision.</li>
</ol>


<p>One inefficiency in the code is that it will call <code>:setLinearVelocity()</code> even if neither value has changed, so you may want to add some conditional logic to only make that call when necessary - I haven&#8217;t tested it, but my suspicion is that a couple of <code>if</code> statements will have less overhead than the <code>:setLinearVelocity</code> call, especially in an app with a lot of collisions.</p>

<p>Keep one thing in mind: the physics engine is meant to more or less accurately emulate real-world physics. My guess is that the real problem is that, in real life, the ball <em>is supposed to stop bouncing</em>. The <code>y</code> velocity is simply not great enough for an object of that (simulated) size to bounce off the wall. For example, if you comment out the <code>collision</code> event listener and if you play around with the values used in the <code>:applyForce()</code> function call (e.g. <code>ball:applyForce(100, 30)</code>), you&#8217;ll find that the workaround isn&#8217;t even required, and the visual effect of the bouncing ball in this case isn&#8217;t significantly different from the lesser initial <code>y</code> velocity of 10. However, just because Box2D is meant to model the real world, doesn&#8217;t mean you have to use it to do that, so hopefully you can use this to get you out of the &#8216;sticky&#8217; situations you may sometimes find yourself in with the Corona physics engine.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Fastest way to remove all Event Listeners]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/08/24/remove-all-listeners/"/>
    <updated>2011-08-24T22:17:00-05:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/08/24/remove-all-listeners</id>
    <content type="html"><![CDATA[<p>Removing the event listeners that you&#8217;ve created is key to avoiding memory leaks in your Corona applications. Here&#8217;s a simple little function that will remove all of the event listeners attached to a particular object.</p>

<!--more-->




<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="k">function</span> <span class="nf">removeAllListeners</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
</span><span class='line'>  <span class="n">obj</span><span class="p">.</span><span class="n">_functionListeners</span> <span class="o">=</span> <span class="kc">nil</span>
</span><span class='line'>  <span class="n">obj</span><span class="p">.</span><span class="n">_tableListeners</span> <span class="o">=</span> <span class="kc">nil</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The function takes as its argument the object for which you want to remove the listeners. This would typically be a display object, but could also be the global <code>Runtime</code> object - basically anything that could have event listeners attached to it.</p>

<p>After looking at that function, you&#8217;re probably asking, &#8220;That&#8217;s great, but where did these <code>_functionListeners</code> and <code>_tableListeners</code> tables come from, and how did they get the information about the listeners in the first place?&#8221; They&#8217;re built-in properties of objects that listen for events. Since they start with an underscore, they&#8217;re meant to be somewhat private properties, but they&#8217;re completely accessible. In an object that has no event listeners, those tables don&#8217;t exist, so by setting their values to <code>nil</code>, we&#8217;re effectively returning the object to its pre-event listener state.</p>

<p>This sort of thing would be handy if you&#8217;re creating objects dynamically, such that they may have a different set of event listeners depending on their specific role. Rather than create a separate property to track the event listeners you need to remove, you can use the above function to take care of this clean-up for you. One word of warning: since these are semi-private properties that aren&#8217;t really meant to be exposed, it&#8217;s possible that Ansca will at some point change how things work under the hood, and this may stop working. So use at your own risk, etc., etc.</p>

<p>(Also, I&#8217;m not suggesting the approach I just described is the best way to architect your application, but it&#8217;s possible. And you may want to use this function for extra assurance that you got all the event listeners that were added. I&#8217;m just reporting my findings here; what you do with it is up to you!)</p>

<p>If you&#8217;re interested: I found out about these tables while playing around with the debugger, specifically the <code>dump</code> command, which as I mentioned in my <a href="www.ludicroussoftware.com/blog/2011/08/22/using-the-corona-debugger/">last post</a>, can yield some interesting findings (There&#8217;s also this post on the <a href="http://developer.anscamobile.com/forum/2010/01/05/inspecting-simulator">Ansca forums</a> that shows a whole bunch of information via straight Lua code, rather than using the debugger). If you attach function listeners to an object, you&#8217;ll see something like this in the debugger:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="o">&gt;</span> <span class="n">dump</span> <span class="n">square</span>
</span><span class='line'><span class="n">square</span><span class="p">(</span><span class="n">table</span><span class="p">:</span> <span class="mh">0x1c320e0</span><span class="p">)</span> <span class="o">=</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">_proxy</span> <span class="o">=</span> <span class="n">userdata</span><span class="p">:</span> <span class="mh">0x1c320d4</span>
</span><span class='line'>  <span class="n">_class</span> <span class="o">=</span> <span class="n">table</span><span class="p">:</span> <span class="mh">0x1c1d8f0</span>
</span><span class='line'>  <span class="n">_functionListeners</span> <span class="o">=</span> <span class="n">table</span><span class="p">:</span> <span class="mh">0x1c36580</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>And there&#8217;s <code>_functionListeners</code>. <code>dump</code> that, and here&#8217;s what you see:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="o">&gt;</span> <span class="n">dump</span> <span class="n">square</span><span class="p">.</span><span class="n">_functionListeners</span>
</span><span class='line'><span class="n">square</span><span class="p">.</span><span class="n">_functionListeners</span><span class="p">(</span><span class="n">table</span><span class="p">:</span> <span class="mh">0x1c36580</span><span class="p">)</span> <span class="o">=</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">touch</span> <span class="o">=</span> <span class="n">table</span><span class="p">:</span> <span class="mh">0x1c37590</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>As you can see, the <code>_functionListeners</code> table uses the name of the event to listen for as the key, and the value is a table. You might think that the value would be a function instead of a table, but since you can attach multiple listeners for a single event, Corona uses a table to store all of those listeners. Here&#8217;s the output from an object that has multiple <code>touch</code> event listeners:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="o">&gt;</span> <span class="n">dump</span> <span class="n">square</span><span class="p">.</span><span class="n">_functionListeners</span><span class="p">.</span><span class="n">touch</span>
</span><span class='line'><span class="n">square</span><span class="p">.</span><span class="n">_functionListeners</span><span class="p">.</span><span class="n">touch</span><span class="p">(</span><span class="n">table</span><span class="p">:</span> <span class="mh">0x1a27820</span><span class="p">)</span> <span class="o">=</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="mi">1</span> <span class="o">=</span> <span class="n">function</span><span class="p">:</span> <span class="mh">0x1a23420</span>
</span><span class='line'>  <span class="mi">2</span> <span class="o">=</span> <span class="n">function</span><span class="p">:</span> <span class="mh">0x1a23910</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>So, you could use <code>pairs()</code> and <code>ipairs()</code> on these tables to iterate through them and remove listeners selectively. You can also use <code>table.insert()</code> to add event listeners if you so chose. Setting the tables to <code>nil</code> results in the wholesale removal of listeners.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using the Corona Debugger]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/08/22/using-the-corona-debugger/"/>
    <updated>2011-08-22T14:32:00-05:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/08/22/using-the-corona-debugger</id>
    <content type="html"><![CDATA[<p>I have to confess: I do not write bug-free Lua code (I know, shocking). Much of the time, judicious use of <code>print()</code> statements will be enough to help me figure out just where things have gone off the rails. But every so often, that&#8217;s not enough, and so I turn to the debugger that ships with the Corona SDK - that&#8217;s right, there&#8217;s a debugger hidden away in the SDK (actually a version of <a href="http://www.keplerproject.org/remdebug/index.html">RemDebug</a>). While it doesn&#8217;t have a nice GUI to make working with it a little easier - you&#8217;ll spend quite a bit of time on the command line - it has a lot of functionality. This post will show you how to start and use the debugger.</p>

<!--more-->


<h2>Starting the Debugger</h2>

<p>The debugger is part of the Corona SDK, and needs to be invoked from the command line. So, fire up Terminal.app, make sure there are no instances of the Corona Simulator already running, and from the command prompt type:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c"># replace /Applications/CoronaSDK with the actual path to your SDK installation</span>
</span><span class='line'>/Applications/CoronaSDK/debugger
</span></code></pre></td></tr></table></div></figure>


<p>The debugger will start the Corona Simulator and prompt you to run the program you want to debug. Once you&#8217;ve selected your program to debug, the debugger will pause execution so you can set it up to, well, debug your program. If you just tell the debugger to continue running the program at this point - we&#8217;ll see how in a little bit - it actually won&#8217;t be terribly useful. That&#8217;s because you haven&#8217;t told the debugger to do anything. You can do this by setting <em>breakpoints</em> and <em>watch expressions</em>. We&#8217;ll look at both of these - and more! - but first, here&#8217;s the program that I&#8217;m using for the purpose of this tutorial:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">square</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newRect</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">10</span> <span class="p">)</span>
</span><span class='line'><span class="n">square</span><span class="p">:</span><span class="n">setFillColor</span><span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="kd">local</span> <span class="k">function</span> <span class="nf">foo</span><span class="p">()</span>
</span><span class='line'>  <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">foo&quot;</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="kd">local</span> <span class="k">function</span> <span class="nf">moveSquare</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
</span><span class='line'>  <span class="n">square</span><span class="p">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">square</span><span class="p">.</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span>
</span><span class='line'>  <span class="n">foo</span><span class="p">()</span>
</span><span class='line'>  <span class="k">if</span> <span class="n">square</span><span class="p">.</span><span class="n">x</span> <span class="o">==</span> <span class="mi">100</span> <span class="k">then</span>
</span><span class='line'>      <span class="n">Runtime</span><span class="p">:</span><span class="n">removeEventListener</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">enterFrame&quot;</span><span class="p">,</span> <span class="n">moveSquare</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">Runtime</span><span class="p">:</span><span class="n">addEventListener</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">enterFrame&quot;</span><span class="p">,</span> <span class="n">moveSquare</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>The code creates a red square positioned at the <code>0, 0</code> point on the screen and an <code>enterFrame</code> listener that moves the square three pixels to the right on every frame. When <code>square.x</code> is equal to <code>100</code>, the <code>enterFrame</code> listener is removed. (<code>foo()</code> is just a dummy function to help demonstrate some of the capabilities of the debugger.) You can probably already see the bug in this program - since 100 isn&#8217;t evenly divisible by 3, the square never stops moving to the right. The simplest approach in this case would be to stick a <code>print(square.x)</code> statement in the function to see the values of <code>square.x</code>. With that approach you end up with a ton of output in the terminal, and in even a moderately complex program you probably have a bunch of other <code>print()</code> statements, so you may not even see the output as it zooms by the screen mixed in with other output. So, we&#8217;ll use the debugger to look at a couple of different ways of debugging this application: breakpoints and expressions.</p>

<h2>Breakpoints</h2>

<p>A breakpoint is a point where you want to pause the execution of the program. When the program hits a breakpoint, it will stop in its tracks to let you use the debugger to examine the current state of the application to figure out what&#8217;s going wrong.</p>

<p>The command for setting breakpoints uses the following syntax:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; setb &lt;file&gt; &lt;line&gt;
</span></code></pre></td></tr></table></div></figure>


<p>(<strong>Note</strong>: do <strong>not</strong> enter the greater than symbol; that symbol is the prompt from the debugger.)</p>

<p>You can create as many breakpoints as you like, on any line of any Lua file in your project folder. Just make sure not to put them on blank lines, or they&#8217;ll be ignored. This command will create a breakpoint inside <code>moveSquare()</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; setb main.lua 9
</span></code></pre></td></tr></table></div></figure>


<p>Once you&#8217;ve created the breakpoint(s), you can tell the debugger to continue running the program. The command to do this is your choice of <code>run</code>, <code>continue</code>, or simply <code>c</code> (short for <code>continue</code>). After you type <code>c</code> in the debugger and hit enter, the program will run until it hits a breakpoint. When that happens, the program will pause execution and the debugger will tell you where this happened. So for the breakpoint already set, you&#8217;ll see this message from the debugger:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>Paused at file main.lua line 9
</span></code></pre></td></tr></table></div></figure>


<p>At this point, we can examine the value of <code>square.x</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; print square.x
</span></code></pre></td></tr></table></div></figure>


<p>The only difference between this and the standard Lua <code>print()</code> function is that in the debugger you must omit the parentheses; otherwise you can print whatever value you wish to examine. You can abbreviate the command to just the letter &#8216;p&#8217;, so <code>p square.x</code> will give you the same output.</p>

<p>Once execution has stopped at a breakpoint, you can &#8220;step&#8221; through the code using the following commands:</p>

<h3><code>step</code> (<code>si</code>)</h3>

<p><code>step</code> (or <code>si</code> for short) will advance your progress through the program one line at a time. The short form of the command stands for &#8220;step into&#8221;. This means that when the debugger encounters a function call, it will &#8220;step into&#8221; that function and allow you to step through its code.</p>

<p>You can see what I mean by entering <code>si</code> a couple of times in the debugger after execution has been paused at line 9. If you do that, your output will look like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; setb main.lua 9
</span><span class='line'>&gt; c
</span><span class='line'>Paused at file main.lua line 9
</span><span class='line'>&gt; si
</span><span class='line'>Paused at file main.lua line 10
</span><span class='line'>&gt; si
</span><span class='line'>Paused at file main.lua line 5
</span><span class='line'>&gt; si
</span><span class='line'>foo
</span></code></pre></td></tr></table></div></figure>


<p>The program paused at line 5 in main.lua, which is the <code>print("foo")</code> statement inside <code>foo()</code>. When the debugger encountered the call to <code>foo()</code> on line 10, it jumped to that function to let us step through it. Once it&#8217;s finished there, program flow will return to the function that called <code>foo()</code>, just as it normally would.</p>

<p>(Also, you can see that output from <code>print()</code> statements in your code will be directed to the debugger.)</p>

<p>But what if you know that <code>foo()</code> is unproblematic, and you just want to let it do its thing without having to bother stepping through that function? That&#8217;s where <code>over</code> comes in handy.</p>

<h3><code>over</code> (<code>so</code>)</h3>

<p><code>over</code> is the companion to <code>step</code>, and as soon as I tell you that <code>so</code> is short for &#8220;step over&#8221;, you&#8217;ll probably guess what <code>so</code> does. It steps on to the next line in the program, but if that line is a function call, it simply executes that function and continues stepping through the code. In other words, it causes the debugger to &#8220;step over&#8221; function calls. So the output in the debugger for <code>so</code> will look like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>Paused at file main.lua line 9
</span><span class='line'>&gt; so
</span><span class='line'>Paused at file main.lua line 10
</span><span class='line'>&gt; so
</span><span class='line'>foo
</span><span class='line'>Paused at file main.lua line 11
</span><span class='line'>&gt;
</span></code></pre></td></tr></table></div></figure>


<p>We can see that <code>foo()</code> was called because of the &#8220;foo&#8221; output from the function, but we didn&#8217;t have to manually step through it while debugging. This is handy when we&#8217;re sure that a certain function isn&#8217;t relevant to whatever bug we&#8217;re hunting down.</p>

<h3><code>run</code>/<code>continue</code> (<code>c</code>)</h3>

<p>The last command you can use to control the debugger after you&#8217;ve hit a breakpoint is the <code>run</code> or <code>continue</code> command (<code>c</code> for short). This command simply lets the program continue to run until it hits another breakpoint or watch expression. This is very handy if you&#8217;re only interested in the code immediately after your breakpoint, and don&#8217;t want to have to step through all the lines of code between it and the next breakpoint. If you enter the <code>c</code> command in the debugger, you&#8217;ll see this output from the program:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>Paused at file main.lua line 9
</span><span class='line'>&gt; c
</span><span class='line'>foo
</span><span class='line'>Paused at file main.lua line 9
</span><span class='line'>&gt; c
</span><span class='line'>foo
</span><span class='line'>Paused at file main.lua line 9
</span></code></pre></td></tr></table></div></figure>


<p>As you can see from the output, after entering the <code>continue</code> command, the program simply continues to run. <code>foo()</code> is executed, and then when the program hits the breakpoint in <code>moveSquare()</code> again - remember, this is the handler function for the <code>enterFrame</code> event, so it&#8217;s getting called 30 times per second, and that breakpoint will get triggered every time - the program is paused.</p>

<p>(If you haven&#8217;t noticed it already, check the Corona Simulator window as you enter the continue command; you&#8217;ll see the red square move across the screen a little bit after every time you hit &#8216;enter&#8217;. That&#8217;s because the code is actually running, so the square is being animated across the screen.)</p>

<h3>Deleting Breakpoints</h3>

<p>If you&#8217;re finished with a breakpoint, but you&#8217;re not done debugging your code, you can delete it using the following command:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; delb &lt;file&gt; &lt;line&gt;
</span></code></pre></td></tr></table></div></figure>


<p>So to delete our breakpoint, we would enter:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; delb main.lua 9
</span></code></pre></td></tr></table></div></figure>


<p>Delete all breakpoints with:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; delallb
</span></code></pre></td></tr></table></div></figure>


<h3>Listing Breakpoints</h3>

<p>The command to see all of the breakpoints that you&#8217;ve created in your debugging session is:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; listb
</span></code></pre></td></tr></table></div></figure>


<p>As you can see, breakpoints can be extremely handy. But given the current example, you can also see a potential downside: the bug in our code occurs somewhere around the time when <code>square.x</code> is about 100. That means having to sit there and enter <code>continue</code> about 30 times before we finally get to the point in the program that really interests us. That would be tedious. Happily, there&#8217;s a nice way to skip ahead in the program to get closer to where we want to be, and that&#8217;s by using watch expressions.</p>

<h2>Watch Expressions</h2>

<p>A watch expression is a Lua expression that is being constantly evaluated by the debugger. As soon as it evaluates to <code>true</code>, execution of the program is paused. So you can think of it kind of like a breakpoint, where instead of specifying a particular line where you want to stop the program, you give the debugger a specific condition.</p>

<p>In our example program, we&#8217;re expecting <code>square.x</code> to be equal to 100 at some point, but that&#8217;s not happening. So we want to know the value of <code>square.x</code> around that point. The easiest way to do that would be to be able to pause the program as soon as <code>square.x</code> is greater than 99. We can create a watch expression to do that:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; setw square.x &gt; 99
</span><span class='line'>Inserted watch exp no. 1
</span><span class='line'>&gt;
</span></code></pre></td></tr></table></div></figure>


<p>The debugger tells us the number of the watch expression we&#8217;ve created. The particular expression can be any valid Lua condition to check - as far as I know, if you can use the expression in a Lua <code>if</code> statement, you can use it in the debugger. So if, for example, we were also fading out the square, we could enter a watch expression like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>     setw square.x &gt; 99 and square.alpha &lt; 0.5
</span></code></pre></td></tr></table></div></figure>


<p>Returning to our original expression, now we can continue the program and wait until our condition is met:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; c
</span><span class='line'>foo
</span><span class='line'><span class="c"># lots more foos snipped for the sake of brevity</span>
</span><span class='line'>foo
</span><span class='line'>Paused at file main.lua line 10 <span class="o">(</span>watch expression 1: <span class="o">[</span>square.x &gt; 99<span class="o">])</span>
</span><span class='line'>&gt;
</span></code></pre></td></tr></table></div></figure>


<p>Now we&#8217;re talking! The program just did whatever it was supposed to do until our condition is true. Now that <code>square.x</code> is greater than 99, the program is paused. So we can <code>print</code> the value we&#8217;re interested in and do whatever else we need to help figure out what&#8217;s going on. Couple things to keep in mind:</p>

<ol>
<li><p>As long as a watch expression is true, the debugger will pause execution on every line of the program until either the expression is no longer true or the watch expression is removed. In this sense, it kind of acts like having a breakpoint on every line.</p></li>
<li><p>Your watch expression can specify local variables. So if there&#8217;s a particular variable that&#8217;s within the scope of a function, and you want to set a watch expression on that, go right ahead. It doesn&#8217;t matter if the local variable doesn&#8217;t exist when you create the watch expression. Keep in mind that if you&#8217;re in the habit of using the same name for local variables in different functions, the watch expression will apply to all of them.</p></li>
</ol>


<h2>Deleting Watch Expressions</h2>

<p>To delete a watch expression, use the following command:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; delw &lt;number of expression to delete&gt;
</span></code></pre></td></tr></table></div></figure>


<p>The expression number is the number provided to you by the debugger after you&#8217;ve entered the expression. So in the case of our expression, which we were told was &#8220;exp no. 1&#8221;, we would enter:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; delw 1
</span></code></pre></td></tr></table></div></figure>


<p>If you want to see all the watch expressions you&#8217;ve created:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; listw
</span></code></pre></td></tr></table></div></figure>


<p>If you want to delete all of the watch expressions you&#8217;ve created:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; delallw
</span></code></pre></td></tr></table></div></figure>


<h2>Other Helpful Commands</h2>

<p>There are a few other commands that may be helpful in your debugging efforts. As before, the short form of the command is in parentheses:</p>

<ul>
<li><p><code>locals</code> (<code>l</code>) - shows you the names and values of all of the local variables in the program.</p></li>
<li><p><code>backtrace</code> (<code>bt</code>) - shows the function call stack (similar to what you see in the terminal when you have an error in your code).</p></li>
<li><p><code>eval</code> - basically the same as <code>print</code></p></li>
<li><p><code>exec</code> - this command allows you to execute any chunk of valid Lua code in the current context (i.e. with access to all local variables, etc.). For example, when our example code is stopped by a breakpoint or watch expression, you could move the square to any arbitrary <code>x</code> or <code>y</code> position:</p>

<p>&#8220;`bash</p>

<pre><code>&gt; exec square.y = 200
nil
</code></pre>

<p>&#8220;`</p>

<p>Since a chunk in Lua can be multiple lines of code, one way to enter a chunk would be to type it on one line, separating each statement with a semi-colon. Remember: in Lua a semi-colon is an <em>optional</em> end-of-line marker - you don&#8217;t need to put the semi-colon at the end of a line if the line contains only one statement. But if you want to put multiple statements on a single line, you must separate them with a semi-colon. For instance, the following will create a large green square on the screen:</p>

<p>&#8220;`bash</p>

<pre><code>&gt; exec local rect = display.newRect(100, 100, 100, 100); rect:setFillColor(0, 255, 0)
nil
</code></pre>

<p>&#8220;`</p></li>
<li><p><code>dump</code> (<code>d</code>) - this is like <code>print</code> on steroids. It will give you detailed information about whatever variable you want. For example, when execution of the test program is paused in the <code>moveSquare</code> function, you can dump the value of any variable that exists at that point in time (remember, use <code>locals</code>/<code>l</code> to get a list of those). So you can take a look at the event object that gets passed to <code>moveSquare</code> like this:</p>

<p>&#8220;`bash</p>

<pre><code>&gt; dump event
event(table: 0x2a443e0) =
{
    name = "enterFrame"
    time = 8817.956
}
</code></pre>

<p>&#8220;`</p>

<p>This can give you access to a lot of detailed information about the current state of your application (and also lets you discover some potentially interesting things about what&#8217;s going on under the hood in Corona, if you&#8217;re the nosy type and start checking out anything that comes to mind).</p></li>
<li><p><code>help</code> - a list of all the commands available in the debugger.</p></li>
</ul>


<p>Incidentally, some of the functionality in the debugger seems to be added on top of RemDebug, such as being able to list local variables and the backtrace. (One thing that has me confused is the <code>frame</code> command - not really sure what that one does, but I&#8217;d like to find out one day.) I think that&#8217;s pretty cool - they&#8217;ve put effort into something that ships with the SDK and that most users will probably never touch.</p>

<p>(Maybe if you all start to use the debugger and submit suggestions for improving it, we&#8217;ll see even more cool new features added, like having breakpoints and watch expressions persist between debugging sessions, the ability to restart a debugging session without having to exit out of Simulator and debugger completely first, and maybe a nice GUI on top of it all.)</p>

<h2>Conclusion</h2>

<p>A lengthy post, to put it mildly, but I hope you found it interesting and educational. The debugger hides a lot of power behind a not very user-friendly interface, and I think that the time spent getting familiar with it will easily repay itself the first time you can use it to track down a particularly nasty bug.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Corona Ads API Tutorial]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/08/17/corona-ads-api-tutorial/"/>
    <updated>2011-08-17T10:13:00-05:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/08/17/corona-ads-api-tutorial</id>
    <content type="html"><![CDATA[<p>With the recent release of the latest version of the <a href="http://www.anscamobile.com/corona/">Corona SDK</a>, Ansca has gone and made <a href="http://www.ludicroussoftware.com/blog/2010/06/03/admob-ad-support-in-corona/">my AdMob hack</a> obsolete - not that I&#8217;m complaining: it&#8217;s nice to see native ad support in Corona. I thought I&#8217;d kick the tires on it and put together a quick tutorial on how to set up ads in your app. There are a couple of gotchas that I encountered, so hopefully this will save you some hassle as you incorporate ads into your own app.</p>

<!--more-->


<p>If you want to see the results in action, I encourage you to grab <a href="https://market.android.com/details?id=com.ludicroussoftware.games.knightspuzzle">Knight&#8217;s Puzzle</a> from the Android Market. And click on some ads while you&#8217;re at it. My boys&#8217; RESP isn&#8217;t going to grow by itself, you know.</p>

<p>The first thing you need to do is create an <a href="http://www.inmobi.com">InMobi</a> account, as this is the only ad provider that is currently supported. That&#8217;s pretty straightforward so I&#8217;ll assume you don&#8217;t need to be walked through that.</p>

<p>Once that&#8217;s done, you can create your app from the &#8220;My Sites/Apps&#8221; screen:</p>

<p><img src="http://www.ludicroussoftware.com/images/2011/08/17/inmobi01.png"></p>

<p>One thing to bear in mind: <strong>once an app is created, you cannot delete it</strong>. I find this kind of annoying - you can see in the above screen shot that I tried creating an iOS test app, as well as a web app, when I was getting things set up. I&#8217;ll now be staring at these unneeded apps for the entire time I have an InMobi account. Ah well.</p>

<p>Here&#8217;s the information required for an Android app; the requirements for an iOS application are pretty much the same (substitute App Store URL for &#8220;Market URL&#8221;):</p>

<p><img src="http://www.ludicroussoftware.com/images/2011/08/17/inmobi02.png"></p>

<p>The package name is the reverse-DNS style name that you&#8217;ll use when you&#8217;re building your Android app from within Corona (e.g. <code>com.bar.foo.mygreatgame</code>). The Market URL follows a standard format: <code>http://market.android.com/details?id=</code> followed by the reverse-DNS package name. I&#8217;ll assume you don&#8217;t need me to explain the app name and description.</p>

<p>Once you&#8217;ve created your app, you&#8217;ll be provided with an App ID. You can find this on the &#8220;App Details&#8221; page. You&#8217;ll need this in a little bit to plug into your Corona application. Ignore the reminders to download the InMobi Android SDK.</p>

<p>A word of warning: before they start serving ads to your app, InMobi must approve the app. This seems to apply even when you set your ad code to test mode - more on that in a bit. When I first created the Knight&#8217;s Puzzle entry, the game wasn&#8217;t yet live in the Android Market, so I left the App URL empty. But, InMobi wouldn&#8217;t begin the approval process because I hadn&#8217;t provided all of the required information. Since the Market URL is easy enough to figure out in advance (see above for the formula), I plugged that in and waited to see what would happen. I received notice that my app had been rejected because of an invalid URL - this makes sense, given that the app wasn&#8217;t live yet (which I&#8217;d indicated when I first filled out the form), but it makes it kind of tough to test the ad integration before the app goes live. The other strange thing was that it took about 12 days to find out that the app was rejected. I&#8217;m pretty sure that&#8217;s some sort of outlier - or at least I hope it is. And yes, I could have made an inquiry with InMobi to find out what was going on. In any event, after Knight&#8217;s Puzzle went live in the Android Market, I resubmitted the app to InMobi and it was approved within a matter of minutes, so there must be some sort of automatic check that happens after an app is submitted.</p>

<p>Unless I did something wrong - which is entirely possible - the upshot of this is that to capitalize on the initial downloads of your app, you want to make sure that your ad code is as bulletproof as possible. This is less of an issue with Android, because you can upload an update and it will get picked up almost immediately, but the lag with iOS approvals can cause problems. Happily, Ansca has done a great job of making the integration of ads as easy as possible. I hit only minor speed bumps, which I&#8217;ll mention in a little bit.</p>

<p>Here&#8217;s the basic code to get you ready to show ads:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">ads</span> <span class="o">=</span> <span class="nb">require</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">ads&quot;</span><span class="p">)</span>
</span><span class='line'><span class="kd">local</span> <span class="n">adNetwork</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="s">inmobi&quot;</span>
</span><span class='line'><span class="c1">-- the value for appID is the string assigned by InMobi after you create your app</span>
</span><span class='line'><span class="kd">local</span> <span class="n">appID</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="s">adflksadjflkasdfjoasgu&quot;</span> <span class="c1">-- not a real app ID</span>
</span><span class='line'><span class="n">ads</span><span class="p">.</span><span class="n">init</span><span class="p">(</span><span class="n">adNetwork</span><span class="p">,</span> <span class="n">appID</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>First, we include the ad module. Next, the name of the ad network and the app ID are set. Right now, InMobi is the only ad network supported by the Corona ad API, so you can certainly skip creating the variables and just set the <code>adNetwork</code> and <code>appID</code> as literals in the <code>ads.init()</code> call. Once that initial code is in place, then you can display an ad using the <code>ads.show()</code> method. Here&#8217;s where I started:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">showBanner</span> <span class="o">=</span> <span class="n">function</span><span class="p">()</span>
</span><span class='line'>  <span class="kd">local</span> <span class="n">adX</span><span class="p">,</span> <span class="n">adY</span> <span class="o">=</span> <span class="p">(</span><span class="n">display</span><span class="p">.</span><span class="n">contentWidth</span> <span class="o">-</span> <span class="mi">320</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">display</span><span class="p">.</span><span class="n">contentHeight</span> <span class="o">-</span> <span class="mi">48</span>
</span><span class='line'>  <span class="n">ads</span><span class="p">.</span><span class="n">show</span><span class="p">(</span> <span class="s2">&quot;</span><span class="s">banner320x48&quot;</span><span class="p">,</span> <span class="p">{</span> <span class="n">x</span> <span class="o">=</span> <span class="n">adX</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">adY</span><span class="p">,</span> <span class="n">interval</span> <span class="o">=</span> <span class="mi">60</span><span class="p">,</span> <span class="n">testMode</span> <span class="o">=</span> <span class="kc">false</span> <span class="p">}</span> <span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The first parameter passed to <code>ads.show()</code> is the size/type of ad you want to show. The second parameter is a table of optional values. The first values are the <code>x</code> and <code>y</code> positions of the ad, the third is the refresh rate, expressed in seconds (aside: this is in seconds, <code>timer.performWithDelay()</code> intervals are in milliseconds; that sort of inconsistency drives me bonkers).</p>

<p>The last argument sets the test mode - I&#8217;m still unsure of its purpose. During initial testing, the <code>ads.init()</code> call would fail because the app wasn&#8217;t live in the Android Market yet. Perhaps I&#8217;m missing something? The table is optional. Without it, you&#8217;ll get an ad appearing at the 0, 0 position of the screen that will refresh every ten seconds.</p>

<p>And that&#8217;s all you need to get your ads showing. The ads seem to behave like a native widget, rather than a Corona display object, which means that they&#8217;ll appear on top of whatever other display objects you have in your app. If you want to remove the ad at any point in your application, all you need to do is call <code>ads.hide()</code>.</p>

<p>You&#8217;ll notice that I&#8217;m requesting an iPhone-sized ad, since the API reference for <code>ads.show()</code> doesn&#8217;t list any Android-specific sizes. Accordingly, I initially tried to position the ad according to the size I requested (centred on the screen, 48 pixels up from the bottom of the screen). This is where I hit a couple of issues:</p>

<ol>
<li><p>It looks like InMobi will override the ad size you&#8217;ve requested in some cases. In this case, it displays a banner ad that is 480 pixels wide by 72 pixels high, instead of the size requested. So the positioning was off because of that.</p></li>
<li><p>After I modified the values of <code>adX</code> and <code>adY</code> accordingly, the ad was still showing up in the wrong position. It turned out that passing in the value of variables for the ad position wasn&#8217;t working, for some reason. Instead, I had to inline the calculation. Once I did that, the ad shows up exactly where I want it:</p></li>
</ol>


<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">showBanner</span> <span class="o">=</span> <span class="n">function</span><span class="p">()</span>
</span><span class='line'>  <span class="n">ads</span><span class="p">.</span><span class="n">show</span><span class="p">(</span> <span class="s2">&quot;</span><span class="s">banner320x48&quot;</span><span class="p">,</span> <span class="p">{</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">contentHeight</span> <span class="o">-</span> <span class="mi">72</span><span class="p">,</span> <span class="n">interval</span> <span class="o">=</span> <span class="mi">60</span><span class="p">,</span> <span class="n">testMode</span> <span class="o">=</span> <span class="kc">false</span> <span class="p">}</span> <span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>I didn&#8217;t change the value for the ad that I&#8217;m requesting. I probably should do this, or at the very least do some device sniffing and display a more appropriate ad (e.g. a larger ad for tablet devices).</p>

<p>Another thing to bear in mind: when an ad request fails, it fails silently, at least from the user&#8217;s perspective, and the area reserved for the ad will remain &#8216;blank&#8217;. So, you have to plan for the possibility that no ad will appear. What will the user see in your app if the space reserved for an ad isn&#8217;t showing an ad? In the case of Knight&#8217;s Puzzle, it&#8217;s not really a problem, since the game play is confined to a smaller area of the screen. But for other games, you may need to plan for the possibility that the ad request will fail.</p>

<p>For example, assume you&#8217;re building a game with a lot of enemy objects moving around the screen. If your code assumes an object is off-screen when it reaches the area reserved for an ad, but there&#8217;s no ad there, what happens? One approach may be to code your app as if the ad will never be there, and accept that when an object is in the ad space, it&#8217;s still &#8220;in play&#8221;. This can have consequences for some types of games, however - for example, you may not want your enemy planes shooting at the player from underneath an ad, since the bullets can look like they&#8217;re coming out of nowhere. So perhaps in that situation, the planes keep moving until they&#8217;re completely off the screen, but don&#8217;t shoot at the player unless they&#8217;re in the &#8216;safe&#8217; (i.e. non-ad) area. There may be other approaches depending on the specifics of your game, but it&#8217;s something you should keep in mind.</p>

<p>Handling this sort of thing would be easier if Corona dispatched an event indicating whether an ad request was successful, so hopefully we&#8217;ll see that added in a later version (or if InMobi served up house ads, the way AdMob does, then you wouldn&#8217;t need to worry about a less than 100% fill rate, since the unfilled requests could be filled with house ads).</p>

<p>All in all, I&#8217;m pretty pleased with the ad integration in Corona. Now go play Knight&#8217;s Puzzle so my kids can go to a good university!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[table_AS3: AS3 Array Methods for Lua]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/08/12/table_as3-as3-array-methods-for-lua/"/>
    <updated>2011-08-12T10:49:00-05:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/08/12/table_as3-as3-array-methods-for-lua</id>
    <content type="html"><![CDATA[<p>Inspired by the comment in <a href="http://portal-studios.com/category/game-development/">Dave Fulton&#8217;s blog post comparing the AS3 to Lua</a> that many of the methods of AS3&#8217;s Array class aren&#8217;t available in Lua, I&#8217;ve created a Lua module that ports those methods to Lua.</p>

<!--more-->


<p><a href="https://github.com/osadchuk/table_AS3">You can grab the code on GitHub</a>. It includes a sample main.lua that will show you how to use the functions, but if you&#8217;re familiar with <a href="http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/Array.html">AS3&#8217;s Array methods</a>, then you can probably skip that altogether.</p>

<p>If you are familiar with the Array class, then there are a few things you&#8217;ll still need to remember:</p>

<ul>
<li>I&#8217;ve renamed Array.concat to table.concat_as3, to avoid conflict with Lua&#8217;s built-in table.concat method.</li>
<li>Since tables in Lua are 1-based, wherever you&#8217;d see a 0 in the docs for the Array class, just substitute a 1.</li>
</ul>


<p>The library does some basic error checking (e.g. to make sure you&#8217;re calling the method on an actual table), and will use the same default values as the Array methods for optional parameters.</p>

<p>If you catch any bugs, or if there are any other Array methods you think should be added to the library, let me know in the comments!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Object-Oriented Function Calls in Corona]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/08/10/object-oriented-function-calls-in-corona/"/>
    <updated>2011-08-10T13:21:00-05:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/08/10/object-oriented-function-calls-in-corona</id>
    <content type="html"><![CDATA[<p>If you&#8217;re coming to Lua from another programming language, one of the things that will look unusual when you see sample code is manner in which many built-in functions are called.</p>

<!--more-->


<p>For example, if you want to insert a value into a table, you&#8217;ll often see the code written like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">myTable</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;</span><span class="s">a&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">b&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">d&quot;</span><span class="p">}</span>
</span><span class='line'><span class="nb">table.insert</span><span class="p">(</span><span class="n">myTable</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">c&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is different from, say, ActionScript, where the equivalent code would look like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='as3'><span class='line'><span class="kd">var</span> <span class="n">myArray</span><span class="p">:</span><span class="kt">Array</span> <span class="o">=</span> <span class="o">[</span><span class="s2">&quot;a&quot;</span><span class="o">,</span> <span class="s2">&quot;b&quot;</span><span class="o">,</span> <span class="s2">&quot;d&quot;</span><span class="o">];</span>
</span><span class='line'><span class="n">myArray</span><span class="o">.</span><span class="na">splice</span><span class="o">(</span><span class="mi">2</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="s2">&quot;c&quot;</span><span class="o">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>Ignoring the differing syntax between <code>insert</code> and <code>splice</code> for the moment, notice that the <code>splice</code> method is called directly on <code>myArray</code>, which determines the scope. In Lua, the <code>insert</code> method is akin to a static method of the <code>table</code> module, and the first argument is the table that will have the value inserted.</p>

<p>If you want your Lua to look more like ActionScript (or JavaScript, etc.), then all you need to do is make one simple change to your code:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">myTable</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;</span><span class="s">a&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">b&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">d&quot;</span><span class="p">}</span>
</span><span class='line'><span class="n">myTable</span><span class="p">:</span><span class="n">insert</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="s2">&quot;</span><span class="s">c&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Basically, using the colon before the function call is for convenience: it lets you skip having to pass the object as the first argument. When you&#8217;re writing functions in your own code, the colon serves the same purpose:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='lua'><span class='line'><span class="kd">local</span> <span class="n">myRect</span> <span class="o">=</span> <span class="n">display</span><span class="p">.</span><span class="n">newRect</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">function</span> <span class="nc">myRect</span><span class="p">.</span><span class="nf">rotateTo</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">angle</span><span class="p">)</span>
</span><span class='line'>  <span class="n">self</span><span class="p">.</span><span class="n">rotation</span> <span class="o">=</span> <span class="n">angle</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="c1">-- These two calls are functionally (ha ha!) equivalent</span>
</span><span class='line'><span class="c1">-- Pass myRect explicitly</span>
</span><span class='line'><span class="n">myRect</span><span class="p">.</span><span class="n">rotateTo</span><span class="p">(</span><span class="n">myRect</span><span class="p">,</span> <span class="mi">45</span><span class="p">)</span>
</span><span class='line'><span class="c1">-- myRect is the implicit first argument</span>
</span><span class='line'><span class="n">myRect</span><span class="p">:</span><span class="n">rotateTo</span><span class="p">(</span><span class="mi">45</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">function</span> <span class="nf">myRect</span><span class="p">:</span><span class="n">fadeTo</span><span class="p">(</span><span class="n">newAlpha</span><span class="p">)</span>
</span><span class='line'>  <span class="c1">-- &#39;self&#39; isn&#39;t declared, but it&#39;s the implicit first parameter because of</span>
</span><span class='line'>  <span class="c1">-- the colon in the function declaration</span>
</span><span class='line'>  <span class="n">self</span><span class="p">.</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">newAlpha</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="c1">-- again, these do the same thing</span>
</span><span class='line'><span class="n">myRect</span><span class="p">:</span><span class="n">fadeTo</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span>
</span><span class='line'><span class="n">myRect</span><span class="p">.</span><span class="n">fadeTo</span><span class="p">(</span><span class="n">myRect</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Custom Corona SDK Locations for TextMate Bundle]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/08/03/custom-corona-sdk-locations-for-textmate-bundle/"/>
    <updated>2011-08-03T14:16:00-05:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/08/03/custom-corona-sdk-locations-for-textmate-bundle</id>
    <content type="html"><![CDATA[<p>An old request for the Corona SDK TextMate bundle was the ability to run the Corona Simulator if it was installed to a custom location - if you&#8217;ve used the bundle before, you probably know that it assumes that the Corona SDK is installed to <code>/Applications/CoronaSDK</code>. With the recent release of a new version of the SDK, I&#8217;ve added the ability to specify which SDK to run when launching the Simulator from within TextMate.</p>

<!--more-->


<p>To employ this method, here are the steps to follow:</p>

<ol>
<li><p>You must have your Corona app set up in TextMate as a project.</p></li>
<li><p>When you have the project drawer open, make sure that no files in the drawer are selected (just click on some white space within the drawer).</p></li>
<li><p>Click the &#8216;info&#8217; button at the bottom of the project drawer. You should see this window pop up:
<img src="http://www.ludicroussoftware.com/images/2011/08/03/TMEnvVariables.png"></p></li>
<li><p>Click the &#8216;+&#8217; button to add a new environment variable. Name the variable <code>SDK_PATH</code> and set the value to the location of the SDK version you want to use for this project. Make sure to leave off the trailing slash! Like this:
<img src="http://www.ludicroussoftware.com/images/2011/08/03/TMEnvVariablesFilled.png"></p></li>
<li><p>Close the window and when you hit Cmd-R from within TextMate, it should launch your project in the SDK version you&#8217;ve specified. If nothing happens, then you probably have a typo somewhere. Right now the script isn&#8217;t doing much in the way of sanity or other checking, so it&#8217;s up to you to make sure things are set up properly.</p></li>
</ol>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Switching to Octopress]]></title>
    <link href="http://www.ludicroussoftware.com/blog/2011/08/02/switching-to-octopress/"/>
    <updated>2011-08-02T11:04:00-05:00</updated>
    <id>http://www.ludicroussoftware.com/blog/2011/08/02/switching-to-octopress</id>
    <content type="html"><![CDATA[<p>Yesterday I migrated ludicroussoftware.com over to <a href="http://www.octopress.com">Octopress</a> from WordPress. On the weekend, I stumbled across the fact that my site had suffered a spam injection attack. This isn&#8217;t the first time it&#8217;s happened - I found out a couple months ago that this had happened, and spent a good chunk of time finding the source of the problem and cleaning up the site. All seemed well. Then I noticed a few weeks ago that the same thing had happened; again, time spent dealing with the problem. This past weekend&#8217;s realization was the last straw. After I mentioned on Twitter what happened, I was pointed to Octopress, and after spending a couple of hours getting it all set up, I realized that it would be perfect for my needs.</p>

<!--more-->


<p>Octopress, if you&#8217;re not already familiar with it, is a framework for <a href="https://github.com/mojombo/jekyll/wiki">Jekyll</a>. If you&#8217;re not familiar with Jekyll, it&#8217;s a Static Site Generator. Instead of querying a database every time somebody requests a particular web page and building it on the fly, it pre-generates all of the pages on the site, and then rsync (or whatever you like) is used to sync the local build with the remote server. The result is nothing but plain old html, etc. on the site. Certainly, that doesn&#8217;t make my site hack-proof, but it has eliminated the primary attack vector, and that&#8217;s a good thing.</p>

<p>I liked using WordPress, and I&#8217;m glad that I can set up and configure a WP-based site if I need to. But in the long run, for my own needs, it&#8217;s overkill. The extensibility via plug-ins makes it attracting, but is probably also a significant cause of my problems.I run another WP-based site that has no plugins installed, and it&#8217;s clean of spam at the moment (correlation does not equal causation and all that, I know). I&#8217;ve no doubt that it&#8217;s entirely possible to run a WP-based site and keep it entirely clean of spam, etc.</p>

<p>However, since I&#8217;m not a frequent blogger, I&#8217;m sure that my WP install is out-of-date more often than it should be, which opens me up to attacks. I could and should have been more vigilant, and this isn&#8217;t a criticism of WordPress, it&#8217;s more the realization that WordPress isn&#8217;t the tool that fits my needs (if I chop off my hand because I&#8217;m using a chainsaw to slice bread, it&#8217;s not the chainsaw&#8217;s fault). In my personal battle against spam on this site, as a very smart computer once said, &#8220;the only winning move is not to play.&#8221;</p>

<p>So, all that said, here are some notes/comments on the migration process and my experience so far. A lot of this is me stumbling down a sometimes not terribly well-lit path, so you may have an easier time of it:</p>

<ul>
    <li>The installation process uses Ruby and installs a bunch of stuff via gems. I&#8217;ve fiddled around with Ruby and gem installation before while doing some client work, but my use of the phrase &#8220;bunch of stuff&#8221; should be a pretty clear indication that I&#8217;m not a Ruby developer. Following the instructions went reasonably well. I still need to (figure out how to) upgrade my Ruby installation, and at some point I installed something via `sudo` when I shouldn&#8217;t have, so I need to use `sudo` to run the rake tasks.</li>
    <li>Installing Octopress requires installing RVM, which is I think where I hit my `sudo` issues before. Having tried the reinstall as root, I&#8217;ve hit some confusing points. The install notes for RVM recommend installing Ruby 1.7.2, but then when it comes time to review the `.rvmrc` file for the site, I&#8217;m told I need to install 1.9.2. This is easily fixed by following the instructions in the install notes and just substituting the higher version number, but it would be nice if this were all a little clearer from the get-go.</li>
    <li>Even after that was done, I still get prompted to trust the `.rvmrc` file every time I switch into that directory.</li>
    <li>The instructions say to do `bundle install` but the bundler gem isn&#8217;t installed. I installed it as root, but that doesn&#8217;t seem to be helping. Installing it via `sudo` seems to work, so I&#8217;m not sure why installing it as `root` didn&#8217;t. But at least it works!</li>
    <li>For importing my existing WordPress posts, I exported an xml file from WordPress and then used <a href="http://martin.elwin.com/blog/2010/03/migrated-to-octopress/">this script</a> to get the data out. The script doesn&#8217;t import absolutely everything - I&#8217;ve lost comments, categories, and various other metadata-y bits. This isn&#8217;t a big deal because a) my blog doesn&#8217;t get a lot of (non-spam) comments, and b) it&#8217;s possible that the spam injection affected the database itself, so I&#8217;m inclined to think it&#8217;s best to leave some of that information in the db where it can&#8217;t do any more harm. One other note if you use that script: you need to change the value for &#8216;layout&#8217; from &#8216;blog_post&#8217; to just &#8216;post&#8217;.</li>
    <li>Related to the above point, the support for Octopress is fabulous. I hit the blog_post/post issue, and went to the Octopress support page, which is essentially a chat application, and Brandon Mathis seems to always be there. My question on the above was answered in about two minutes, and some additional helpful information was offered along the way.</li>
    <li>One thing I&#8217;m still figuring out is where Octopress stops and Jekyll starts, so to speak. For example, I&#8217;ve hit an issue where the older posts aren&#8217;t getting rendered properly because Jekyll by default uses a particular template for those pages. I&#8217;ve already bothered Brandon about this, but as I kept looking into it, it seems like it&#8217;s an issue with Jekyll, not with Octopress, <em>per se</em>.</li>
    <li>The documentation for various Jekyll-related things could be, um, a little better, let&#8217;s say.</li>
    <li>There are other import methods, including extracting data directly from your WordPress database. I wasn&#8217;t too concerned about getting absolutely everything out of the db, so I didn&#8217;t really mess around with these too much.</li>
    <li>The posts didn&#8217;t need to be cleaned up overly much at that point. Fixing a few paths for image tags, and changing the formatting of code snippets to use the built-in method, but that was all relatively quick.</li>
    <li>I&#8217;m not entirely sold on markdown just yet. Seems great for some things, but there are some things that it just doesn&#8217;t handle. I know that I can always fall back on writing actual HTML, so it&#8217;s not really an issue.</li>
    <li>Since Jekyll is the blogging framework behind Github, its support for code snippets is excellent as you&#8217;d expect. Equal or better the WordPress plugins that I&#8217;d tried before.</li>
</ul>


<p>Overall, was this harder than setting up a WordPress install? I&#8217;d say a little bit, although I don&#8217;t really remember how long it took me to set up WordPress the very first time, for what that&#8217;s worth. And while writing this post, I took some time to set up another blog - I&#8217;m planning to start blogging at my old osadchuk.org domain one of these days - and that went much more smoothly than the first time. Learning curves and all that. Overall, for a site with about 135 posts and a handful of pages, I&#8217;d say that the whole process of installing Octopress, importing and cleaning up the data, skinning the site to resemble the WordPress version, and going live took about eight hours. I&#8217;m considering that time very well spent.</p>
]]></content>
  </entry>
  
</feed>

