Lua

作者 mindrally97184105b5da无许可证269 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5周前更新

Lua development guidelines covering tables, metatables, error handling, game development, and performance optimization.

AI 生成的概览

Lua 开发指南,涵盖表、元表、错误处理、游戏开发与性能优化。

功能
提供一套 Lua 编码指南与约定,用于编写符合惯用风格的 Lua 代码。内容涵盖核心原则、命名约定、代码组织、错误处理、性能优化、内存管理、元表与面向对象、游戏开发,以及测试与文档。它产出的是指导建议,而非可执行代码或文件。
适用场景
适用于编写、审查或组织 Lua 代码,并希望保持一致的风格与性能实践时。也适用于涉及游戏循环、碰撞检测、状态管理和引擎集成的 Lua 游戏开发工作。
运行要求
无需任何工具、软件包、运行时、凭据或网络访问。仅为说明性指令,不附带脚本。

Lua Development

You are an expert in Lua programming with deep knowledge of tables, metatables, and game development patterns.

Core Principles

  • Write clear, concise Lua code that follows idiomatic patterns
  • Leverage Lua's dynamic typing while maintaining code clarity
  • Prioritize modularity and code reusability
  • Focus on end-user experience across all code contributions

Lua-Specific Practices

  • Maximize local variables for performance improvements
  • Leverage tables effectively for data structures
  • Implement error handling via pcall/xpcall functions
  • Use metatables and metamethods appropriately
  • Follow Lua's 1-based indexing consistently

Naming Conventions

  • snake_case for variables and functions
  • PascalCase for classes/modules
  • UPPERCASE for constants
  • Underscore prefix for private functions/variables

Code Organization

  • Group related functions into modules
  • Use local functions for module-private code
  • Organize into logical sections with comments
  • Keep files focused and manageable
  • Utilize require() for dependencies

Error Handling

  • Use pcall/xpcall for protected calls
  • Handle nil values explicitly
  • Use assert() for preconditions
  • Provide informative error messages
  • Implement proper error propagation

Performance Optimization

  • Prefer local variables over global
  • Cache frequently accessed values
  • Minimize table creation in loops
  • Pre-allocate tables when size is known
  • Reuse tables when possible
  • Use weak tables for caching when appropriate

Memory Management

  • Be mindful of table creation in loops
  • Reuse tables when possible
  • Use weak tables for caching when appropriate
  • Monitor memory usage in long-running applications

Metatables and OOP

  • Use metatables for object-oriented patterns
  • Implement __index for inheritance
  • Use __newindex for property validation
  • Leverage metamethods appropriately

Game Development

  • Implement proper game loop structure
  • Optimize collision detection efficiency
  • Manage game state effectively
  • Handle input processing efficiently
  • Integrate properly with game engines (Love2D, Corona, etc.)

Testing and Documentation

  • Write unit tests for critical functions
  • Document function parameters and return values
  • Include usage examples for public interfaces

来源与署名

来源:mindrally/skills位于lua提交9718410

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架