If you’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.
For example, if you want to insert a value into a table, you’ll often see the code written like this:
1 2 |
|
This is different from, say, ActionScript, where the equivalent code would look like this:
1 2 |
|
Ignoring the differing syntax between insert
and splice
for the moment, notice that the splice
method is called directly on myArray
, which determines the scope. In Lua, the insert
method is akin to a static method of the table
module, and the first argument is the table that will have the value inserted.
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:
1 2 |
|
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’re writing functions in your own code, the colon serves the same purpose:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|