Category Archives: Lua

Preventing Duplicate dofile() Calls in Lua

I’m using dofile() to pull in support files, similar to how you would use #include <filename> in C++.  However, you can’t do the C++ #ifdef syntax to prevent a file from getting included more than once. You can however customize the dofile() function to do something similar.  Remember that all function names are variables that you can assign in Lua.  Here’s the code:

local oldDoFile = dofile
local loadedFiles = {}
function dofile(filename)
  if loadedFiles[filename] ~= true then
    print("Loading " .. filename)
    oldDoFile(filename)
    loadedFiles[filename] = true
  else
    print("Skipping load for " .. filename)
  end
end

First you save a reference to the existing dofile() function, but call it oldDoFile.  We also have a local table to store all filenames that have been loaded called “loadedFiles”.  Then we can redefine dofile() to check this table before calling the oldDoFile() function.  Now you can add dofile() calls to the tops of your various .lua scripts without worrying that a file will get included twice!

My Proposed Lua Style Guide

I don’t know much about how people typically style their Lua code, but I’ve picked a few guidelines I’m going to try to stick to.  They seem consistent with what the Moai SDK uses, and if you end up working with my code, please try to stick to it:

  • Always keep the scope as small as possible, which means marking most things “local”, including local helper functions.
  • Local variables are in camel case, starting with a lowercase character, without any kind of Hungarian notation.  For example “local numberOfItems”.  Since these are local, if you want you can go with “local number_of_items”, since it only affects the scope you are working on, so I don’t care so much.
  • Global variables are in camel case, starting with an uppercase character.  This includes classes and global functions.  For example “SpriteManager = CreateClass()” or “MyGlobalVariable = 1”
  • Keys in tables are in camel case, starting with a lowercase character.  This includes function names in classes.  For example “function SpriteManager:draw()”.
  • File names are all lowercase with underscores separating words.  For example “sprite_manager.lua”.
  • Tabs are 2 spaces.
  • Comment on any global variables that get created in functions.

Other than these items, I am not following an other strict style guide.  I did come across the style guide the Moai developers follow, and it is super long and really specific.  I haven’t found that it is worth it to care so much about such little things.  Also when in doubt, just match the code around what you are working with.

As a general design principle, I am still trying to stick with the general OOP concepts you would use in C++/C#/Java.  I’ve read that the “Lua” way to do things is not do use all these OOP patterns and just keep things as simple as possible, but I can’t imagine that scales well to larger projects, or would be easy to port to other languages.

Of course since I’m just starting out here, things might change, and if you can think of a better way to do things, let me know!

Classes with Lua

Even though Lua doesn’t have the concept of classes, we can still use tables to simulate them.  You can create tables with string to number/string/bool to represent member variables, and string to functions to represent member functions.  This would be an example of a table with some of these defined:

--This is the syntax to create a new table
Foo = {}
Foo.memberVariable = 1
function Foo.memberFunction()
  print("hi")
end

Continue reading Classes with Lua

Lua Basics

I’ve worked with a handful of scripting languages before (DCL, perl, python) but I’ve never written more than a few projects in any one.  Since you are usually doing something pretty simple with them, I typically just find a tutorial, and look up just enough to get my work done.

I’m just going to list some interesting points about the Lua language here, and if you need more information, you can search for it online.  This should be enough for you to get enough of an understanding to read basic scripts.
Continue reading Lua Basics