Questions tagged [lua]

Lua is a powerful, fast, lightweight, embeddable scripting language. It is dynamically typed, runs by interpreting bytecode, and has automatic garbage collection. Its speed is one of the main reasons it is widely used by the machine learning community. It is often referred to as an "extensible extension language".

This tag is used for questions about the Lua programming language.

From Lua's About page:

What is Lua?

Lua is a powerful, fast, lightweight, embeddable scripting language.

Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.

The official implementation of Lua is written in clean ANSI C, but a list of several other implementations can be found on the Lua Users Wiki page on Lua Implementations.

The latest official version of Lua is 5.4.1. The previous minor version (5.3.6) is available in the version history on the official site. Lua version numbers are defined as follows: major.minor.bugfix. Minor version increases will often no longer be backwards compatible with prior minor versions.

Lua is certified Open Source software distributed under the terms of the MIT license. As such, Lua is free to use, even for commercial products.

Please note that the official name of this programming language is Lua (a Portuguese word for Earth's moon). It is not an acronym -- it is not spelled LUA.

Learning Lua

Resources

  • Lua.org - The official Lua site.
  • LuaJIT - A fast tracing Just-In-Time compiler for Lua 5.1.
  • LuaRocks - the package manager for Lua modules.
  • ZeroBrane Studio - An IDE and debugger supporting a variety of Lua systems.
  • Lua Development Tools - An Eclipse-based IDE and debugger.
  • LuaDist - Lua modules management system with Virtual Environment capabilities.

Some projects using Lua

  • Torch - A scientific computing framework based on Lua[JIT] with strong CPU and CUDA backends.
  • lua-alchemy - Port of the Lua programming language for ActionScript using Alchemy.
  • Nutria - PHP Standard Library Written in Lua Programming Language
  • Sputnik - A content management system designed for extensibility.

Community

Other places for discussing Lua, beyond the question & answer format of Stack Overflow:

Books

22266 questions
28
votes
6 answers

Forward define a function in Lua?

How do I call a function that needs to be called from above its creation? I read something about forward declarations, but Google isn't being helpful in this case. What is the correct syntax for this?
Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
28
votes
9 answers

Python or Ruby Interpreter on iOS

I found this application on the app store: iLuaBox and I wondered if there was anything else like this for the iPhone without jailbreaking but instead for Python or Ruby? Lua is probably similar for me to play around with the basic programming I do…
YURI
  • 281
  • 1
  • 3
  • 3
28
votes
1 answer

How can I ignore first results from a function in Lua?

Lua functions can return multiple results : a, b, c = unpack({'one', 'two', 'three'}) If I'm not interested in the third return value, I can choose to ignore it when calling the function : a, b = unpack({'one', 'two', 'three'}) Is there a similar…
Thibault Falise
  • 5,795
  • 2
  • 29
  • 32
28
votes
3 answers

Why does Lua's length (#) operator return unexpected values?

Lua has the # operator to compute the "length" of a table being used as an array. I checked this operator and I am surprised. This is code, that I let run under Lua 5.2.3: t = {}; t[0] = 1; t[1] = 2; print(#t); -- 1 aha lua counts from one t[2] =…
LeMoussel
  • 5,290
  • 12
  • 69
  • 122
28
votes
5 answers

lua call function from a string with function name

Is it possible in lua to execute a function from a string representing its name? i.e: I have the string x = "foo", is it possible to do x() ? If yes what is the syntax ?
Gregoire
  • 24,219
  • 6
  • 46
  • 73
28
votes
1 answer

"For each" loop in a lua table with key value pairs

Say I have a table defined like this: myTable = { myValue = nil, myOtherValue = nil} How would I iterate through it in a for each fashion loop like this? for key,value in myTable do --pseudocode value = "foobar" end Also, if it helps, I…
sFuller
  • 1,305
  • 2
  • 14
  • 23
27
votes
1 answer

Embedding Lua in C++

I've been trying to embed lua in a c++ application but to no avail since the compiler complains about "lua_open".I'm using Lua 5.2. I found alot of articles claiming that lua_open() was replaced in the fifth version but none of them mentioned with…
NT_SYSTEM
  • 367
  • 1
  • 6
  • 14
27
votes
10 answers

Forcing a Lua script to exit

How do you end a long running Lua script? I have two threads, one runs the main program and the other controls a user supplied Lua script. I need to kill the thread that's running Lua, but first I need the script to exit. Is there a way to force a…
deft_code
  • 57,255
  • 29
  • 141
  • 224
27
votes
1 answer

Handle event callbacks with Luabind

I'm adding scripting with Lua to an application of ours, and I need to implement bindings for the GUI-toolkit. The toolkit we use is wxWidgets. I'm using Lua 5.1 and luabind 0.9.1, and it has worked very well so far. However, I'm not sure how to…
Jonatan
  • 3,752
  • 4
  • 36
  • 47
27
votes
10 answers

How Do I Install Lua on MacOS?

I just downloaded Lua from the official website. I want to install it on my Mac but I have no clue how. And I've never tried using Mac to install and use compilers other then (xcode , titanium , corona) so easy on me please :) I tried this link but…
Mohammad Fadin
  • 499
  • 5
  • 11
  • 22
27
votes
10 answers

How to calculate distance between two rectangles? (Context: a game in Lua.)

Given two rectangles with x, y, width, height in pixels and a rotation value in degrees -- how do I calculate the closest distance of their outlines toward each other? Background: In a game written in Lua I'm randomly generating maps, but want to…
Philipp Lenssen
  • 8,818
  • 13
  • 56
  • 77
27
votes
6 answers

how to delete all elements in a Lua table?

How do I delete all elements inside a Lua table? I don't want to do: t = {} table.insert(t, 1) t = {} -- this assigns a new pointer to t I want to retain the same pointer to t, but delete all elements within t. I tried: t = {} table.insert(t,…
bob
  • 1,941
  • 6
  • 26
  • 36
27
votes
9 answers

"main" function in Lua?

In python, one would usually define a main function, in order to allow the script to be used as module (if needed): def main(): print("Hello world") return 0 if __name__ == "__main__": sys.exit(main()) In Lua, the idiom if __name__ ==…
user350814
27
votes
4 answers

How to get the first character of a string in Lua?

Given a string s in Lua: s = "abc123" If the string is non-empty, I want to store the first character of this string in a variable s1, else store nothing. I've tried the following, but both return nil: s1 = s[1] s1 = s[0] How can I get the first…
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
27
votes
4 answers

How do I run an executable using Lua?

I have an executable I want to run using Lua ... how do I do this? Can't seem to find any documentation anywhere about this.
Brian T Hannan
  • 3,925
  • 18
  • 56
  • 96