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)

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)

Defines the constructor function for the class.

Parameters:

  • func (function) - The constructor function

Example:

Class:method(name, func)

Adds a method to the class.

Parameters:

  • name (string) - The name of the method

  • func (function) - The method function

Example:

Class:static(name, func)

Adds a static method to the class.

Parameters:

  • name (string) - The name of the static method

  • func (function) - The static method function

Example:

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)

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)

Adds a private method to the class (convention-based).

Parameters:

  • name (string) - The name of the private method

  • func (function) - The private method function

Example:

instance:super(...)

Calls the parent class constructor or method.

Parameters:

  • ... - Arguments to pass to the parent

Example:

Inheritance Example

Best Practices

  1. Use descriptive class names - Make class names clear and specific

  2. Initialize all properties - Set default values in the constructor

  3. Validate inputs - Always validate parameters in methods

  4. Use inheritance appropriately - Don't over-engineer simple classes

Last updated