Lua Tutorial

Read(23043) update time(2022-04-13)

Lua is a small scripting language. It is a research team at the Pontifical Catholic University of Rio de Janeiro in Brazil, composed of Roberto Ierusalimschy, Waldemar Celes and Luiz Henrique de Figueiredo and developed in 1993. It is designed to be embedded in applications to provide them with flexible extensions and customization capabilities. Lua is written in standard C and can be compiled and run on almost all operating systems and platforms. Lua does not provide a powerful library, which is determined by its positioning. Therefore, Lua is not suitable as a language for developing stand-alone applications. Lua has a concurrent JIT project that provides just-in-time compilation capabilities on specific platforms.


Lua scripts can be easily called by C/C code, and can also call C/C functions in turn, which makes Lua widely used in applications. Not only as an extension script, but also as an ordinary configuration file, replacing XML, ini and other file formats, and is easier to understand and maintain.

Let’s experience the first Lua program!

The first Lua program

Instance

print("Hello World!")

Run instance»

Click "Run instance" " button to view online examples

Lua is written in standard C. The code is simple and beautiful, and can be compiled and run on almost all operating systems and platforms. A complete Lua interpreter only costs 200k. Among all current script engines, Lua is the fastest. All this determines that Lua is the best choice for embedded scripts.

Tips: Our Lua tutorials will help you learn Lua knowledge from beginner to advanced. If you have any questions, please go to the PHP Chinese website Lua Community to ask your question, and enthusiastic netizens will answer it for you.

Lua features

  • Lightweight

##The official version of the Lua language only includes a streamlined core and The most basic library. This makes Lua small in size and fast to start, making it suitable for embedding in other programs. The kernel of Lua version 5.0.2 is less than 120KB, while the kernel of Python is about 860KB and the kernel of Perl is about 1.1MB.

  • Extensible

Lua is not like many other "big and comprehensive" languages, including many functions, such as network communication, graphics interface etc. But Lua provides very easy-to-use extension interfaces and mechanisms: the host language (usually C or C++) provides these functions, and Lua can use them as if they were built-in functions.

  • Other features

Lua also has some other features: it supports both procedure-oriented programming and functional programming ); automatic memory management; only provides a general type of table (table), which can be used to implement arrays, hash tables, collections, objects; language built-in pattern matching; closure; functions can also be regarded as a Value; provides support for multi-threading (co-process [4], not threads supported by the operating system); through closures and tables, some key mechanisms required for object-oriented programming can be easily supported, such as data abstraction, virtual functions, and inheritance. and overloading etc.

Lua Goals

The goal of Lua is to become a language that is easily embedded in other languages. Most programmers agree that it does this.

Many applications and games use LUA as their embedded scripting language to achieve configurability and scalability. These include World of Warcraft, Baldur's Gate, Angry Birds, QQ Three Kingdoms, VOCALOID3, Garry's Mod, Sun God Three Kingdoms, Yu-Gi-Oh! ygocore and Don't Starve, etc.

Lua application scenario

  • Game development

  • Independent application script

  • ## Web application scripts
  • Extensions and database plug-ins such as: MySQL Proxy and MySQL WorkBench
  • Security systems such as intrusion detection systems
  • Content covered by this Lua tutorial manual

This Lua tutorial covers all basic and advanced knowledge of Lua, including basic Lua syntax, Lua process control, Lua functions, Lua operators, and Lua characters Basic and advanced knowledge of Lua such as strings, Lua arrays, Lua iterators, Lua error handling, Lua object-oriented, etc.

Tips: Each chapter of this tutorial contains many Lua examples. You can directly click the "Run Example" button to view the results online. These examples will help you better understand and use Lua language.

Latest chapter


Lua 数据库访问 2016-10-20
Lua 面向对象 2016-10-18
Lua 垃圾回收 2016-10-18
Lua 调试(Debug) 2016-10-18
Lua 错误处理 2016-10-18
Lua 文件 I/O 2016-10-18
Lua 协同程序(coroutine) 2016-10-18
Lua 元表(Metatable) 2016-10-18