Here are some notes that have been useful for me. Also refer to this nice brief Introduction to Lua on the wxLua website
Tables
- Often you want to find everything in a table. There are two main ways of doing this. The simplest is to use table.foreach, like this:
The other main way of iterating through a table is to use the "for ... pairs" construct, like this:
NOTE: Variables only hold references to tables i.e. they are all pointers to the actual data, once all the references to a data are gone the data is garbage collected so any time you do operations on the variable its the pointed data that is being modified
Functions
Pass By Value or Pass By Reference?
From here
Pre-Defined functions
require
- A module is a library that can be loaded through require and that defines one single global name containing a table.
- package.path variable has the paths that the require function searches for the lua module. Every path is separated by a semi-colon and if there is any question mark in the path then that is replaced by the module name.
- if nothing is found from package.path then lua searches from package.cpath assuming the module is a C library to be linked.
- Well-behaved C libraries should export one function called luaopen_modname, which is the function that require tries to call after linking the library.
- if a module is named a-b, require expects its open function to be named luaopen_b, instead of luaopen_a-b (which would not be a valid C name anyway). So, if we need to use two modules named mod, we can rename one of them to v1-mod (or -mod, or anything like that). When we call m1=require"v1-mod", require will find both the renamed file v1-mod and, inside this file, the function with the original name luaopen_mod. This allows maintaining multiple versions of C libraries and load them simultaneously.
Object Oriented Programming in Lua
Public and Private
Everything private would have to be an upvalue
To create a class with public and private methods create a class creating function and define all methods and variables inside the class. When returning the object filled up with those defined functions and variables just don't include the variables and functions you want to be private since now they become upvalues of the functions and can only be accessed by them. Example from Programming in Lua:
General Structure
Important things to note:
- Private static things cannot see private variables or functions
- To be able to see a private static thing either by new or another private static thing like var4 it must be defined before, i.e. if var is a function var cannot see var4, but var4 can see var just fine since it was defined before. The way to get around is by declaring a reference first as follows:
Protected Variables
The above structure returns a object. To derive and inherit the object we will have to define a __index metamethod or if everything in the object is to be exposed to the derived object then just the object can be set in the __index value. The __index metamethod will define protected variablesInheritance
If every object as defined above is used then inheritance and multiple inheritance works easily:Multiple Inheritance
Programming tricks
- To find out all undeclared variables i.e. variables going into Global space do:
- and then search bytecode.txt for SETGLOBAL to find all global variables.
Deployment of software
- Used my custom script many2one.lua to combine all lua files (linked by require) into one main file. (All it does it defines an alternate require function that uses loadstring instead of loadfile and includes all the files as strings in the beginning of the main file)
- Used wxluafreeze to convert it to an exe using the command: