luaguides

Guides

Guides

In-depth guides for Lua developers.

  1. Lua for MQTT and IoT Devices

    Learn how to use MQTT to connect Lua applications to IoT devices with this practical guide covering pub/sub patterns, QoS levels, and real-world code examples.

  2. Cross-Platform Scripting with Lua

    Write Lua scripts that run on Windows, macOS, and Linux. Covers paths, line endings, executable discovery, and platform detection.

  3. Dependency Management Patterns in Lua

    Manage Lua dependencies with luarocks, submodule imports, and inline bundles. Find the right approach for your project size and deployment target.

  4. Event Systems and Message Passing in Lua

    Build a simple event system in plain Lua. Covers callbacks, publish-subscribe, message queues, and integrating with game loops or OpenResty request handling.

  5. Logging Patterns and Frameworks in Lua

    Add timestamps, log levels, and output to files with plain Lua. Then level up with LuaLogging or build your own structured logger.

  6. Designing Plugin Architectures in Lua

    Build extensible Lua applications with plugin systems.Coyer plugins as modules, register hooks, handle lifecycle events, and isolate plugin code safely.

  7. Building Template Engines in Lua

    Build template engines in Lua from scratch. Covers string interpolation, pattern-based templates, output buffering, and when to reach for LPeg.

  8. Finite Automata and Lexers in Lua

    Build tokenizers and lexers in Lua with deterministic finite automata. Learn how DFAs work, recognize token patterns, and explore Lua patterns and LPeg.

  9. Scripting Redis with EVAL and Lua

    Write atomic Lua scripts inside Redis with EVAL. Implement transaction-like patterns, atomic counters, and complex logic without multiple round-trips.

  10. Using SQLite from Lua

    Connect Lua to SQLite using lsqlite3 or luasql. Execute queries, use prepared statements, handle transactions, and integrate with OpenResty.

  11. Implementing State Machines in Lua

    Build state machines in Lua using tables, metatables, and coroutines. Covers the state pattern, event-driven transitions, and practical game and UI examples.

  12. Lua as a Configuration Language

    Use Lua files as configuration. Return tables from .lua files, load them with loadfile, and validate settings with metatables.

  13. Lua-C Interop: A Practical Guide

    Connect Lua and C code using the official C API. Register C functions for Lua, call Lua from C programs, and move data across the language boundary safely.

  14. Writing World of Warcraft Addons in Lua

    Build a World of Warcraft addon with Lua. Learn the addon structure, event-driven model, saved variables, chat commands, and the WoW Lua API.

  15. Building Domain-Specific Languages in Lua

    Build domain-specific languages in Lua with closures, metatables, and tables. Design fluent APIs for configuration, validation, and game scripting.

  16. Functional Programming Patterns in Lua

    Apply functional ideas in Lua: higher-order functions, composition, pipelines, and immutability patterns without abandoning Lua's table-based style.

  17. Memory Optimization Techniques

    Reduce memory usage in Lua programs by fixing leaks, reusing objects, and understanding how Lua allocates and collects memory.

  18. Profiling Lua Code for Bottlenecks

    Learn how to find performance bottlenecks in Lua programs using manual timing, debug hooks, and LuaJIT's built-in profiler.

  19. Working with Binary Data in Lua 5.3+

    Serialize and deserialize binary data using string.pack, unpack, and packsize. Covers format specifiers, endianness, and common pitfalls.

  20. LuaJIT FFI: Calling C from Lua

    Learn how LuaJIT's FFI library lets you call C functions and use C data structures directly from Lua without writing extension modules.

  21. Parsing with LPeg and re

    Parse structured text using LPeg, a PEG-based parsing library that ships with Lua 5.2+, and its companion re module.

  22. TCP and HTTP with LuaSocket

    Learn to use LuaSocket for TCP client/server sockets, HTTP requests, URL parsing, and common networking patterns in Lua.

  23. Parsing and Generating JSON in Lua

    Learn how to parse and generate JSON in Lua using dkjson, cjson, and json.lua with practical examples and common gotchas.

  24. Lua Performance Tips and Benchmarking

    Practical techniques to write faster Lua: table pre-allocation, local caching, table.concat, and correct benchmarking.

  25. Serializing Lua Tables to Disk

    Learn how to serialize Lua tables to disk using built-in approaches, json.lua, MessagePack, and the Lua C API.

  26. Sorting Tables: Comparators and Stability

    Learn how table.sort() works, how to write custom comparators, and why Lua's sort is not stable.

  27. Closures and Upvalues in Practice

    Learn how Lua closures capture variables from enclosing scopes, how upvalues work as live references, and practical patterns for stateful functions.

  28. Error Handling Patterns and Best Practices

    Learn how to handle errors gracefully in Lua using pcall, xpcall, and patterns that keep your programs running.

  29. The debug Library: Inspection and Hooks

    Explore Lua's debug library for inspecting stack frames, local variables, and upvalues at runtime, plus hooks for tracing and profiling code execution.

  30. Lua's Garbage Collector Explained

    Learn how Lua's automatic garbage collector works, including the tri-color algorithm, collectgarbage() API, weak tables, and performance tuning.

  31. Writing Custom Iterators in Lua

    Build custom iterators in Lua with closures, factory functions, and coroutines. Practical examples included.

  32. Sandboxing Lua for Safe Execution

    Learn to safely execute untrusted Lua code. Control globals, block dangerous functions, and enforce CPU and memory limits.

  33. Lua String Patterns vs Regular Expressions

    Lua string patterns look like regex but aren't. Learn the key differences, unique features, and the gotchas that'll trip you up.

  34. Testing Lua Code with Busted

    Practical patterns for testing Lua with Busted — running specific tests, structuring test suites, isolating state, and integrating with CI pipelines.

  35. Weak Tables and Their Uses

    Learn how weak tables let you hold references to objects without preventing garbage collection, with coverage of all three modes and practical use cases.

  36. Metatables and Metamethods in Lua

    Learn how metatables let you customize table behavior, implement operator overloading, and build OOP patterns in Lua.

  37. Understanding Lua Environments and _ENV

    Learn how Lua uses _ENV as an upvalue for global variable lookups, how to load code with custom environments, and how environments enable sandboxing.

  38. Filesystem Operations with LuaFileSystem

    Guide to LuaFileSystem (lfs): working with directories, file attributes, timestamps, and file locking on Linux, macOS, and Windows.

  39. Managing Packages with LuaRocks

    LuaRocks is the package manager for Lua. Learn to install, search, list, and remove packages from luarocks.org repository, and how to publish your own rocks.

  40. Penlight: A Batteries-Included Library

    Penlight extends Lua's standard library with utilities for tables, strings, files, paths, and more. Learn the most useful modules with practical examples.

  41. MoonScript: CoffeeScript for Lua

    MoonScript compiles to Lua, adding cleaner syntax, classes, arrow functions, and table comprehensions that make code easier to read and write.

  42. Teal: Typed Lua That Compiles to Lua

    Teal is a typed dialect of Lua that compiles to plain Lua. Add type annotations to catch bugs at compile time, then run on any Lua runtime.