Lua

From G1Joshi/Agent-Skills · View interactive version

Lua is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Lua programming assistance covering tables, metatables, closures, coroutines, and C embedding. Use when writing game scripts, Neovim plugins, Nginx OpenResty modules, or embedded device logic.

How to use this skill

Skill file

Lua

A powerful, efficient, lightweight, embeddable scripting language.

When to Use

Quick Start

print("Hello, World!")

function factorial(n)
  if n == 0 then
    return 1
  else
    return n * factorial(n - 1)
  end
end

Core Concepts

Tables as Universal Data Structures

Lua represents arrays, hashes, objects, modules, and namespaces using a single unified primitive: the Table:

-- Array style (1-indexed!)
local fruits = {"Apple", "Banana", "Cherry"}
print("First item:", fruits[1])

-- Hash / Dictionary style
local config = {
  host = "127.0.0.1",
  port = 8080,
  ["max-connections"] = 100
}

-- Iterating key-value pairs
for key, value in pairs(config) do
  print(key .. " = " .. tostring(value))
end

Metatables & Object-Oriented Inheritance

Overloads table behaviors (operators, indexing, custom methods) via metamethods:

local Account = {}
Account.__index = Account

function Account.new(balance)
  local self = setmetatable({}, Account)
  self.balance = balance or 0
  return self
end

function Account:deposit(amount)
  self.balance = self.balance + amount
  return self.balance
end

local acc = Account.new(100)
acc:deposit(50)
print("Account balance:", acc.balance) -- 150

Coroutines for Cooperative Concurrency

Fibers with manual yield and resume control:

local co = coroutine.create(function()
  for i = 1, 3 do
    print("Coroutine yield:", i)
    coroutine.yield()
  end
end)

coroutine.resume(co) -- "Coroutine yield: 1"
coroutine.resume(co) -- "Coroutine yield: 2"

Common Patterns

Object-Oriented Class via Metatables

Problem: Lua lacks a native class keyword; object models must be built from first principles.

Solution:

Use metatables with the __index metamethod:

local Vector2 = {}
Vector2.__index = Vector2

function Vector2.new(x, y)
    local self = setmetatable({}, Vector2)
    self.x = x or 0
    self.y = y or 0
    return self
end

function Vector2:magnitude()
    return math.sqrt(self.x * self.x + self.y * self.y)
end

local v = Vector2.new(3, 4)
print("Magnitude:", v:magnitude()) -- Output: 5

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :-------------------------------------------------------------- | :-------------------------------------------------------------- | :------------------------------------------------------------------------ |

| attempt to index a nil value | Accessing key on a variable that evaluated to nil. | Guard access with if tbl and tbl.key then or assign default table {}. |

| attempt to call a nil value (method call with . instead of :) | Calling method with v.magnitude() instead of v:magnitude(). | Use colon syntax : to automatically pass self as the first argument. |

| 1-based indexing confusion | Treating first index in Lua array tables as 0. | Access first element with tbl[1] and iterate with ipairs. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.