Singletons in Lua

Since learning Lua and playing around with Corona and as it is not a very OOP language and is very script based, even more so that javascript, I felt like having a go at trying to see if it was possible to:

  • Structure my projects in a class based way to make them seem more familiar and cleanly organised
  • Mimic some design patterns to find out some of the strength / weaknesses of the language

My first task was structuring the projects I was creating to mimic a more OOP structure, this at first wasn't easy as most of the examples I could find were very much scripts full of procedural code. Then I stumbled upon Jesse Warden's Robotlegs for Corona. Written by a fellow actionscripter it was structured in a manner that was very familiar to me and helped a lot in figuring out the best methods for structuring my own code. So after a little playing I decided to see if I could easily create a singleton class in Lua. Now the simplest method of creating a singleton in Lua would be to create a name space with static methods using the ':' operator, which makes your method calls static. This can also be done in actionscript, but multiple instances within a project can still be created if called within different contexts, so the only sure fire way is with the 'getInstance()' call. So after a little playing I managed to structure a very simple Lua singleton, which I found to be a lot simpler than I expected:

Lua:
  1. Singleton = {};
  2.  
  3. local _instance;
  4.  
  5. function Singleton.getInstance()
  6.     if not _instance then
  7.         _instance = Singleton;
  8.     end
  9.  
  10.     --'any new methods would be added to the _instance object like this'
  11.     _instance.getType = function()
  12.         return 'singleton';
  13.     end
  14.  
  15.     return _instance
  16. end
  17.  
  18. function Singleton:new()
  19.     print('Singleton cannot be instantiated - use getInstance() instead');
  20. end

Now my only gripe about this class is that the 'Singleton' object can still be access globally and altered from outside of the class, but I guess this is just something you have to get used to with dynamic languages. This may not be the best way to do things but during my tests the object created and returned was always the same object, so if you think this is wrong and could be done better please do let me know.

Posted in Games, Lua | Tagged , | Leave a comment

New year – new challenges

OK with the dawn of a new year - don't worry no reflective blog post here ;) I think it is about time to dust off the cob webs with this blog and dedicate some time to not only blogging but also to my site which has been much neglected over the last 6 months due to an incredibly busy work life.

So in order to kick start things I have decided to set my self a challenge for at least the first 6 months of this year and see where it goes from there. I have many languages that I want to learn, there are so many that look really tempting and in order to get a feel for them I need to get my teeth into a project with them. So as I currently have a game idea in development I am intending to milk it to death by creating different versions - top down, side scroller, 3D version etc each with a different technology and language. Each game will have contain pretty much the same characters, to make it easy on myself, but the game theme and play will be different. The first version is going to be done with Lua & the Corona SDK, I then have a Javascript / Canvas version planned, then Unity3D in C# and also HaxeNME and then who knows what else. I will be fully documenting everything that happens here in this blog and posting demos of the games when each is ready. First I need to get to grips with Lua - it has been two days so far and it is looking like a pretty easy language to get used to... :)

Posted in Games, Lua | Tagged , , , | Leave a comment

Creating a custom HTML Wrapper template in FDT

Recently whist using playing with Molehill, which due to the fact that no stand alone debugger exists yet, as the player is still completely in beta version, I have been forced to compile and test my swfs in the browser to take advantage of the incubator build flash player. As I use FDT for my Actionscript dev I am able to run my applications in the browser from the offset, which is great, but the HTML wrapper that comes with FDT is the crappy one provided by Adobe that runs by default in Flash Builder and comes with the Flex SDK.

So how to get your own custom one to be built into your projects templates? Its easy, all you need to do is go to your Application Data folder ('%APPDATA%' on a Windows PC and 'User/Library/Application Support/' on a Mac) and in here you will need to go to 'FDT\projectTemplates\Web' where you will find the three default project templates which come with FDT. In any of these except the Flash Professional one, so lets start with the AS3 one, you will find a description.xml file, an icon and a project folder. If you look through the description.xml you will see a section which is commented .

XML:
  1. <!-- If HTML Wrapper -->
  2.         <folder src="html-wrapper" dest="${outputFolder}" if="${htmlTemplate}"
  3.             process="true" recursive="true" />

This of course means, if 'html template' has been selected, then put the whole 'html-wrapper' folder into the output folder and process the text in each file whilst you do. Simple. So in the project folder you will find the html-wrapper folder which contains all the needed files. The one we are going to edit is, 'index.html'. Now the default file comes with loads of crap, which whenever anyone looks at your source code, looks unelegant and ugly. I prefer to use swfobject for my html wrapper pages as it keeps the page clean, easy to read and only adds what that particular browser needs rather than having text for every eventuality. So I replace the whole file with this text:

HTML:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <title>${projectName}</title>
  7. <meta name="description" content="">
  8. <meta name="author" content="">
  9. <style type="text/css"
  10.             html{            
  11.                 height:100%;
  12.                 overflow: hidden;
  13.             }            
  14.             body{
  15.                 height:100%;
  16.                 margin: 0;
  17.                 padding: 0;
  18.                 background-color: #FFFFFF;
  19.                 overflow-y:hidden
  20.             }
  21.  
  22. </style>
  23. <script type="text/javascript" src="js/swfobject.js"></script>
  24. <script type="text/javascript">
  25.  
  26.         var flashvars = {};
  27.         var params = {};
  28.         var attributes = {};
  29.  
  30.         params.menu = "false";
  31.         params.quality = "high";
  32.         params.scale = "default";
  33.         params.bgcolor = "#FFFFFF";
  34.         params.allowFullScreen = "true";
  35.         params.AllowScriptAccess = "always";
  36.  
  37.         attributes.id = "flash";
  38.  
  39.         swfobject.embedSWF("Main.swf", "myContent", "100%", "100%", "10.0.0", "expressInstall.swf", flashvars, params, attributes);
  40. </script>
  41. </head>
  42. <body>
  43. <div id="myContent">
  44.     <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player" />
  45. </div>
  46. </body>
  47. </html>

As you can see it is much cleaner, I delete the history folder as I never use it, put swfobject in a 'js' folder, change playerProductInstall.swf for expressInstall.swf and away I go. As you can see the only parameter needed is the ${projectName} for the title of the html page, which gets parsed and changed to the title of the project when the project is first created.

If you want you can get the template here.

Posted in Actionscript 3.0, FDT, Javascript | Tagged , | Leave a comment