luaguides

Guides

Guides

In-depth guides for Lua developers.

  1. Lua MQTT: Connecting IoT Devices with Lightweight Messaging

    Learn how to use Lua MQTT to connect IoT devices. Covers pub/sub patterns, QoS levels, retained messages, will messages, and production deployment checklists.

  2. Cross-Platform Scripting with Lua

    Cross-platform scripting with Lua: handle paths, line endings, and environment variables on Windows, macOS, and Linux. Write portable scripts that run anywhere.

  3. Dependency Management Patterns in Lua

    Master Lua dependency management 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 event systems in plain Lua: callbacks, publish-subscribe, message queues, coroutines, and OpenResty request handling.

  5. Logging Patterns and Frameworks in Lua

    Explore practical logging patterns for Lua: add timestamps and log levels, write to files, use LuaLogging for advanced needs, and build a structured logger.

  6. Designing Plugin Architectures in Lua

    Designing plugin architectures in Lua: load plugins as modules, register hooks, handle lifecycle events, and sandbox untrusted code.

  7. Building Template Engines in Lua

    Building template engines in Lua: string interpolation with gsub, pattern-based templates, buffered output with partials, escaping, and when to use 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

    Scripting Redis with EVAL and Lua: atomic server-side logic, rate limiters, counters, and replacing pipelines with single scripts.

  10. SQLite Lua: A Complete Integration Guide with lsqlite3

    Learn SQLite Lua database integration with lsqlite3 and luasql. Covers queries, prepared statements, transactions, error handling, and OpenResty setup.

  11. Implementing State Machines in Lua

    Implementing state machines in Lua using tables, metatables, and coroutines. Covers guard conditions, hierarchical states, and practical game patterns.

  12. Lua configuration: using Lua tables for flexible app settings

    Lua configuration turns .lua files into powerful app settings. Load tables with loadfile, branch by environment, validate keys with metatables.

  13. Lua-C Interop: A Practical Guide

    Master Lua-C interop with the official API: register C functions, call Lua from C, and move data safely across the language boundary.

  14. Writing World of Warcraft Addons in Lua

    Writing World of Warcraft addons from scratch with Lua: the .toc structure, event-driven model, saved variables, slash commands, and the full WoW Lua API.

  15. Building Domain-Specific Languages in Lua

    A guide to building domain-specific languages in Lua using closures and metatables. Design fluent APIs for configuration, validation, and query building.

  16. Functional Programming Patterns in Lua

    Master functional programming patterns in Lua with map, filter, fold, composition, pipelines, and immutability — all within Lua's table-based style.

  17. Memory Optimization Techniques

    Learn memory optimization techniques for Lua: fix leaks with object pooling, weak tables, and GC profiling to cut allocation rates in hot loops.

  18. Profiling Lua Code for Bottlenecks

    Profiling Lua programs to find bottlenecks: os.clock() timing, debug.sethook() sampling, and LuaJIT's profiler. A practical workflow for real code.

  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 to use LuaJIT FFI for calling C functions and working with C data structures directly from Lua. No extension modules or C boilerplate required.

  21. LPeg Parsing in Lua: PEG Grammars, Captures, and the re Module

    Learn LPeg parsing in Lua: PEG grammars, pattern captures, table and fold captures, and the re module for structured text processing.

  22. TCP and HTTP Networking in Lua with LuaSocket

    Build TCP clients and servers with LuaSocket, send HTTP requests, parse URLs, and handle networking patterns in Lua. Covers timeouts and non-blocking I/O.

  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

    Lua performance tips: table pre-allocation, local caching, table.concat, and benchmarking. Techniques to write faster Lua code with measurable results.

  25. Serializing Lua Tables to Disk

    Serializing Lua tables to disk with string concat, json.lua, MessagePack, and the C API. Examples for persisting configuration, game state, and cache data.

  26. Sorting Tables: Comparators and Stability

    Sorting tables in Lua: learn table.sort(), write comparators for multi-field sorts, handle case-insensitive ordering, and build stable sort alternatives.

  27. Lua Closures: Upvalues and Practical Patterns

    Learn how Lua closures capture upvalues as live references, with practical patterns for factory functions, private state, memoization, and currying.

  28. Error Handling Patterns and Best Practices

    Master Lua error handling with practical patterns for nil-plus-error returns, guard clauses, safe wrappers, and batch error collection.

  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. Writing Custom Iterators in Lua

    Writing custom iterators in Lua, from stateless factories and closure-based loops to coroutine-driven traversals for graphs, trees, and streams.

  31. Lua's Garbage Collector Explained

    Understand Lua's garbage collector: tri-color algorithm, incremental and generational modes, collectgarbage() tuning, weak references, and __gc finalizers.

  32. Sandboxing Lua for Safe Execution

    Learn sandboxing Lua for safe code execution. Covers custom environments, metatable access controls, and execution limits with debug.sethook.

  33. Lua String Patterns vs Regular Expressions

    Lua string patterns vs regex: learn magic characters, character classes, repetition modifiers, balanced pairs with %b, and the frontier pattern.

  34. Testing Lua Code with Busted: A Practical Guide

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

  35. How Lua Weak Tables Work and When to Use Them

    How Lua weak tables let you hold references without blocking garbage collection. Covers all three modes, memoization caches, and observer patterns.

  36. Metatables and Metamethods in Lua

    Learn how Lua metatables customize table behavior through __index, __newindex, and operator overloading to build object-oriented patterns and prototypes.

  37. Understanding Lua Environments and _ENV

    Understanding Lua environments: how global variable lookups work through _ENV upvalues, sandbox code with custom environments, and build secure module systems.

  38. Filesystem Operations with LuaFileSystem

    Learn filesystem operations in Lua with LuaFileSystem (lfs): working with directories, file attributes, timestamps, and file locking across platforms.

  39. MoonScript: CoffeeScript for Lua

    MoonScript brings CoffeeScript syntax to Lua: classes, arrow functions, and comprehensions that compile to readable standard Lua with no runtime dependencies.

  40. Managing Packages with LuaRocks

    Learn how managing packages with LuaRocks works: install, search, list, remove, and publish your own rocks to the luarocks.org repository.

  41. Penlight Batteries-Included Library: Lua Utility Guide

    Penlight batteries-included library extends Lua with utilities for tables, strings, files, and config parsing. Practical examples included.

  42. Teal: Typed Lua That Compiles to Lua

    Add types to Lua with Teal: a typed dialect compiling to plain Lua that catches bugs at compile time while running on any runtime from 5.1 through LuaJIT.