MTA:SA Lua Enums - TypeScript-inspired Enumerations for Lua
Overview
A lightweight, type-safe enumeration library for Multi Theft Auto: San Andreas, bringing TypeScript-like enum functionality to Lua. This library provides a clean, intuitive way to define and use enumerations in your MTA:SA scripts.
Key Features
Multiple Syntax Styles: Create enums using traditional function calls or with a more elegant syntax similar to TypeScript
Global and Local Enums: Define global enums for application-wide constants or local enums for module-specific use
Type Safety: Validate enum values and prevent modification after creation
Auto-incrementing Values: Automatically assign sequential values to enum members
Utility Methods: Easily convert between enum values and names with built-in helper functions
Example Usage
-- Global enum with string name and curly braces
enum 'Direction' {
NORTH = 1,
EAST = 2,
SOUTH = 3,
WEST = 4
}
-- Local enum with direct table assignment
local HttpStatus = enum {
OK = 200,
NOT_FOUND = 404,
SERVER_ERROR = 500
}
-- Auto-incrementing values
enum 'Size' { "SMALL", "MEDIUM", "LARGE" }
-- Using enum values
local direction = Direction.NORTH
local status = HttpStatus.OK
-- Get name from value
local directionName = Direction.getName(1) -- Returns "NORTH"
-- Check if value belongs to enum
if HttpStatus(200) then
-- 200 is a valid HttpStatus value
end
Why Use This Library?
Clean Code: Eliminate magic numbers and strings with descriptive enum constants
Error Prevention: Catch typos and invalid values at runtime
Self-documenting: Make your code more readable and maintainable
Flexibility: Choose the syntax style that best fits your coding preferences
Lightweight: Minimal overhead with no external dependencies
Installation
Simply include the enum.lua file in your project and add it to your meta.xml:
<script src="path/to/enum.lua" type="shared" cache="false" />
Check out the full source code at: https://github.com/norelockk/mtasa-lua-enums
This library is perfect for MTA:SA developers looking to write cleaner, more maintainable code with proper type safety. Whether you're building a simple gamemode or a complex resource, these enums will help organize your constants and prevent common errors.