Class System
The Class System provides a simple and intuitive way to create object-oriented code in Lua, making it easier to organize and structure your FiveM scripts.
Compatibility
✅ Shared - Works on both client and server-side
Available Methods
class(name)
class(name)Creates a new class with the specified name.
Parameters:
name(string) - The name of the class
Returns:
table- The created class
Example:
local Animal = class("Animal")
-- Or use global access
local Animal = class("Animal")Class:constructor(func)
Class:constructor(func)Defines the constructor function for the class.
Parameters:
func(function) - The constructor function
Example:
Class:method(name, func)
Class:method(name, func)Adds a method to the class.
Parameters:
name(string) - The name of the methodfunc(function) - The method function
Example:
Class:static(name, func)
Class:static(name, func)Adds a static method to the class.
Parameters:
name(string) - The name of the static methodfunc(function) - The static method function
Example:
Class:new(...)
Class:new(...)Creates a new instance of the class.
Parameters:
...- Arguments to pass to the constructor
Returns:
table- The new instance
Example:
Class:extend(name)
Class:extend(name)Creates a new class that extends the current class.
Parameters:
name(string) - The name of the new class
Returns:
table- The new extended class
Example:
Class:private(name, func)
Class:private(name, func)Adds a private method to the class (convention-based).
Parameters:
name(string) - The name of the private methodfunc(function) - The private method function
Example:
instance:super(...)
instance:super(...)Calls the parent class constructor or method.
Parameters:
...- Arguments to pass to the parent
Example:
Inheritance Example
Best Practices
Use descriptive class names - Make class names clear and specific
Initialize all properties - Set default values in the constructor
Validate inputs - Always validate parameters in methods
Use inheritance appropriately - Don't over-engineer simple classes
Last updated