Skip to the content ForoHackers
Log inJoin

Lua: start here

Lua
#569 · Ophuscado · · Report

Lua is a small, fast, dynamically typed language designed to be embedded in other programs. Its whole implementation is tiny and it talks to C easily, so games, editors, network devices and embedded systems use it for configuration and scripting. It has few features, and builds everything from one data structure, the table.

Key concepts

  • Tables: the only data structure, used as array, dictionary, object and module.
  • First-class functions and closures: functions are values and capture their variables.
  • Metatables: tables whose behaviour, such as indexing and operators, is customised, which builds objects and inheritance.
  • Coroutines: cooperative tasks that yield and resume, useful for generators and state machines.
  • Embedding: a host program creates a Lua state, exposes functions and runs scripts through a stack-based C interface.
  • Small standard library: deliberately little, so hosts provide the rest.

Vocabulary

  • Chunk: a piece of Lua code loaded and run as a function.
  • Metamethod: a function in a metatable that overrides an operation.
  • Host: the program that embeds the Lua interpreter.
  • Nil: the single absent value, and the only false besides false itself.

What to study, in order

  1. Basic syntax, tables, loops and functions, using the standalone interpreter.
  2. Closures, varargs, string handling and error handling with pcall.
  3. Metatables: build a class system and operator overloading.
  4. Coroutines: write a generator and a simple scheduler.
  5. Embed Lua in a small C program and expose one function to scripts.
  6. Read the language's reference manual, which is short and complete.

Related tags

#interpreters #game-development #embedded-linux #c

Log in or join to reply.